【发布时间】:2013-12-07 18:24:23
【问题描述】:
我正在用java做一个表格。
想法是:
当用户插入他的名字和第二个名字时,他必须按下“提交”按钮。现在我想用提交按钮收听这个动作,但我不知道如何获取用户在文本字段中输入的内容:“名称”和“第二个名称”。
我想通过提交按钮查看这些数据,但我不知道是否可以。
或者我想知道当用户点击“提交”时,我可以通过哪种方式查看所有这些数据。
谢谢,这是代码。
public class appNegozio extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
appNegozio frame = new appNegozio();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public appNegozio() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
SpringLayout sl_contentPane = new SpringLayout();
contentPane.setLayout(sl_contentPane);
textField = new JTextField();
sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10,
SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62,
SpringLayout.WEST, contentPane);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16,
SpringLayout.SOUTH, textField);
sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0,
SpringLayout.WEST, textField);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNome = new JLabel("Nome");
sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10,
SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7,
SpringLayout.WEST, textField);
contentPane.add(lblNome);
JLabel lblCognome = new JLabel("Cognome");
sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0,
SpringLayout.NORTH, textField_1);
sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6,
SpringLayout.WEST, textField_1);
contentPane.add(lblCognome);
JButton btnSubmit = new JButton("Submit");
sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24,
SpringLayout.SOUTH, textField_1);
sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10,
SpringLayout.WEST, contentPane);
contentPane.add(btnSubmit);
}
}
【问题讨论】:
标签: java forms swing actionlistener jtextfield