Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - Thread and JLabel in Swing- Not working properly

Please look at the code below.

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;

class thread_jishu extends Thread{
    @Override
    public void run(){
        int p=-1;
        for(;;){
            //here continuously checking that whether
            //the value of label_thread.i is equals to p or not

            if(label_thread.i!=p){
                try{
                    Thread.sleep(1000);
                }catch(Exception e){}

                label_thread.lb.setText("after sleeping at -> "+label_thread.i);
                // here i want to set the JLabel
                // text after waiting 1sec only when
                // label_thread.i has been changed,
                // but not happening 
                p=label_thread.i;
            }
        }
    }
}

public class label_thread  implements java.awt.event.ActionListener{

    /**
     * @param evt
     */
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt){
        i+=1;
        lb.setText("Button clicked at -> "+i);
    }

    static int i=-1;
    static JLabel lb=new JLabel("hello here");
    static JFrame window=new JFrame("Jishu");
    static JFrame window2=new JFrame("Jishu");

    public static void main(String[] args) {
        // TODO code application logic here
        new thread_jishu().start();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setMinimumSize(new java.awt.Dimension(200,200));

        JButton bt=new JButton("Click here");
        bt.addActionListener(new label_thread());
        JPanel panel=new JPanel();
        panel.add(bt);
        window.add(panel);
        window2.add(lb);
        window.setVisible(true);
        window2.setVisible(true);
        window2.setMinimumSize(new java.awt.Dimension(200,200));
    }

}

I want to reset the JLabel in the 2nd window when the value of i is not equals to p that means the button is clicked in 1st window.

But when button is clicked JLabel's text is not being changed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Swing elements should not be modified outside of the AWT event thread. You can use SwingUtilities.invokeLater if you'd like to execute code in the AWT event thread.

Something like:

SwingUtilities.invokeLater(new Runnable() {
    @override
    public void run() {
        //Swing stuff here.
    }
});

would get you pretty close.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...