javajlabel循环刷新 公共类Okno扩展JFrame{ 私人利兹尼克·利兹尼克; 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; 公共奥克诺(利兹尼克f) { JPanel面板=新的JPanel(); panel.setLayout(空); //getContentPane().add(面板); setVisible(真); licznik=f; 双x,y,z,l,m,n; 对于(inti=0;i

javajlabel循环刷新 公共类Okno扩展JFrame{ 私人利兹尼克·利兹尼克; 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; 公共奥克诺(利兹尼克f) { JPanel面板=新的JPanel(); panel.setLayout(空); //getContentPane().add(面板); setVisible(真); licznik=f; 双x,y,z,l,m,n; 对于(inti=0;i,java,swing,Java,Swing,Swing是一个单线程框架,这意味着任何阻止事件调度线程的操作都会阻止它处理新事件,包括绘制事件 Thread.sleep将阻止您的代码执行,阻止EDT,这意味着在它返回并且您的方法存在之前无法绘制任何内容 Swing也不是线程安全的,这意味着您不应该从EDT以外的任何线程更新或修改UI 相反,您需要将“等待”卸载到后台线程,当它完成时,相应地更新UI 在这种情况下,最好的选择可能是使用Swing计时器,这将允许您在将来的某个时间安排回调,但在EDT上下文中触发回调,从而可以安全地从内部更新UI

Swing是一个单线程框架,这意味着任何阻止事件调度线程的操作都会阻止它处理新事件,包括绘制事件

Thread.sleep
将阻止您的代码执行,阻止EDT,这意味着在它返回并且您的方法存在之前无法绘制任何内容

Swing也不是线程安全的,这意味着您不应该从EDT以外的任何线程更新或修改UI

相反,您需要将“等待”卸载到后台线程,当它完成时,相应地更新UI

在这种情况下,最好的选择可能是使用Swing
计时器,这将允许您在将来的某个时间安排回调,但在EDT上下文中触发回调,从而可以安全地从内部更新UI

有关详细信息,请参阅和

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) ;
  }

}

导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
JPanel filled=新JPanel(新网格布局(4,4));
JPanel empty=新的JPanel(新的网格布局(4,4));
公共测试窗格(){
setLayout(新的BorderLayout());
对于(int-index=0;index<4*4;index++){
add(新JLabel(Integer.toString(index));
}
对于(int-index=0;index<4*4;index++){
空。添加(新JLabel(“”);
}
添加(空);
JButton show=新JButton(“给我看看钱”);
show.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
移除(空);
增加(填写);
重新验证();
show.setEnabled(false);
重新油漆();
计时器计时器=新计时器(1000,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
添加(空);
移除(填充);
重新验证();
show.setEnabled(true);
重新油漆();
}
});
timer.setRepeats(假);
timer.start();
}
});
添加(显示,边界布局。南部);
}
}
}

线程睡眠(1000)
没有帮助我不确定将所有标签添加到面板中的目的是什么,只是在它们显示之前将它们全部删除。请考虑提供一个示例来说明您的问题。这不是代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少冲突融合和更好responses@MadProgrammer我需要它,所以它需要一些时间来改变,这样我才能看到它是如何改变的。不,你不需要。你需要了解Swing中的并发是如何工作的,以及为什么你所做的是一个致命的钉子。看看和了解更多细节
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);

        }

    }

}