gpt4 book ai didi

java - 多个相似面板

转载 作者:行者123 更新时间:2023-12-01 05:04:51 26 4
gpt4 key购买 nike

在我的应用程序中,我将有几个 JPanel(10 个或更多),每个 JPanel 都略有修改(附加按钮、标签等)。

所以我的想法是创建具有通用元素的 Panel 类(3 个按钮、3 个标签和 3 个文本字段 - 它们将位于同一位置的每个面板上,当然具有不同的数据) - 在下面的示例中只有 1 个按钮、1 个标签和 1 个文本字段。

在我的主类中,我想更改标签名称,在文本字段中设置值,就像我手动创建的这个面板中一样:

 p1.btn1.setText("TEST");

我希望你能理解我的解释..谢谢。

正确执行此操作的最佳方法是什么?

enter image description here

到目前为止我所做的事情:

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);

}
}

最后是我的小组类(class):

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
}
}

最佳答案

这有点模糊,这取决于您希望如何与容器上的组件交互。

您可以使用抽象类来表示类的基本概念并实现每个单独的案例

您可以构建一个描述每个组件的模型,并允许组件根据模型的要求构建 UI。

这里要尝试实现的重要事情是保持适当的责任分离

关于java - 多个相似面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com