【问题标题】:JTextField not editing JTextInputJTextField 不编辑 JTextInput
【发布时间】:2015-10-29 17:28:29
【问题描述】:

我正在尝试制作一个简单的程序,其中有一个文本输入字段和一个标签,当您在该字段中输入内容时,它会将输入存储为“testInput”并将标签更改为该文本。我为此使用了 Eclipse 的 Window Builder 插件,它之前运行良好。现在发生的事情是将文本输入,但不会将其发送回 jLabel。这是我的代码:

package interest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestWin {

String testInput;

private JFrame frame;
private JTextField txtTest;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TestWin window = new TestWin();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TestWin() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    frame.getContentPane().setLayout(gridBagLayout);

    txtTest = new JTextField(); 
    //Change label when enter is pressed
    txtTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            testInput = txtTest.getText();
            txtTest.setText(testInput);
        }
    });
    txtTest.setText("test");
    GridBagConstraints gbc_txtTest = new GridBagConstraints();
    gbc_txtTest.insets = new Insets(0, 0, 5, 0);
    gbc_txtTest.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtTest.gridx = 5;
    gbc_txtTest.gridy = 1;
    frame.getContentPane().add(txtTest, gbc_txtTest);
    txtTest.setColumns(10);

    JLabel lblTest = new JLabel("test");
    GridBagConstraints gbc_lblTest = new GridBagConstraints();
    gbc_lblTest.gridx = 5;
    gbc_lblTest.gridy = 4;
    frame.getContentPane().add(lblTest, gbc_lblTest);
}

}

我确定我犯了一个非常简单的错误,因此非常感谢任何帮助!

【问题讨论】:

  • 'txtTest.setText(testInput);'在 'testInput = txtTest.getText();' 之后没有任何意义- 你想写'lblTest.setText(testInput);' ?

标签: java eclipse swing jlabel jtextfield


【解决方案1】:

JLabel lblTest = new JLabel("test");

initialize()(或txtTest.addActionListener...以上的任何地方)的开头,然后替换

txtTest.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        testInput = txtTest.getText();
        txtTest.setText(testInput);
    }
});

txtTest.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        testInput = txtTest.getText();
        lblTest.setText(testInput);    //This line was changed.
    }
});

【讨论】:

  • 如果您建议只替换一行代码,而不是显示两个六行代码块,所有代码行都相同,除了一个,您的答案会更容易理解,这迫使读者玩一场耗时的找茬游戏。
  • 在被替换的行旁边添加了注释。
  • @VGR 当然,但其他行提供上下文并保存回答者使用 OP 玩“我不知道在哪里进行更改”游戏:P - 但是他们之间的比较拥有并且他们应该改变也很有帮助;)
猜你喜欢
  • 1970-01-01
  • 2015-10-12
  • 2017-09-26
  • 2010-11-19
  • 1970-01-01
  • 2014-06-22
  • 2015-10-29
  • 1970-01-01
  • 2015-08-03
相关资源
最近更新 更多