以下簡單代碼:
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按鈕和文本欄位之間交替/導航時出現問題。以下是在我的設定中總是重現相同錯誤的具體步驟:
- 運行程式。
- 將框架調整為比原來大一點。
- 在文本欄位中鍵入一些短文本。
- 按下Tab可將焦點導航到按鈕。
- 按下Spacebar以呼叫按鈕。
- 按下Tab可將焦點導航回文本欄位。
- 在文本欄位中輸入您喜歡的任何字母。
筆記:
- 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
abcbut 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 -versionyields: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 -versionyieldsjavac 1.8.0_161. There is a missmatch here with the runtime environment.
My second tested setup:
Apache NetBeans IDE 11.0.
java -versionyields: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 -versionyieldsjavac 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).
uj5u.com熱心網友回復:
根據上述評論,我重新測驗了,現在我能夠重現該問題。當您增加文本欄位的垂直高度(少量)時會出現此問題。
我想我之前只通過增加文本欄位的水平大小進行了測驗。
我找到了一個簡單的解決方法:
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);
});
}
}
這對我來說絕對是一個錯誤。
話雖如此,您很少希望文本欄位的垂直高度增加大小。所以也許你能找到一個只影響水平尺寸而不影響垂直高度的布局管理器?
uj5u.com熱心網友回復:
我沒有這個問題。我使用 VScode,java 版本 1.8.0.25。我認為問題是你的編譯器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439792.html
標籤:java swing jbutton border-layout
上一篇:按下按鈕時出現復選框,java
下一篇:在Swing中漸變顏色?
