Reputation: 1
public class Okno extends JFrame {
private Licznik licznik;
JLabel lWyświetl1;
JLabel lWyświetl2;
JLabel lWyświetl3;
JLabel lWyświetl4;
JLabel lWyświetl5;
JLabel lWyświetl6;
JLabel lWyświetl7;
JLabel lWyświetl8;
JLabel lWyświetl9;
JLabel lWyświetl10;
JLabel lWyświetl20;
JLabel lWyświetl30;
JLabel lWyświetl40;
JLabel lWyświetl50;
JLabel lWyświetl60;
JLabel lWyświetl70;
JLabel lWyświetl80;
JLabel lWyświetl90;
JLabel lWyświetl11;
JLabel lWyświetl21;
JLabel lWyświetl31;
JLabel lWyświetl41;
JLabel lWyświetl51;
JLabel lWyświetl61;
JLabel lWyświetl71;
JLabel lWyświetl81;
JLabel lWyświetl91;
public Okno(Licznik f)
{
JPanel panel = new JPanel();
panel.setLayout (null);
//getContentPane().add(panel);
setVisible(true);
licznik = f;
double x,y,z,l,m,n;
for(int i=0; i<9; i++)
{
x=licznik.cut() ;
y=licznik.cut1() ;
z=licznik.cut2() ;
l=x / 1000000000 * ((int)Math.pow(10, i));
m=y / 1000000000 * ((int)Math.pow(10, i));
n=z / 1000000000 * ((int)Math.pow(10, i));
int q = (int)l;
int w = (int)m;
int s = (int)n;
int t,r,p;
t=q;
r=w;
p=s;
System.out.println( + t);
System.out.println( + r);
System.out.println( + p);
t=q % 10;
r=w % 10;
p=s % 10;
System.out.println( + t);
System.out.println( + r);
System.out.println( + p);
setSize(300,300);
setLayout(null);
String a = " -- ";
String b = "*********";
if(t==r&&r==p)
{
t=0;
r=0;
p=0;
}
String bb=a;
String cc=a;
String dd=a;
String ee=a;
String ff=a;
String gg=a;
String hh=a;
String ii=a;
String jj=a;
//if(t==8){bb = b;}
lWyświetl1 = new JLabel(bb);
lWyświetl1.setBounds(100,70,20,20);
add(lWyświetl1);
bb =a;
//if(t==7){cc = b;}
lWyświetl2 = new JLabel(cc);
lWyświetl2.setBounds(100,90,20,20);
add(lWyświetl2);
cc=a;
//if(t==6){dd = b;}
lWyświetl3 = new JLabel(dd);
lWyświetl3.setBounds(100,110,20,20);
add(lWyświetl3);
dd=a;
//if(t==5){ee = b;}
lWyświetl4 = new JLabel(ee);
lWyświetl4.setBounds(100,130,20,20);
add(lWyświetl4);
ee=a;
//if(t==4){ff = b;}
lWyświetl5 = new JLabel(ff);
lWyświetl5.setBounds(100,150,20,20);
add(lWyświetl5);
ff=a;
//if(t==3){gg = b;}
lWyświetl6 = new JLabel(gg);
lWyświetl6.setBounds(100,170,20,20);
add(lWyświetl6);
gg=a;
if(t==0){hh = b;}
lWyświetl7 = new JLabel(hh);
lWyświetl7.setBounds(100,190,20,20);
add(lWyświetl7);
hh=a;
if(t==2){ii = b;}
lWyświetl8 = new JLabel(ii);
lWyświetl8.setBounds(100,210,20,20);
add(lWyświetl8);
ii=a;
if(t==1){jj = b;}
lWyświetl9 = new JLabel(jj);
lWyświetl9.setBounds(100,230,20,20);
add(lWyświetl9);
jj=a;
////////////////////////////////////////////////////////////////////////////////////////////////////
//if(r==8){bb = b;}
lWyświetl10 = new JLabel(bb);
lWyświetl10.setBounds(120,70,20,20);
add(lWyświetl10);
bb=a;
//if(r==7){cc = b;}
lWyświetl20 = new JLabel(cc);
lWyświetl20.setBounds(120,90,20,20);
add(lWyświetl20);
cc=a;
//if(r==7){dd = b;}
lWyświetl30 = new JLabel(dd);
lWyświetl30.setBounds(120,110,20,20);
add(lWyświetl30);
dd=a;
if(r==6){ee = b;}
lWyświetl40 = new JLabel(ee);
lWyświetl40.setBounds(120,130,20,20);
add(lWyświetl40);
ee=a;
if(r==5){ff = b;}
lWyświetl50 = new JLabel(ff);
lWyświetl50.setBounds(120,150,20,20);
add(lWyświetl50);
ff=a;
if(r==4){gg = b;}
lWyświetl60 = new JLabel(gg);
lWyświetl60.setBounds(120,170,20,20);
add(lWyświetl60);
gg=a;
if(r==0){hh = b;}
lWyświetl70 = new JLabel(hh);
lWyświetl70.setBounds(120,190,20,20);
add(lWyświetl70);
hh=a;
//if(r==2){ii = b;}
lWyświetl80 = new JLabel(ii);
lWyświetl80.setBounds(120,210,20,20);
add(lWyświetl80);
ii=a;
//if(r==1){jj = b;}
lWyświetl90 = new JLabel(jj);
lWyświetl90.setBounds(120,230,20,20);
add(lWyświetl90);
jj=a;
////////////////////////////////////////////////////////////////////////////////////////////////////
if(p==9){bb = b;}
lWyświetl11 = new JLabel(bb);
lWyświetl11.setBounds(140,70,20,20);
add(lWyświetl11);
bb=a;
if(p==8){cc = b;}
lWyświetl21 = new JLabel(cc);
lWyświetl21.setBounds(140,90,20,20);
add(lWyświetl21);
cc=a;
if(p==7){dd = b;}
lWyświetl31 = new JLabel(dd);
lWyświetl31.setBounds(140,110,20,20);
add(lWyświetl31);
dd=a;
//if(p==5){ee = b;}
lWyświetl41 = new JLabel(ee);
lWyświetl41.setBounds(140,130,20,20);
add(lWyświetl41);
ee=a;
//if(p==4){ff = b;}
lWyświetl51 = new JLabel(ff);
lWyświetl51.setBounds(140,150,20,20);
add(lWyświetl51);
ff=a;
//if(p==3){gg = b;}
lWyświetl61 = new JLabel(gg);
lWyświetl61.setBounds(140,170,20,20);
add(lWyświetl61);
gg=a;
if(p==0){hh = b;}
lWyświetl71 = new JLabel(hh);
lWyświetl71.setBounds(140,190,20,20);
add(lWyświetl71);
hh=a;
//if(p==2){ii = b;}
lWyświetl81 = new JLabel(ii);
lWyświetl81.setBounds(140,210,20,20);
add(lWyświetl81);
ii=a;
//if(p==1){jj = b;}
lWyświetl91 = new JLabel(jj);
lWyświetl91.setBounds(140,230,20,20);
add(lWyświetl91);
jj=a;
////////////////////////////////////////////////////////////////////////////////////////////////////
panel.add(lWyświetl1);
panel.add(lWyświetl2);
panel.add(lWyświetl3);
panel.add(lWyświetl4);
panel.add(lWyświetl5);
panel.add(lWyświetl6);
panel.add(lWyświetl7);
panel.add(lWyświetl8);
panel.add(lWyświetl9);
panel.add(lWyświetl10);
panel.add(lWyświetl20);
panel.add(lWyświetl30);
panel.add(lWyświetl40);
panel.add(lWyświetl50);
panel.add(lWyświetl60);
panel.add(lWyświetl70);
panel.add(lWyświetl80);
panel.add(lWyświetl90);
panel.add(lWyświetl11);
panel.add(lWyświetl21);
panel.add(lWyświetl31);
panel.add(lWyświetl41);
panel.add(lWyświetl51);
panel.add(lWyświetl61);
panel.add(lWyświetl71);
panel.add(lWyświetl81);
panel.add(lWyświetl91);
panel.removeAll();
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
licznik.kil(0) ;
licznik.kil1(0) ;
licznik.kil2(0) ;
}
}
I tried but have no idea how to make these JLabels to repaint. Plus it sometimes print sth really stupid. I simply want it to remove all and place the Jlabels once more. But no matter what I do it simply stays the same way. I tried to do repaint, remove all. Is there a way to do it in a simple way?
Upvotes: 0
Views: 1053
Reputation: 347204
Swing is a single threaded framework, this means that anything that blocks the Event Dispatching Thread, will prevent it from processing new events, including paint events.
Thread.sleep
will stop your code from executing, blocking the EDT, which means nothing can ever be painted until it returns and your method exists.
Swing is also not thread safe, this means that you should never update or modify the UI from any thread other then the EDT
Instead, you need to off load the "waiting" to a background thread and when it completes, update the UI accordingly.
Probably the best choice in this sitation is using a Swing Timer
, which will allow you to schedule a callback at some time in the future, but which is triggered within the context of the EDT, making it safe to update the UI from within.
See How to use Swing Timers and Concurrency in Swing for more details
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
JPanel filled = new JPanel(new GridLayout(4, 4));
JPanel empty = new JPanel(new GridLayout(4, 4));
public TestPane() {
setLayout(new BorderLayout());
for (int index = 0; index < 4 * 4; index++) {
filled.add(new JLabel(Integer.toString(index)));
}
for (int index = 0; index < 4 * 4; index++) {
empty.add(new JLabel(" "));
}
add(empty);
JButton show = new JButton("Show me the money");
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remove(empty);
add(filled);
revalidate();
show.setEnabled(false);
repaint();
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(empty);
remove(filled);
revalidate();
show.setEnabled(true);
repaint();
}
});
timer.setRepeats(false);
timer.start();
}
});
add(show, BorderLayout.SOUTH);
}
}
}
Upvotes: 2