[英]JButton text changes on its own?
The following simple code:下面是简单的代码:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ButtonTextMain {
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final JTextField field = new JTextField(20);
final JButton button = new JButton("Click to change text");
button.addActionListener(e -> button.setText(field.getText()));
final JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
can always reproduce the same bug (at least for my setups, which are given below).总是可以重现相同的错误(至少对于我的设置,如下所示)。
The program is supposed to change the button text, according to the text-field's text, when the button is clicked.单击按钮时,程序应该根据文本字段的文本更改按钮文本。
The problem is that the text of the button reverts/changes back to its previous value on its own unexpectedly.问题是按钮的文本意外地自行恢复/更改回其先前的值。
The problem arises when I use the Tab to alternate/navigate between the button and the text-field.当我使用Tab在按钮和文本字段之间交替/导航时,问题就出现了。 Here are the specific steps which always reproduce the same bug on my setups:以下是总是在我的设置中重现相同错误的具体步骤:
Notes:笔记:
abc
but the error repoduces for any other input I tried.在第 3 步中,我通常会键入类似abc
的内容,但对于我尝试的任何其他输入,错误都会重现。My first tested setup:我的第一个测试设置:
Apache NetBeans IDE 8.2, which is a bit outdated. Apache NetBeans IDE 8.2,有点过时了。
java -version
yields: java -version
产量:
java version "1.8.0_321" Java(TM) SE Runtime Environment (build 1.8.0_321-b07) Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode) java 版本“1.8.0_321”Java(TM) SE 运行时环境(build 1.8.0_321-b07) Java HotSpot(TM) Client VM(build 25.321-b07,混合模式)
javac -version
yields javac 1.8.0_161
. javac -version
产生javac 1.8.0_161
。 There is a missmatch here with the runtime environment.这里与运行时环境不匹配。
My second tested setup:我的第二个测试设置:
Apache NetBeans IDE 11.0. Apache NetBeans IDE 11.0.
java -version
yields: java -version
产量:
java version "12.0.2" 2019-07-16 Java(TM) SE Runtime Environment (build 12.0.2+10) Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing) java 版本“12.0.2” 2019-07-16 Java(TM) SE 运行时环境(build 12.0.2+10) Java HotSpot(TM) 64 位服务器 VM(build 12.0.2+10,混合模式,共享)
javac -version
yields javac 12.0.2
. javac -version
产生javac 12.0.2
。
The operating system is Windows 10 on both setups.两种设置上的操作系统均为 Windows 10。
So the question is: did I do something wrong, and if so, what is it please?所以问题是:我做错了什么吗?如果是,请问是什么? First of, can anybody else reproduce the bug I am getting on their setup?首先,其他人能否重现我在他们的设置中遇到的错误?
I can also reproduce this behaviour on the system L&F too, by using:我也可以在系统 L&F 上重现此行为,方法是:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
inside just before the JTextField
creation (and catching exceptions, etc).在JTextField
创建之前的内部(并捕获异常等)。
Based on the above comments I retested and now I am able to reproduce the problem.根据以上评论,我重新测试了,现在我能够重现该问题。 The issue occurs when you increase the vertical height of the text field (by some minimal amount).当您增加文本字段的垂直高度(增加一些最小值)时会出现此问题。
I guess I tested before by only increasing the horizontal size of the text field.我想我之前只是通过增加文本字段的水平大小进行了测试。
I found a simple work around:我找到了一个简单的解决方法:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ButtonTextMain {
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final JTextField field = new JTextField(20);
final JButton button = new JButton("Click to change text");
button.addActionListener(e ->
{
button.setText(field.getText());
//button.getParent().revalidate();
button.getParent().repaint();
});
final JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
This definitely seems like a bug to me.这对我来说绝对是个错误。
Having said that, rarely would you want the vertical height of the text field to increase in size.话虽如此,您很少希望文本字段的垂直高度增加。 So maybe you can find a layout manager that only affects the horizontal size and not the vertical height?那么也许您可以找到一个只影响水平尺寸而不影响垂直高度的布局管理器?
I don't have this problem.我没有这个问题。 I use VScode, java version 1.8.0.25.我使用 VScode,java 版本 1.8.0.25。 I think problem is your compiler.我认为问题是你的编译器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.