您好我有两个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调用。任何帮助将不胜感激!
答案 0 :(得分:1)
基本思想是,你的对话框需要提供一个“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
方法返回包含在另一个对象中的状态和值,如果状态对调用者很重要,但这只是一个简单的示例