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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…