我一直在尝试让 GUI 适合我正在开发的应用程序,而且,自从在 Swing 中做任何事情以来已经有很长时间了。大多数东西都回来了,但这个没有。我无法理解为什么,在任何不同的文本组件中,如果我在其中输入内容, getText() 将始终返回“”。如果我使用 setText() 作为测试,它会正确返回,但这并没有多大帮助。
这在 JTextArea/JTextField、引用文本字段的各种方式(直接变量与从 HashMap 中提取)之间保持一致,并且无论访问哪种线程。有时,我使用调试器尝试查看通过其他测试片段发生的事情,但仍然一无所获。自编辑以来,已经删除了很多此类情况,但问题仍然存在。
在所有情况下,都不会获取用户输入。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LifePathUI2 {
final static boolean shouldWeightX = true;
private GridBagConstraints cons;
private BorderLayout windowLayout;
private GridBagLayout layout;
private JFrame mainWindow;
private JPanel mainPanel;
private JComponent currentComponent;
private JTextField characterName;
/**
* @throws HeadlessException
*/
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
init();
}
public void init()
{
mainWindow.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mainWindow.setLayout(windowLayout);
cons.ipadx = 5;
cons.ipady = 5;
cons.anchor = GridBagConstraints.NORTHWEST;
cons.fill = GridBagConstraints.NONE;
cons.weighty = 1.0;
cons.weightx = 1.0;
// to make everything work right we add a mainPanel under the mainWindow
cons.gridheight = 1;
cons.gridwidth = 1;
mainWindow.add(mainPanel);
// Readers keep in mind if you don't set this below, strange behavior happens
mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mainPanel.setLayout(layout);
currentComponent = mainPanel;
addLabel(0,0,"Character Name");
characterName = addTextF(1,0,"",30,true);
endRow(2,0);
addButton(0,1,"Foo").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println(characterName.getText());
}
});
endVertical(0,2);
mainWindow.setSize(1400, 900);
mainWindow.setVisible(true);
}
/**
* Inserts a spacer to signify the end of components for this row (effects layout so it doesn't center)
*/
private void endRow(int x, int y)
{
cons.weightx = 100.0;
addLabel(x,y,"");
cons.weightx = 1.0;
}
/**
* Inserts a spacer to signify the end of components vertically (effects layout so it doesn't center)
*/
private void endVertical(int x, int y)
{
cons.weighty = 100.0;
addLabel(x,y,"");
cons.weighty = 1.0;
}
/**
* Shorthand command to add label at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param text Display Text for label
*/
private JLabel addLabel(int x, int y, String text)
{
JLabel temp = new JLabel(text);
addC(temp,x,y);
return temp;
}
/**
* Shorthand command to add Button at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param text Display Text for Button
* @return The component created
*/
private JButton addButton(int x, int y, String text)
{
JButton temp = new JButton(text);
addC(temp,x,y);
return temp;
}
/**
* Shorthand command to add Text Field at coordinates with text
* @param x non-negative integer
* @param y non-negative integer
* @param value the default value for the text field
* @param cols Number of Columns
* @param updateListen If true will add listener that triggers UI updates when values change
* @return The component created
*/
private JTextField addTextF(int x, int y, String value, int cols, boolean updateListen)
{
JTextField temp = new JTextField(value, cols);
// this prevents the common issue of the text fields turning into slits
temp.setMinimumSize(temp.getPreferredSize());
addC(temp,x,y);
return temp;
}
/**
* Shorthand to add new components to the UI tab at given coords
* @param comp Component to add to UI
* @param x non-negative integer
* @param y non-negative integer
*
*/
private void addC(JComponent comp, int x, int y) {
cons.gridx = x;
cons.gridy = y;
currentComponent.add(comp,cons);
}
/**
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
LifePathUI2 ui = new LifePathUI2();
ui.init();
}
});
}
}
我在这里搜索了每个问题以查找有关 Swing 的内容,但没有一个问题似乎与这个问题具体对应。并不完全期待这一点。
我对更大的片段表示歉意,但由于所有部分都立即围绕创建文本字段、读取它们、创建 GUI 本身,所有这些似乎都与我在网上看到的示例相匹配......我只是足够聪明要知道我在这里一定做了一些奇怪的事情,只是没有看到它。
编辑:简化示例。尽管将其分解为一个更简单的测试用例,但仍然没有骰子 EDIT2:完成了事物的压缩。我实际上期望它能够工作,因为这是我之前编写的工作代码的级别。但还是什么都没有
最佳答案
一个疯狂的猜测,但你的 update()
方法看起来可能正在尝试从它正在监听的文档中提取文本(你没有说),如果是,则使用 DocumentEvent 获取 Document,然后提取文本。像这样的东西:
private class TextChangeListener implements DocumentListener {
@Override
public void changedUpdate(DocumentEvent e) {
update(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
update(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
update(e);
}
}
和
private void update(DocumentEvent e) {
String text;
try {
Document doc = e.getDocument();
text = e.getDocument().getText(0, doc.getLength());
// do something with text here! ************
System.out.println(text);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
除此之外,我迷失在你的代码中。简化。重构。再简化一些。重构一些。考虑可以单独进行全面测试的较小类。
编辑
你调用了init()
两次,这就是问题所在,因为这意味着你创建了两个东西,一个显示,一个不显示.
您首先在这里调用它:
public LifePathUI2() throws HeadlessException {
// ....
init();
}
然后在你的 main 中再次调用它:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
LifePathUI2 ui = new LifePathUI2();
ui.init();
}
});
仅调用一次。
更改此:
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
init();
}
对此:
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// init();
}
关于java - Swing getText 不会读取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363119/