gpt4 book ai didi

java - JTextField 不编辑 JTextInput

转载 作者:行者123 更新时间:2023-12-02 04:14:57 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的程序,其中有一个文本输入字段和一个标签,当您在该字段中输入某些内容时,它将输入存储为“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);
}

}

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

最佳答案

放置

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.
}
});

关于java - JTextField 不编辑 JTextInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33420888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com