从不同 Class 添加 JButton 到 JPanel
Add JButton to JPanel from Different Class
您好,有没有一种方法可以将不同的 JButton 添加到 JPanel Class。所以基本上 JPanel 在 Class A
中,而 JButton 在 Class B
中,我如何将按钮放在不同 class 中的面板上。希望这是有道理的,如果你需要我澄清让我知道。提前感谢您的帮助。
你可以做这样的事情:
public OtherClass {
public JButton getButton (){
JButton b = new JButton();
b.set...();
b.set...();
b.set...();
b.set...();
return b;
}
}
然后您可以使用此函数创建一个始终相同的 JButton。
另一种选择是将您的 Button 创建为静态按钮并在您的 OtherClass 中使用它,这不是一个很好的解决方案,但它可以是一个选项
您需要 class B 在 class A 中的实例对象来访问它的变量和方法。然后您可以编写如下内容:
public ClassB {
public JButton getButton() {
return myJButton;
}
}
另一种方法是在 class B 中使 JButton 静态化,但这是一种肮脏的 hack,是一种糟糕的设计模式。
public ClassB {
public static JButton myJButton;
}
然后您可以使用 ClassB.myJButton
从 ClassA 访问 JButton
您可以继承 类 或使用一个:
public class Example{
public static void main(String []args){
JFrame wnd = new JFrame();
//edit your frame...
//...
wnd.setContentPane(new CustomPanel()); //Panel from your class
wnd.getContentPane().add(new CustomButton()); //Button from another class
//Or this way:
wnd.setContenPane(new Items().CustomPanel());
wnd.getContentPane().add(new Items().CustomButton());
}
static class CustomButton extends JButton{
public CustomButton(){
//Implementation...
setSize(...);
setBackground(...);
addActionListener(new ActionListener(){
//....
});
}
}
static class CustomPanel extends JPanel{
public CustomPanel(){
//Implementation...
setSize(...);
setBackground(...);
OtherStuff
//....
}
}
static class Items{
public JButton CustomButton(){
JButton button = new JButton();
//Edit your button...
return button;
}
public JPanel CustomPanel(){
JPanel panel = new JPanel();
//Edit your panel...
return panel;
}
}
}
您好,有没有一种方法可以将不同的 JButton 添加到 JPanel Class。所以基本上 JPanel 在 Class A
中,而 JButton 在 Class B
中,我如何将按钮放在不同 class 中的面板上。希望这是有道理的,如果你需要我澄清让我知道。提前感谢您的帮助。
你可以做这样的事情:
public OtherClass {
public JButton getButton (){
JButton b = new JButton();
b.set...();
b.set...();
b.set...();
b.set...();
return b;
}
}
然后您可以使用此函数创建一个始终相同的 JButton。
另一种选择是将您的 Button 创建为静态按钮并在您的 OtherClass 中使用它,这不是一个很好的解决方案,但它可以是一个选项
您需要 class B 在 class A 中的实例对象来访问它的变量和方法。然后您可以编写如下内容:
public ClassB {
public JButton getButton() {
return myJButton;
}
}
另一种方法是在 class B 中使 JButton 静态化,但这是一种肮脏的 hack,是一种糟糕的设计模式。
public ClassB {
public static JButton myJButton;
}
然后您可以使用 ClassB.myJButton
您可以继承 类 或使用一个:
public class Example{
public static void main(String []args){
JFrame wnd = new JFrame();
//edit your frame...
//...
wnd.setContentPane(new CustomPanel()); //Panel from your class
wnd.getContentPane().add(new CustomButton()); //Button from another class
//Or this way:
wnd.setContenPane(new Items().CustomPanel());
wnd.getContentPane().add(new Items().CustomButton());
}
static class CustomButton extends JButton{
public CustomButton(){
//Implementation...
setSize(...);
setBackground(...);
addActionListener(new ActionListener(){
//....
});
}
}
static class CustomPanel extends JPanel{
public CustomPanel(){
//Implementation...
setSize(...);
setBackground(...);
OtherStuff
//....
}
}
static class Items{
public JButton CustomButton(){
JButton button = new JButton();
//Edit your button...
return button;
}
public JPanel CustomPanel(){
JPanel panel = new JPanel();
//Edit your panel...
return panel;
}
}
}