简体   繁体   English

JButton 文本会自行更改吗?

[英]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:以下是总是在我的设置中重现相同错误的具体步骤:

  1. Run the program.运行程序。
  2. Resize the frame to be a bit bigger than it was.将框架调整为比原来大一点。
  3. Type some short text in the text-field.在文本字段中键入一些短文本。
  4. Press Tab to navigate the focus to the button.Tab将焦点导航到按钮。
  5. Press Spacebar to invoke the button.空格键调用按钮。
  6. Press Tab to navigate the focus back to the text-field.Tab将焦点导航回文本字段。
  7. Type any letter you like into the text-field.在文本字段中输入您喜欢的任何字母。

Notes:笔记:

  • I know that step 2 is relevant, because if I ommit it then the bug does not reproduce.我知道第 2 步是相关的,因为如果我忽略它,那么错误就不会重现。
  • After step 2 (and before 3), the mouse should not be needed any more.在第 2 步之后(和第 3 步之前),应该不再需要鼠标了。 Leave it.别管它。 The focus of the program should be in the text-field as it was when the program launched.程序的焦点应该在程序启动时的文本字段中。
  • In step 3 I usually type something like abc but the error repoduces for any other input I tried.在第 3 步中,我通常会键入类似abc的内容,但对于我尝试的任何其他输入,错误都会重现。
  • In step 6 you can also use Shift + Tab to navigate back to the text-field.在第 6 步中,您还可以使用Shift + Tab导航回文本字段。
  • On step 7, after typing the first letter, you will see that the button's text changes back to its initial/previous value.在第 7 步,输入第一个字母后,您将看到按钮的文本变回其初始/先前值。

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?首先,其他人能否重现我在他们的设置中遇到的错误?

Update:更新:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM