JButton 在单独的 class 文件中
JButton in separate class file
我写了 class,其中包含简单的 JFrame,我想添加其他按钮
class 到此 JFrame
当我尝试写作时
panel.add(new CancelButton())
我收到一个错误:LoginWindow.java:29:错误:找不到适合添加(取消按钮)的方法
你能帮我吗?
import javax.swing.*;
import java.awt.*;
public class LoginWindow {
public LoginWindow(){
//Login Window
JFrame frame = new JFrame("Movie date base");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//label
JLabel label = new JLabel("Welcom to Movie Date Base! ");
label.setFont(new Font("Verdana", Font.PLAIN, 18));
//panel
JPanel panel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel(new GridBagLayout());
//add panel to the frame
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
//Grid layout
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(100,10,10,10);
panel.add(label,c);
}
}
import javax.swing.*;
public class CancelButton {
public CancelButton(){
JButton cancel = new JButton("cancel");
}
}
如果您想将 CancelButton class 视为 JButton,则必须扩展 JButton。
public class CancelButton extends JButton{
public CancelButton(){
//call the parent-level constructor
super("cancel");
}
}
请注意,我在这里删除了您的取消变量,因为您的 CancelButton class is-a JButton 现在。您可以在任何可以使用 JButton 的地方使用它。
但是,如果您想在其他 class 中使用取消变量,则需要为其创建某种 getter 函数:
public class CancelButton {
JButton cancel;
public CancelButton(){
cancel = new JButton("cancel");
}
public JButton getButton(){
return cancel;
}
}
然后您将调用 getButton() 函数,如下所示:
panel.add(new CancelButton().getButton())
您采用哪种方法实际上取决于您尝试做什么,以及您希望如何组织代码。
如果您想保持原样,请将按钮放入数组中,然后在 jframe 中实例化取消 class 并从数组中调用。
我写了 class,其中包含简单的 JFrame,我想添加其他按钮 class 到此 JFrame
当我尝试写作时
panel.add(new CancelButton())
我收到一个错误:LoginWindow.java:29:错误:找不到适合添加(取消按钮)的方法
你能帮我吗?
import javax.swing.*;
import java.awt.*;
public class LoginWindow {
public LoginWindow(){
//Login Window
JFrame frame = new JFrame("Movie date base");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//label
JLabel label = new JLabel("Welcom to Movie Date Base! ");
label.setFont(new Font("Verdana", Font.PLAIN, 18));
//panel
JPanel panel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel(new GridBagLayout());
//add panel to the frame
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
//Grid layout
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(100,10,10,10);
panel.add(label,c);
}
}
import javax.swing.*;
public class CancelButton {
public CancelButton(){
JButton cancel = new JButton("cancel");
}
}
如果您想将 CancelButton class 视为 JButton,则必须扩展 JButton。
public class CancelButton extends JButton{
public CancelButton(){
//call the parent-level constructor
super("cancel");
}
}
请注意,我在这里删除了您的取消变量,因为您的 CancelButton class is-a JButton 现在。您可以在任何可以使用 JButton 的地方使用它。
但是,如果您想在其他 class 中使用取消变量,则需要为其创建某种 getter 函数:
public class CancelButton {
JButton cancel;
public CancelButton(){
cancel = new JButton("cancel");
}
public JButton getButton(){
return cancel;
}
}
然后您将调用 getButton() 函数,如下所示:
panel.add(new CancelButton().getButton())
您采用哪种方法实际上取决于您尝试做什么,以及您希望如何组织代码。
如果您想保持原样,请将按钮放入数组中,然后在 jframe 中实例化取消 class 并从数组中调用。