java线程问题

发布于 2024-10-21 20:42:52 字数 1314 浏览 0 评论 0原文

我用 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 技术交流群。

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

发布评论

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

评论(3

你的笑 2024-10-28 20:42:52

尝试使用 getName().equals("Thread1") 代替。

equals 比较字符串,== 检查两个字符串是否是同一个对象。

Try using getName().equals("Thread1") instead.

equals compares the strings, == checks if the two strings are the same object.

飘然心甜 2024-10-28 20:42:52

尝试:

if (Thread.currentThread().getName().equals("Thread1"))

if (Thread.currentThread().getName().compareTo("Thread1") > 0)

Try:

if (Thread.currentThread().getName().equals("Thread1"))

or

if (Thread.currentThread().getName().compareTo("Thread1") > 0)
流年已逝 2024-10-28 20:42:52
  1. 为什么要检查当前线程名称?无论如何,该线程将是唯一调用该方法的线程。

  2. 您不得使用 == 比较 String 值,因为它会检查对象身份。您应该使用 Thread.currentThread().getName().equals("Thread1") 代替。

  3. 您不应与事件之外的任何 Swing/AWT 组件进行交互调度线程

  1. why do you have that check for the current threads name anyway? That thread will be the only one to call that method anyway.

  2. You must not compare String values using == as it checks for object identity. You should use Thread.currentThread().getName().equals("Thread1") instead.

  3. You should not interact with any Swing/AWT components outside of the Event Dispatch Thread!

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