嗨有没有办法从不同的类添加JButton到JPanel。所以基本上JPanel在Class A
和JButton在Class B
我怎么能把按钮放在不同类的面板上。希望这是有道理的,如果你需要我澄清让我知道。我在这里先向您的帮助表示感谢。将JButton添加到来自不同类的JPanel
1
A
回答
0
你可以让这样的事情:
public OtherClass {
public JButton getButton(){
JButton b = new JButton();
b.set...();
b.set...();
b.set...();
b.set...();
return b;
}
}
然后你就可以使用这个功能来创建了一个JButton这始终是相同的。
另一种选择是创建按钮为静态,并在您OtherClass使用它,这不是一个很好的解决方案,但它可以是一种选择
0
您需要B类的实例对象A类访问其变量和方法。然后,您可以编写类似以下内容:
public ClassB {
public JButton getButton() {
return myJButton;
}
}
另一种方式来做到这一点是让JButton的B类静态的,但是这是一个肮脏的黑客是一个不好的设计模式。
public ClassB {
public static JButton myJButton;
}
然后,您可以通过使用ClassB.myJButton
0
可以继承类或者使用一个单一的一个访问来自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;
}
}
}
相关问题
- 1. 将JButton添加到JPanel
- 2. 添加的JButton到的JPanel
- 3. 来自不同类的JButton
- 4. JTextArea和JButton不会添加到JPanel
- 5. JButton添加到JPanel不显示
- 6. 如何动态添加JButton到JPanel?
- 7. 重画来自不同类的JPanel
- 8. 如何将JPanel添加到不同类的容器中?
- 9. 将JPanel从一个类添加到另一个类中的JPanel
- 10. 如何在JPanel中添加JButton?
- 11. 如何通过点击JButton添加JPanel?
- 12. 将听众添加到JButton
- 13. 将JButton添加到JTextPane
- 14. 将JButton添加到JTable中
- 15. 将JPanel添加到另一个类中的另一个JPanel
- 16. iOS将实体添加到来自类
- 17. 将jbutton和jlabel的数组添加到jpanel数组到框架中的问题
- 18. 将Jpanel添加到JFrame?
- 19. 将JPanel添加到JFrame中
- 20. 将JPanel添加到JList?
- 21. 将组件添加到JPanel
- 22. 将JFXPanel添加到JPanel
- 23. 将JPanel添加到Canvas
- 24. 将组件添加到JPanel
- 25. 将ButtonGroup添加到JPanel
- 26. 将按钮添加到JPanel
- 27. 将JScrollPane添加到JPanel?
- 28. 将path2d添加到jpanel
- 29. 将JScrollPane添加到JPanel
- 30. 将JDesktopPane添加到JPanel