gpt4 book ai didi

java - EventDispatchThread.run() 行 : not available error in eclipse

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:13:02 25 4
gpt4 key购买 nike

我是 Swing 的新手,正在 Eclipse 中制作一个非常基本的事件处理程序。这是我写的代码:

public class SwingDemo2 {

JLabel jl;

public SwingDemo2() {
JFrame jfr = new JFrame("Swing Event Handling");
jfr.setSize(250, 100);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jl = new JLabel();
jl.setVisible(false);

JButton jb1 = new JButton("OK");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jl.setText("You Pressed OK");
jl.setVisible(true);
}
});

JButton jb2 = new JButton("Reset");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jl.setText("You Pressed Reset");
jl.setVisible(true);
}
});

jfr.setLayout(new BorderLayout());
jfr.add(jl, SwingConstants.NORTH);
jfr.add(jb1, SwingConstants.EAST);
jfr.add(jb2, SwingConstants.WEST);
jfr.setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo2();
}
});
}

}

Eclipse 提示我打开调试透视图,他向我显示了错误:
线程 [AWT-EventQueue-0](暂停(异常 IllegalArgumentException))EventDispatchThread.run() 行:不可用 [局部变量不可用]

当我使用 FlowLayout 而不是 BorderLayout 时,我没有收到任何错误。

我一直试图在门户网站上查找有关错误的信息,我遇到了 this类似的问题。答案是在不解释问题的情况下更改一堆设置(这也没有帮助)。请解释错误,以便我确保不再重复。提前致谢!

注意:更新了错误信息

最佳答案

  • 使用 BorderLayout 代替 SwingConstantjfr.add(jl, BorderLayout.NORTH);

    /li>
  • SwingConstants 是为 TextLayout 实现的,而不是为 JComponents 布局

关于java - EventDispatchThread.run() 行 : not available error in eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183951/

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