java 如何从 JFrame 表单获取输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15392699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the input from a JFrame form?
提问by MrSnare
I am trying to get the input that I submit in a JFrame form and store it in an arraylist. I am not sure how to get what input i place in the text field when my ActionEvent occurs. Can anyone tell me how to do it?
我正在尝试获取我在 JFrame 表单中提交的输入并将其存储在一个数组列表中。当我的 ActionEvent 发生时,我不确定如何获取我在文本字段中放置的输入。谁能告诉我怎么做?
my form currently looks like
我的表格目前看起来像
Here is my code:
这是我的代码:
public class SpringDemo {
private static void createAndShowGUI() {
final String[] labels = {"Bill: ", "Last Top Up Date: ", "Same Network? "};
int labelsLength = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < labelsLength; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
l.setLabelFor(textField);
p.add(textField);
}
JButton button = new JButton("Submit");
p.add(new JLabel());
p.add(button);
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
labelsLength + 1, 2, //rows, cols
7, 7, //initX, initY
7, 7); //xPad, yPad
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
System.out.println("Test");
}
});
//Create and set up the window.
JFrame frame = new JFrame("SpringForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
p.setOpaque(true); //content panes must be opaque
frame.setContentPane(p);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
回答by Vishal K
To do this your createAndShowGUI
method should be like this:
要做到这一点,你的createAndShowGUI
方法应该是这样的:
private static void createAndShowGUI() {
final String[] labels = {"Bill: ", "Last Top Up Date: ", "Same Network? "};
int labelsLength = labels.length;
final JTextField[] textField = new JTextField[labels.length];
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < labelsLength; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
textField[i] = new JTextField(10);
l.setLabelFor(textField[i]);
p.add(textField[i]);
}
JButton button = new JButton("Submit");
p.add(new JLabel());
p.add(button);
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
labelsLength + 1, 2, //rows, cols
7, 7, //initX, initY
7, 7); //xPad, yPad
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
for (int i = 0 ; i < labels.length ; i++)
{
System.out.println(labels[i]+"->"+textField[i].getText());
}
}
});
//Create and set up the window.
JFrame frame = new JFrame("SpringForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
p.setOpaque(true); //content panes must be opaque
frame.setContentPane(p);
//Display the window.
frame.pack();
frame.setVisible(true);
}
回答by Gilbert Le Blanc
Keep the JTextField
s in a List, and refer to them in your action listener.
将JTextField
s保留在列表中,并在您的动作侦听器中引用它们。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class SpringDemo {
private static void createAndShowGUI() {
final String[] labels = { "Bill: ", "Last Top Up Date: ",
"Same Network? " };
int labelsLength = labels.length;
final List<JTextField> textFields = new ArrayList<JTextField>();
// Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < labelsLength; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
textFields.add(textField);
l.setLabelFor(textField);
p.add(textField);
}
JButton button = new JButton("Submit");
p.add(new JLabel());
p.add(button);
// Lay out the panel.
SpringUtilities.makeCompactGrid(p, labelsLength + 1, 2, // rows, cols
7, 7, // initX, initY
7, 7); // xPad, yPad
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Execute when button is pressed
System.out.println("Test");
for (JTextField jTextField : textFields) {
String s = jTextField.getText();
}
}
});
// Create and set up the window.
JFrame frame = new JFrame("SpringForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
p.setOpaque(true); // content panes must be opaque
frame.setContentPane(p);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
回答by 3kings
You would just use
你只会用
textField.getText();
unless im wrong.
除非我错了。
Honestly though that might not work for each and everyone i would just make them as seperate textFields not creating a new one through a for loop
老实说,虽然这可能对每个人都不起作用,但我只会将它们作为单独的 textFields 而不是通过 for 循环创建一个新的
JTextField a = new JTextField();
and so on.....