让两个Java Swing窗体相互通信



嗨,我有两个Swing表单。。。InputStudentInfo和InputStudentAddressInfo。InputStudentInfo有一个简单的文本字段,允许用户输入学生的姓名。它还有一个标记为"添加地址"的按钮,单击后会打开InputStudentAddressInfo。此表单有一个文本字段,允许用户输入学生地址。它也有一个保存按钮。我的问题是,当用户单击保存地址按钮时,我需要将为学生地址输入的值传递给InputStudentInfo。这是InputStudentInfo:的代码

/*
* To change this license header, choose License Headers in Project 
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testswing;
public class InputStudentInfo extends javax.swing.JFrame {
/**
* Creates new form InputStudentInfo
*/
public InputStudentInfo() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
saveButton = new javax.swing.JButton();
addAddress = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
saveButton.setText("Save");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
addAddress.setText("Add Address");
addAddress.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addAddressActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(jLabel1)
.addGap(56, 56, 56)
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addAddress)
.addComponent(saveButton)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 
93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(154, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(nameField, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addComponent(addAddress)
.addPreferredGap
(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, 
Short.MAX_VALUE)
.addComponent(saveButton)
.addGap(57, 57, 57))
);
pack();
}// </editor-fold>   
// end of generated code     
StudentDataModel studentdatamodel = new StudentDataModel();
private void addAddressActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
InputStudentAddressInfo addStudentAddressForm = new 
InputStudentAddressInfo(studentdatamodel);
addStudentAddressForm.setSize(800,600);
addStudentAddressForm.setVisible(true);
}                                          
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
// Create a Student 
Student newStudent = new Student();
String name = nameField.getText();
//This is where I want to get the address value from
//InputStudentAddressInfo.java and assign it to address
// I tried creating a InputStudentAddressInfo object and calling the 
// saveAddressButtonActionPerformed method from 
//InputStudentAddressInfo but that doesn't work. 
String address=InputStudentAddressInfo.saveAddressButtonActionPerformed(); 

}                                          

// Variables declaration - do not modify                     
private javax.swing.JButton addAddress;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField nameField;
private javax.swing.JButton saveButton;
// End of variables declaration                   
}

这是InputStudentAddressInfo 的代码

package testswing;
public class InputStudentAddressInfo extends javax.swing.JDialog {
/**
* Creates new form InputStudentAddressInfo
*/
public InputStudentAddressInfo(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}

private void saveAddressButtonActionPerformed(java.awt.event.ActionEvent 
evt) {   
// I need to assign the value entered in addressField to address and 
then pass address to InputStudentInfo.java
String address = addressField.getText();
}                                                 
public JTextField getAddressField() {
return addressField;
}
// Variables declaration - do not modify                     
private javax.swing.JTextField addressField;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton saveAddressButton;
// End of variables declaration                   
}

正如您从上面看到的,我试图从InputStudentInfo调用saveAddressButtonActionPerformed来获取地址的值,但这显然是错误的方法。所以我需要一个返回地址的方法,并且可以从InputStudentInfo调用。如有任何帮助,我们将不胜感激!

基本思想是,您的对话框需要提供一个"getter"方法,该方法返回地址字段的当前值,类似于。。。

public class InputStudentAddressInfo extends javax.swing.JDialog {
//...
public String getAddress() {
return addressField.getText();
}
//...
}

例如。

最困难的部分是知道什么时候可以节省下来以获得价值。您需要知道用户是通过按下保存按钮还是通过标题栏的关闭按钮关闭窗口来关闭对话框。

为此,您可以维护某种标志,该标志可以在对话框关闭后进行检查,以确定对话框是如何关闭的

public class InputStudentAddressInfo extends javax.swing.JDialog {
public enum State {
SAVE, CANCEL
}
private State state;
/**
* Creates new form InputStudentAddressInfo
*/
private InputStudentAddressInfo(java.awt.Frame parent, boolean modal) {
super(parent, modal);
state = State.CANCEL;
initComponents();
}
private void saveAddressButtonActionPerformed(java.awt.event.ActionEvent evt) {
state = State.SAVE;
dispose();
}
public State getState() {
return state;
}
//...
}

因为我太懒了,我可能会添加一个static辅助方法。。。

public class InputStudentAddressInfo extends javax.swing.JDialog {
public static String show(java.awt.Frame parent) {
InputStudentAddressInfo dialog = new InputStudentAddressInfo(parent, true);
return dialog.getState() == State.SAVE ? dialog.getAddress() : null;
}
//...
}

这使得调用对话框和管理输出更加容易。如果结果为null,则用户将取消对话框,否则将返回他们输入的值。。。

String address = InputStudentAddressInfo.show(parent);
if (address != null) {
// You know have the address value
}

当然,如果状态对调用方很重要,您可以更改它,使show方法返回另一个对象中包装的状态和值,但这只是一个简单的示例

最新更新