小编Vig*_*ant的帖子

Swing getText不会读取用户输入

我一直在努力让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 = …
Run Code Online (Sandbox Code Playgroud)

java swing

2
推荐指数
1
解决办法
86
查看次数

标签 统计

java ×1

swing ×1