[英]Java Swing: form with text fields
I'm doing a form in java. 我在java中做一个表单。
The idea is: 这个想法是:
When an user insert his name and second name he have to press the "submit" button. 当用户插入他的名字和第二个名字时,他必须按下“提交”按钮。 Now I would like to listen to this action with the submit button but I don't know how to get what the user put in the text fields: "name" and "second name".
现在我想用提交按钮听这个动作,但我不知道如何得到用户在文本字段中输入的内容:“name”和“second name”。
I would like to see this data just with the submit button, but i don't know, if it's possible. 我想看看这个数据只是提交按钮,但我不知道,如果可能的话。
Or I would like to know in which way I can see all this data when the user clicks on "submit". 或者我想知道当用户点击“提交”时我可以通过哪种方式查看所有这些数据。
thanks, this is the code. 谢谢,这是代码。
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);
}
}
An easy approach would be to use a final
local variable for each JTextField
. 一种简单的方法是为每个
JTextField
使用final
局部变量。 You can access them from within an ActionListener
added to your JButton
: 您可以从添加到
JButton
的ActionListener
访问它们:
final JTextField textField = new JTextField();
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
System.out.println(textField.getText());
}
});
Don't extend a JFrame, you are not adding any new functionality to the frame. 不要扩展JFrame,不要向框架添加任何新功能。
Read the section from the Swing tutorial on How to Use Text Fields for a working example that will show you a better way to structure your program so that you have access to the data in the text fields from your ActionListener. 阅读Swing教程中有关如何使用文本字段的部分, 以获取一个工作示例,该示例将向您展示构建程序的更好方法,以便您可以访问ActionListener的文本字段中的数据。
The Swing tutorial has examples for all Swing components. Swing教程提供了所有Swing组件的示例。
use JTextField object.getText() and store it locally in a String in Event function of the button.Use that local String for reference. 使用JTextField object.getText()并将其本地存储在按钮的Event函数中的String中。使用该本地String进行引用。
Example 例
String name=jtextfield.getText();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.