java线程问题
我用 java swing 编写了一个简单的程序,它应该启动另一个线程,当我单击按钮时,该线程中将显示 JForm。但是 JForm 没有显示...当我评论程序运行时,我使用 if (Thread.currentThread().getName() == "Thread1")
来执行该线程的特定任务完美,我不明白为什么它不会进入 if 块...请有人帮我解决这个问题...
提前致谢!
这是代码,
public class Test extends JFrame implements ActionListener {
JPanel panel;
JButton button;
public Test() {
setVisible(true);
setSize(300, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
panel = new JPanel();
button = new JButton("click me");
button.addActionListener(this);
panel.add(button);
add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Test tst=new Test();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==button){
System.out.println("test");
test2 test = new test2();
Thread tr1 = new Thread(test);
tr1.setName("Thread1");
tr1.start();
}
}
}
class test2 implements Runnable{
public void run() {
//if (Thread.currentThread().getName() == "Thread1") {
System.out.println("inside thread");
JFrame frame2=new JFrame();
frame2.setVisible(true);
frame2.setSize(300, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//}
}
}
I wrote a simple program with java swing which suppose to start another thread and in that thread a JForm will show up when I click a button. But JForm is not showing up... I used if (Thread.currentThread().getName() == "Thread1")
to do the specific task for that thread, when I comment that program runs perfectly, I can't understand why it is not going to the if block... Please someone help me with this...
Thanks in advance!
Here is the code,
public class Test extends JFrame implements ActionListener {
JPanel panel;
JButton button;
public Test() {
setVisible(true);
setSize(300, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
panel = new JPanel();
button = new JButton("click me");
button.addActionListener(this);
panel.add(button);
add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Test tst=new Test();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==button){
System.out.println("test");
test2 test = new test2();
Thread tr1 = new Thread(test);
tr1.setName("Thread1");
tr1.start();
}
}
}
class test2 implements Runnable{
public void run() {
//if (Thread.currentThread().getName() == "Thread1") {
System.out.println("inside thread");
JFrame frame2=new JFrame();
frame2.setVisible(true);
frame2.setSize(300, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
getName().equals("Thread1")
代替。equals
比较字符串,==
检查两个字符串是否是同一个对象。Try using
getName().equals("Thread1")
instead.equals
compares the strings,==
checks if the two strings are the same object.尝试:
或
Try:
or
为什么要检查当前线程名称?无论如何,该线程将是唯一调用该方法的线程。
您不得使用
==
比较String
值,因为它会检查对象身份。您应该使用Thread.currentThread().getName().equals("Thread1")
代替。您不应与事件之外的任何 Swing/AWT 组件进行交互调度线程!
why do you have that check for the current threads name anyway? That thread will be the only one to call that method anyway.
You must not compare
String
values using==
as it checks for object identity. You should useThread.currentThread().getName().equals("Thread1")
instead.You should not interact with any Swing/AWT components outside of the Event Dispatch Thread!