1

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

2 回答 2

0

你所做的不是太好。您拥有处理对象的强大而强大的语言,让我们尝试专注于如何使用它们。

您只需使用相同的构造函数和相同的结构编写了 3 个“不同”类。

为什么?

您可以使用 3 个不同的面板,只需创建 3 个不同的实例:例如,我更改了您的以下代码:

p2 = new Panel2(btn2, panel2, lblSpeed2, txtSpeed2, "Panel no. 2");
p3 = new Panel2(btn3, panel3, lblSpeed3, txtSpeed3, "Panel no. 3");

结果是一样的,但是你只有一个类

我要去吃午饭了,那我给你解释清楚。不挂断

于 2012-10-13T11:37:30.443 回答
0

这有点模糊,这将归结为您希望如何与容器上的组件进行交互。

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

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

在这里尝试实现的重要一点是保持适当的责任分离

于 2012-10-13T11:42:37.600 回答