2017-06-21 135 views
1

只要循環正在運行,我想每秒更新一次Jlabel文本。我怎麼能這樣做?我想做這個fromat。在Java Swing中通過循環更新Jlabel文本?

JPanel jpnl=new JPanel(); 
    jfrm.add(jpnl); 
    String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 
    jlab = new JLabel("This is Label"); 
    jpnl.add(jlab); 

     for(int i=0;i<fonts.length;i++){ 
      System.out.println(fonts[i]); 
      jlab.setText(fonts[i]); 
      jlab.setFont(new Font(fonts[i],Font.PLAIN,30)); 
      jlab.setForeground(Color.DARK_GRAY); 
     } 
+0

如果只是時間的問題,你有沒有指定任何其他要求,你可以使用了Thread.sleep(1000)環路 – Nenad

+0

你能解釋一下里面循環內更簡單的Thread.sleep(1000)將不起作用。 –

+1

首先,不要使用'Thread.sleep',它只會讓循環花費更長的時間運行並阻止UI被更新。圓錐形的答案是使用一個Swing的'定時器' – MadProgrammer

回答

6

Swing的單線程性質排除了使用循環或Thread.sleep您似乎嘗試的方式。這樣做,只會阻止用戶界面,並阻止它被繪製/更新,直到循環完成。

因爲Swing不是線程安全的,你不能簡單地用另一Thread和上面的方法來更新UI,而無需通過一些箍

錐形回答你的問題是使用一個Swing Timer跳躍,這會在常規基礎上觸發更新。因爲這些更新是在事件派發線程的上下文中觸發的,所以當您想更新UI時,它可以安全使用。

How to use Swing Timers細看了解更多詳情

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GraphicsEnvironment; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
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 { 

     private String[] fonts; 
     private final JLabel jlab; 
     private int index = 0; 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 
      jlab = new JLabel("This is Label"); 
      add(jlab); 

      Timer timer = new Timer(1000, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        updateFont(); 
        index++; 
        if (index >= fonts.length) { 
         ((Timer)e.getSource()).stop(); 
        } 
       } 
      }); 
      timer.setInitialDelay(0); 
      timer.start(); 
     } 

     protected void updateFont() { 
      System.out.println(fonts[index]); 
      jlab.setText(fonts[index]); 
      jlab.setFont(new Font(fonts[index], Font.PLAIN, 30)); 
      jlab.setForeground(Color.DARK_GRAY);   
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

}