我在做一個java表單。Java Swing:帶有文本字段的表單
的理念是:
當用戶插入他的名字,第二個名字,他必須按「提交」按鈕。現在我想用submit按鈕來聽這個動作,但我不知道如何讓用戶在文本字段中輸入:「name」和「second name」。
我想用提交按鈕看到這些數據,但我不知道,如果可能的話。
或者我想知道當用戶點擊「提交」時,我可以看到所有這些數據。
謝謝,這是代碼。
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);
}
}
太感謝你了 –