JButton 文本会自行更改吗?

发布于 2025-01-12 02:08:20 字数 2713 浏览 3 评论 0原文

下面的简单代码:

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

总是可以重现相同的错误(至少对于我的设置,如下所示)。

当单击按钮时,程序应该根据文本字段的文本更改按钮文本。

问题是按钮的文本意外地自行恢复/更改回之前的值。

当我使用 Tab 在按钮和文本字段之间交替/导航时,就会出现问题。以下是在我的设置中总是重现相同错误的具体步骤:

  1. 运行程序。
  2. 将框架调整为比原来大一点。
  3. 在文本字段中输入一些短文本。
  4. Tab 将焦点导航到该按钮。
  5. 空格键调用该按钮。
  6. Tab 将焦点导航回文本字段。
  7. 在文本字段中输入您喜欢的任何字母。

注意:

  • 我知道步骤 2 是相关的,因为如果我省略它,则错误不会重现。
  • 在步骤 2 之后(和步骤 3 之前),应该不再需要鼠标了。留下它。程序的焦点应该位于文本字段,就像程序启动时一样。
  • 在步骤 3 中,我通常会输入类似 abc 的内容,但对于我尝试过的任何其他输入,错误都会重新出现。
  • 在步骤 6 中,您还可以使用 Shift+Tab 导航回文本字段。
  • 在步骤 7 中,输入第一个字母后,您将看到按钮的文本变回其初始/之前的值。

我的第一个测试设置:

  • Apache NetBeans IDE 8.2,有点过时了。

  • java -version 产量:

    java版本“1.8.0_321”
    Java(TM) SE 运行时环境(内部版本 1.8.0_321-b07)
    Java HotSpot(TM) 客户端 VM(内部版本 25.321-b07,混合模式)
    
    
  • javac -version 生成 javac 1.8.0_161。这里与运行时环境不匹配。

我的第二个测试设置:

  • Apache NetBeans IDE 11.0。

  • java -version 产量:

    java版本“12.0.2”2019-07-16
    Java(TM) SE 运行时环境(版本 12.0.2+10)
    Java HotSpot(TM) 64 位服务器 VM(构建 12.0.2+10,混合模式,共享)
    
    
  • javac -version 产生 javac 12.0.2

两种设置的操作系统均为 Windows 10。

所以问题是:我做错了什么吗?如果是,请问是什么?首先,其他人可以重现我在他们的设置中遇到的错误吗?

更新:

我也可以在系统 L&F 上重现此行为,方法是

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

JTextField 创建之前使用:内部(并捕获异常等)。

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. 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.
  5. Press Spacebar to invoke the button.
  6. Press Tab to navigate the focus back to the text-field.
  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.
  • After step 2 (and before 3), the mouse should not be needed any more. 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.
  • In step 6 you can also use Shift+Tab to navigate back to the text-field.
  • On step 7, after typing the first letter, you will see that the button's text changes back to its initial/previous value.

My first tested setup:

  • Apache NetBeans IDE 8.2, which is a bit outdated.

  • java -version yields:

    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)
    
  • javac -version yields javac 1.8.0_161. There is a missmatch here with the runtime environment.

My second tested setup:

  • Apache NetBeans IDE 11.0.

  • java -version yields:

    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)
    
  • javac -version yields javac 12.0.2.

The operating system is Windows 10 on both setups.

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:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

inside just before the JTextField creation (and catching exceptions, etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2025-01-19 02:08:20

根据上述评论,我重新测试了,现在我能够重现该问题。当您增加文本字段的垂直高度(最小量)时会出现此问题。

我想我之前只通过增加文本字段的水平尺寸进行测试。

我找到了一个简单的解决方法:

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

这对我来说绝对是一个错误。

话虽如此,您很少会希望文本字段的垂直高度增加。那么也许您可以找到一个只影响水平尺寸而不影响垂直高度的布局管理器?

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?

葬花如无物 2025-01-19 02:08:20

我没有这个问题。
我使用VScode,java版本1.8.0.25。
我认为问题出在你的编译器上。

I don't have this problem.
I use VScode, java version 1.8.0.25.
I think problem is your compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文