在我的应用程序中,我将有几个JPanels(10个或更多),每个都略有修改(附加按钮,标签等)。
所以我的想法是创建Panel类,有共同的元素(3个按钮,3个标签和3个文本字段 - 它们将位于同一位置的每个面板上,当然还有不同的数据) - 在我的例子中只有1个按钮,1个标签和1个文本字段。
在我的主要课程中,我想更改标签的名称,在文本字段中设置值,就像在我自己手动创建的面板中一样:
p1.btn1.setText("TEST");
我希望你能理解我的解释..谢谢。
正确执行此操作的最佳方法是什么?
到目前为止我做了什么:
public class Main {
JFrame f;
Panel1 p1;
Panel2 p2;
Panel3 p3;
JButton btn2, btn3;
JPanel panel2, panel3;
JLabel lblSpeed2, lblSpeed3;
JTextField txtSpeed2, txtSpeed3;
public Main() {
f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
p1 = new Panel1();
p2 = new Panel2(btn2, panel2, lblSpeed2, txtSpeed2, "Panel no. 2");
p3 = new Panel3(btn3, panel3, lblSpeed3, txtSpeed3, "Panel no. 3");
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
f.add(p1, c);
c.gridx = 0;
c.gridy = 1;
f.add(p2, c);
c.gridx = 0;
c.gridy = 2;
f.add(p3, c);
p1.btn1.setText("TEST");
}
public static void main(String[] args) {
Main m = new Main();
}
}
我手动创建的面板:
public class Panel1 extends JPanel {
JButton btn1;
JPanel panel1;
JLabel lblSpeed1;
JTextField txtSpeed1;
Border blackline;
public Panel1() {
super();
blackline = BorderFactory.createLineBorder(Color.gray);
panel1 = new JPanel(new GridBagLayout());
panel1.setBorder(blackline);
panel1.setBorder(BorderFactory
.createTitledBorder("Manually created Panel"));
btn1 = new JButton("Btn 1");
lblSpeed1 = new JLabel("Speed");
txtSpeed1 = new JTextField(5);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
panel1.add(btn1, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
panel1.add(lblSpeed1, c);
c.gridx = 1;
c.gridy = 1;
panel1.add(txtSpeed1, c);
add(panel1);
}
}
其他小组的模板:
public class Panel extends JPanel {
Border blackline;
public Panel(JButton btnSend, JPanel MPanel, JLabel lblSpeed,
JTextField txtSpeed, String Name) {
super();
blackline = BorderFactory.createLineBorder(Color.gray);
MPanel = new JPanel(new GridBagLayout());
MPanel.setBorder(blackline);
MPanel.setBorder(BorderFactory.createTitledBorder(Name));
btnSend = new JButton("Btn 1");
lblSpeed = new JLabel("Speed");
txtSpeed = new JTextField(5);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
MPanel.add(btnSend, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
MPanel.add(lblSpeed, c);
c.gridx = 1;
c.gridy = 1;
MPanel.add(txtSpeed, c);
add(MPanel);
}
}
最后是我的小组课程:
public class Panel2 extends Panel {
public Panel2(JButton btnSend, JPanel MPanel, JLabel lblSpeed,
JTextField txtSpeed, String Name) {
super(btnSend, MPanel, lblSpeed, txtSpeed, Name);
// TODO Auto-generated constructor stub
}
}
和
public class Panel3 extends Panel {
public Panel3(JButton btnSend, JPanel MPanel, JLabel lblSpeed,
JTextField txtSpeed, String Name) {
super(btnSend, MPanel, lblSpeed, txtSpeed, Name);
// TODO Auto-generated constructor stub
}
}
答案 0 :(得分:0)
你做的不是太好。你有一个强大而强大的语言来处理对象,让我们试着关注如何使用它们。
您只需使用SAME构造函数和SAME结构编写3个“不同”类。
为什么?
您可以使用3个不同的面板创建3个不同的实例:例如,我更改了您的代码:
p2 = new Panel2(btn2, panel2, lblSpeed2, txtSpeed2, "Panel no. 2");
p3 = new Panel2(btn3, panel3, lblSpeed3, txtSpeed3, "Panel no. 3");
结果是一样的,但你只有一个类
我要去吃午饭,然后我会更好地向你解释。挂在
答案 1 :(得分:0)
这有点模糊,它将归结为您希望如何与容器上的组件进行交互。
您可以使用抽象类来表示类的基本概念并实现每个单独的案例
您可以构建一个描述每个组件的模型,并允许组件根据模型的要求构建UI。
在这里尝试实现的重要一点是保持适当的责任分离