Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将按钮从一个面板类连接到另一个面板类的数据_Java_Swing - Fatal编程技术网

Java 将按钮从一个面板类连接到另一个面板类的数据

Java 将按钮从一个面板类连接到另一个面板类的数据,java,swing,Java,Swing,首先,大家好,谢谢你们的帮助。我在使用特定的类设置创建带有JavaSwing的简单程序时遇到问题。我可以用一个更简单的设置让它工作,但对于我的作业来说,它需要这样做。对代码进行以下操作: 这是包含类实例的基本jframe。非常简单,这里不多 public class MainFrame extends JFrame { private FormPanel fPanel; DonjiBotuniPanel dbtnPanel; public MainFrame() { su

首先,大家好,谢谢你们的帮助。我在使用特定的类设置创建带有JavaSwing的简单程序时遇到问题。我可以用一个更简单的设置让它工作,但对于我的作业来说,它需要这样做。对代码进行以下操作:

这是包含类实例的基本jframe。非常简单,这里不多

public class MainFrame extends JFrame {

private FormPanel   fPanel;
DonjiBotuniPanel    dbtnPanel;

public MainFrame() {

    super("Enrollment app");
    setLayout(new BorderLayout());
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(400, 300);
    setMinimumSize(new Dimension(400, 300));
    // setResizable(false);
    createComposition();
    add(fPanel, BorderLayout.CENTER);
    add(dbtnPanel, BorderLayout.SOUTH);

}

private void createComposition() {
    fPanel = new FormPanel();
    dbtnPanel = new DonjiBotuniPanel();

}

}
然后我们有了第一个panel类,其中包含一些布局和组件创建方法,以及一些字段数据的getter:

public class FormPanel extends JPanel {

private JLabel                          fNameLbl;
private JLabel                          lNameLbl;
private JLabel                          depLbl;
private JLabel                          idLbl;
private JTextField                      fNameTxt;
private JTextField                      lNameTxt;
private JTextField                      idTxt;
private JComboBox<String>               depCombo;
private JRadioButton                    croCitzn;
private JRadioButton                    forCitzn;
private DefaultComboBoxModel<String>    dcBoxModel;
private ButtonGroup                     radGrp;

public FormPanel() {

    setLayout(new GridBagLayout());
    createComposition();
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.insets = new Insets(5, 5, 5, 5);
    // gbc.weighty = 1;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    add(fNameLbl, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    add(lNameLbl, gbc);

    gbc.gridx = 2;
    gbc.gridy = 0;
    add(depLbl, gbc);

    // gbc.weighty = 0;
    gbc.gridx = 0;
    gbc.gridy = 1;
    add(fNameTxt, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    add(lNameTxt, gbc);

    gbc.gridx = 2;
    gbc.gridy = 1;
    add(depCombo, gbc);

    gbc.insets = new Insets(20, 0, 0, 0);
    gbc.gridx = 0;
    gbc.gridy = 3;
    add(idLbl, gbc);

    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 4;
    add(idTxt, gbc);

    gbc.gridx = 1;
    gbc.gridy = 4;
    add(croCitzn, gbc);

    gbc.gridx = 1;
    gbc.gridy = 5;
    add(forCitzn, gbc);
    setBorders();

}

private void setBorders() {

    Border inner = BorderFactory.createTitledBorder("user info:");
    Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);

    setBorder(BorderFactory.createCompoundBorder(outer, inner));
}

private void createComposition() {
    fNameLbl = new JLabel("First name: ");
    fNameTxt = new JTextField(5);
    lNameLbl = new JLabel("Last name: ");
    lNameTxt = new JTextField(5);
    depLbl = new JLabel("Department");
    idLbl = new JLabel("Unique id: ");
    idTxt = new JTextField(5);
    createComboModel();
    depCombo = new JComboBox<>(dcBoxModel);
    radGrp = new ButtonGroup();
    croCitzn = new JRadioButton("CRO citizenship");
    forCitzn = new JRadioButton("Foreign citizenship");
    radGrp.add(croCitzn);
    radGrp.add(forCitzn);
    croCitzn.setSelected(true);

}

private void createComboModel() {
    dcBoxModel = new DefaultComboBoxModel<>();
    dcBoxModel.addElement("Informatika");
    dcBoxModel.addElement("Kultura");
    dcBoxModel.addElement("Turizam");
    dcBoxModel.addElement("Filozofija");
    dcBoxModel.addElement("Jezik");
}



public String getfNameTxt() {
    return fNameTxt.getText();
}

public String getlNameTxt() {
    return lNameTxt.getText();
}

public String getIdTxt() {
    return idTxt.getText();
}


}
现在麻烦开始了。我无法将DonjiBotuniPanel中的按钮与FormPanel中的数据连接起来。我知道我可以在一个java文件中使用内部/外部类来设置不同的类,并且我能够以这种方式工作。但我不能这样做。我知道他们之间需要有某种联系,也许是某种控制器,我尝试了很多不同的方法,但我似乎不太明白

因此,如果您不想通读所有代码(大部分代码只是组件创建和布局),请简化。我有三个java文件中的三个类。一个是大型机,我在其中创建了第二个两个类的实例——FormPanel,它在自己的java文件中包含所有JTextFields(数据)和DonjiBotuniPanel,它有三个按钮。由于我无法在DonjiBotuniPanel中创建FormPanel(数据)的新实例,因此我无法使用DonjiBotuniPanel中的按钮从FormPanel提取数据进行操作


非常感谢您的帮助。

只需在大型机中创建一个新类,例如DataTransferClass和init。 DonjiBotuniPanel和FormPanel将此对象作为其构造函数参数。在DataTransferClass中添加必要的数据字段,并在FormPanel中设置这些字段的值,然后在DonjiBotuniPanel中获取它们。在大型机类createComposition方法中进行如下更改:

大型机:

private void createComposition() {
    dataTransferClass = new DataTransferClass();
    fPanel = new FormPanel(dataTransferClass);
    dbtnPanel = new DonjiBotuniPanel(dataTransferClass);
}

在其他课程中进行必要的更改。

谢谢,我一拿到HOMEK就会这样做,问题如下。启动程序时,字段为空。如果我在FormPanel中使用新DataTransferClass中的setter,它将在运行程序开始时完成。带有按钮的DonjiBotuniPanel具有ActionListener。同样,我需要这个按钮来设置DataTransferClass中的值和FormPanel中的数据,我在DonjiBotuniPanel中找不到这些数据。但是你让我走上了正确的道路!非常感谢!我使用fPanel作为DonjiBotuniPanel构造函数参数,并从那里开始。现在我希望它能正常工作。不客气@Ink22。如果有助于解决您的问题,您可以接受答案;)
private void createComposition() {
    dataTransferClass = new DataTransferClass();
    fPanel = new FormPanel(dataTransferClass);
    dbtnPanel = new DonjiBotuniPanel(dataTransferClass);
}