使用方法添加 JTextField 时从 JTextField 检索文本
Retrieving text from a JTextField when using a method to add the JTextField
我目前正在学习如何在 Java 中使用 Swing。将 JTextFields、JLabels 或 JButtons 添加到 JPanel 时,我通常在 4 行中这样做:
gbc.gridx = 0;
gbc.gridy = 0;
JTextField example = new JTextField("", 6);
p.add(example, gbc);
但是我试图通过制作这样的方法来压缩我的代码:
public static void addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
}
并在主方法中调用 addTextField(),例如:
ClassName.addTextField(p2, gbc, "", 1, 1, 6);
我遇到的问题是如何检索 JTextField 中的文本。像,
String str = example.getText();
或
String str2 = tempTF.getText();
通常能用的,现在不能用了。
关于如何解决这个问题的任何想法?
这是我在回答之前使用的完整代码:
/**
* This program creates two windows.
* Window #1 displays Hello World
* WIndow #2 prompts users to input information
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessingWithSwing extends JFrame{
//Main Method
public static void main(String[] args) {
//makes the first window
MessingWithSwing window1 = new MessingWithSwing("Window #1", 300, 300);
//makes the second window with minimum dimensions
MessingWithSwing window2 = new MessingWithSwing("Window #2", 450, 300, 300, 300);
//GridBagConstraints allows the objects in each panel to be positioned in grid cordinates
GridBagConstraints gbc = new GridBagConstraints();
//sets the spaces between each grid position
gbc.insets = new Insets(10, 10, 10, 10);
//creates a JPanel that uses a grid layout
JPanel p1 = new JPanel(new GridBagLayout());
//adds the panel in the center of window1
window1.add(p1, BorderLayout.CENTER);
//sets the panel visible so it can correctly display the labels, textfields, and buttons
p1.setVisible(false);
//uses the method created below to add JLabels to p1
MessingWithSwing.addLabel(p1, gbc, "Hello", 0, 0);
MessingWithSwing.addLabel(p1, gbc, "World", 0, 1);
p1.setVisible(true);
//creates a second JPanel that also uses a grid layout
JPanel p2 = new JPanel(new GridBagLayout());
//adds the panel at the top of window2
window2.add(p2, BorderLayout.NORTH);
//sets the panel visible so it can correctly display the labels, textfields, and buttons
p2.setVisible(false);
//uses the method created below to add JLabels to p2
MessingWithSwing.addLabel(p2, gbc, "Please Enter The Following", 0, 0);
MessingWithSwing.addLabel(p2, gbc, "Name: ", 0, 1);
MessingWithSwing.addLabel(p2, gbc, "Age: ", 0, 2);
//uses the method created below to add JTextFields to p2
MessingWithSwing.addTextField(p2, gbc, "", 1, 1, 6);
MessingWithSwing.addTextField(p2, gbc, "<age>", 1, 2, 6);
//uses the method created below to add a JButton to p2
MessingWithSwing.addButton(p2, gbc, "Enter", 0, 3);
p2.setVisible(true);
/* Issue:
* The someting like the line below would not work
* How would you access the text inputted into each JTextField?
*/
// String str = tempTF.getText();
} //end of Main Method
//first constructor makes a window that is completely resizable
public MessingWithSwing(String title, int sizeX, int sizeY) {
super(title);
setSize(sizeX, sizeY);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//second constructor makes a window that is resizable but has minimum dimensions
public MessingWithSwing(String title, int sizeX, int sizeY, int minSizeX, int minSizeY) {
super(title);
setSize(sizeX, sizeY);
setMinimumSize(new Dimension(minSizeX, minSizeY));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//This method adds a JLabel with an indicated text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addLabel(JPanel p, GridBagConstraints gbc, String str, int x, int y) {
gbc.gridx = x;
gbc.gridy = y;
JLabel tempLabel = new JLabel(str);
p.add(tempLabel, gbc);
}
//This method adds a JTextField with an indicated length and text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
}
//This method adds a JButton with an indicated text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addButton (JPanel p, GridBagConstraints gbc, String str, int x, int y) {
gbc.gridx = x;
gbc.gridy = y;
JButton tempB = new JButton(str);
p.add(tempB, gbc);
}
}
所以,如果我正确理解 Kequiang Li,它应该是这样的:
public static JTextField addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
return tempTF;
}
然后:
ArrayList<JTextField> tfList = new ArrayList<JTextField>();
tfList.add(ClassName.addTextField(p1,gbc,text,0,0,8));
String str = tfList.get(0).getText();
如何在文本字段中获取文本完全取决于您想要获取文本的位置和时间。
假设您想随时检索文本,那么您可以将 addTextField
returns 添加到文本字段中。这样,您可以跟踪列表、数组甚至地图中的所有文本字段,只需对这些文本字段调用 getText
。
在另一种情况下,您可能希望仅当用户在文本字段中按下回车键时才需要文本,然后您可以将侦听器绑定到文本字段。当事件发生时,您可以访问事件的来源,即文本字段,然后检索文本。
我目前正在学习如何在 Java 中使用 Swing。将 JTextFields、JLabels 或 JButtons 添加到 JPanel 时,我通常在 4 行中这样做:
gbc.gridx = 0;
gbc.gridy = 0;
JTextField example = new JTextField("", 6);
p.add(example, gbc);
但是我试图通过制作这样的方法来压缩我的代码:
public static void addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
}
并在主方法中调用 addTextField(),例如:
ClassName.addTextField(p2, gbc, "", 1, 1, 6);
我遇到的问题是如何检索 JTextField 中的文本。像,
String str = example.getText();
或
String str2 = tempTF.getText();
通常能用的,现在不能用了。
关于如何解决这个问题的任何想法?
这是我在回答之前使用的完整代码:
/**
* This program creates two windows.
* Window #1 displays Hello World
* WIndow #2 prompts users to input information
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessingWithSwing extends JFrame{
//Main Method
public static void main(String[] args) {
//makes the first window
MessingWithSwing window1 = new MessingWithSwing("Window #1", 300, 300);
//makes the second window with minimum dimensions
MessingWithSwing window2 = new MessingWithSwing("Window #2", 450, 300, 300, 300);
//GridBagConstraints allows the objects in each panel to be positioned in grid cordinates
GridBagConstraints gbc = new GridBagConstraints();
//sets the spaces between each grid position
gbc.insets = new Insets(10, 10, 10, 10);
//creates a JPanel that uses a grid layout
JPanel p1 = new JPanel(new GridBagLayout());
//adds the panel in the center of window1
window1.add(p1, BorderLayout.CENTER);
//sets the panel visible so it can correctly display the labels, textfields, and buttons
p1.setVisible(false);
//uses the method created below to add JLabels to p1
MessingWithSwing.addLabel(p1, gbc, "Hello", 0, 0);
MessingWithSwing.addLabel(p1, gbc, "World", 0, 1);
p1.setVisible(true);
//creates a second JPanel that also uses a grid layout
JPanel p2 = new JPanel(new GridBagLayout());
//adds the panel at the top of window2
window2.add(p2, BorderLayout.NORTH);
//sets the panel visible so it can correctly display the labels, textfields, and buttons
p2.setVisible(false);
//uses the method created below to add JLabels to p2
MessingWithSwing.addLabel(p2, gbc, "Please Enter The Following", 0, 0);
MessingWithSwing.addLabel(p2, gbc, "Name: ", 0, 1);
MessingWithSwing.addLabel(p2, gbc, "Age: ", 0, 2);
//uses the method created below to add JTextFields to p2
MessingWithSwing.addTextField(p2, gbc, "", 1, 1, 6);
MessingWithSwing.addTextField(p2, gbc, "<age>", 1, 2, 6);
//uses the method created below to add a JButton to p2
MessingWithSwing.addButton(p2, gbc, "Enter", 0, 3);
p2.setVisible(true);
/* Issue:
* The someting like the line below would not work
* How would you access the text inputted into each JTextField?
*/
// String str = tempTF.getText();
} //end of Main Method
//first constructor makes a window that is completely resizable
public MessingWithSwing(String title, int sizeX, int sizeY) {
super(title);
setSize(sizeX, sizeY);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//second constructor makes a window that is resizable but has minimum dimensions
public MessingWithSwing(String title, int sizeX, int sizeY, int minSizeX, int minSizeY) {
super(title);
setSize(sizeX, sizeY);
setMinimumSize(new Dimension(minSizeX, minSizeY));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//This method adds a JLabel with an indicated text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addLabel(JPanel p, GridBagConstraints gbc, String str, int x, int y) {
gbc.gridx = x;
gbc.gridy = y;
JLabel tempLabel = new JLabel(str);
p.add(tempLabel, gbc);
}
//This method adds a JTextField with an indicated length and text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
}
//This method adds a JButton with an indicated text, at an indicated GridBagConstraints location, to an indicated JPanel
public static void addButton (JPanel p, GridBagConstraints gbc, String str, int x, int y) {
gbc.gridx = x;
gbc.gridy = y;
JButton tempB = new JButton(str);
p.add(tempB, gbc);
}
}
所以,如果我正确理解 Kequiang Li,它应该是这样的:
public static JTextField addTextField (JPanel p, GridBagConstraints gbc, String str, int x, int y, int len) {
gbc.gridx = x;
gbc.gridy = y;
JTextField tempTF = new JTextField(str, len);
p.add(tempTF, gbc);
return tempTF;
}
然后:
ArrayList<JTextField> tfList = new ArrayList<JTextField>();
tfList.add(ClassName.addTextField(p1,gbc,text,0,0,8));
String str = tfList.get(0).getText();
如何在文本字段中获取文本完全取决于您想要获取文本的位置和时间。
假设您想随时检索文本,那么您可以将 addTextField
returns 添加到文本字段中。这样,您可以跟踪列表、数组甚至地图中的所有文本字段,只需对这些文本字段调用 getText
。
在另一种情况下,您可能希望仅当用户在文本字段中按下回车键时才需要文本,然后您可以将侦听器绑定到文本字段。当事件发生时,您可以访问事件的来源,即文本字段,然后检索文本。