在我的应用程序中,我将有几个 JPanel(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
}
}