2012-03-25 106 views
8
import java.awt.Graphics; 
import javax.swing.*; 

public class Demo 
{ 
    JFrame jf; 
    JLabel[] labels; 
    JPanel panel; 

    public Demo() 
    { 
     jf = new JFrame(); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     labels = new JLabel[10]; 
     Box vbox = Box.createVerticalBox(); 
     for (int i = 0; i < 10; i++) 
     { 
      labels[i] = new JLabel(); 
      vbox.add(labels[i]); 
     } 
     panel = new JPanel(); 
     panel.add(vbox); 
     jf.add(panel); 
     jf.setSize(300, 250); 
     jf.setVisible(true); 
    } 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new DemoRunnable()); 
    } 
    public void updateState() 
    { 
     for (JLabel l : labels) 
     { 
      if (Math.random() > 0.5) 
       l.setText("777777777777777777777777777777777777"); 
      else 
       l.setText("10000000000000000000000000000000000000"); 
     } 
    } 
} 
class DemoRunnable implements Runnable 
{ 
    Demo demo; 

    DemoRunnable() 
    { 
     this.demo = new Demo(); 
    } 
    @Override 
    public void run() 
    { 
     Thread t = new Thread(new Runnable() 
     { 

      @Override 
      public void run() 
      { 
       while (true) 
       { 
        try 
        { 
         Thread.sleep(0); 
        } 
        catch (InterruptedException e) 
        { 
         e.printStackTrace(); 
        } 
        demo.updateState(); 
       } 
      } 
     }); 
     t.start(); 

    } 
} 

我看到這樣的效果,當這個程序執行。是否有可能消除它(零點必須是點)?JLabel油漆點

enter image description here

+2

這基本上是因爲你的文本溢出'JLabel'的邊界。 – 2012-03-25 18:50:56

+0

+1 [sscce](http://sscce.org/)。 – trashgod 2012-03-25 19:29:21

回答

3

我在回答代碼示例只,

import java.awt.EventQueue; 
import java.awt.GridLayout; 
import javax.swing.*; 

public class Demo { 

    private JFrame jf; 
    private JLabel[] labels; 
    private JPanel panel; 

    public Demo() { 
     labels = new JLabel[10]; 
     Box vbox = Box.createVerticalBox(); 
     for (int i = 0; i < 10; i++) { 
      labels[i] = new JLabel(); 
      labels[i].setText("10000000000000000000000000000000000000"); 
      vbox.add(labels[i]); 
     } 
     panel = new JPanel(); 
     panel.setLayout(new GridLayout()); 
     panel.add(vbox); 
     jf = new JFrame(); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jf.add(panel); 
     jf.pack(); 
     jf.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new DemoRunnable()); 
    } 

    public void updateState() { 
     for (final JLabel l : labels) { 
      if (Math.random() > 0.5) { 
       EventQueue.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         l.setText("777777777777777777777777777777777777"); 
        } 
       }); 
      } else { 
       EventQueue.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         l.setText("10000000000000000000000000000000000000"); 
        } 
       }); 
      } 
     } 
    } 
} 

class DemoRunnable implements Runnable { 

    private Demo demo; 

    DemoRunnable() { 
     this.demo = new Demo(); 
    } 

    @Override 
    public void run() { 
     Thread t = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       while (true) { 
        try { 
         Thread.sleep(250); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        demo.updateState(); 
       } 
      } 
     }); 
     t.start(); 
    } 
} 
+0

對於invokeLater(),+1以及爲什麼'javax.swing.Timer'非常方便。 – trashgod 2012-03-25 20:40:15

+0

@trashgod同意爲+1,但我認爲OP不明白FlowLayout的工作方式, – mKorbel 2012-03-25 20:51:50

+0

@mKorbel flowLayout如何影響我的問題?如果我正確理解這種行爲的原因(奇怪的點)是在EDT之外調用'setText',但'setText'不是線程安全的方法 – eXXXXXXXXXXX2 2012-03-26 06:25:02

4

代替setSize()使用pack()採取組件的仔細計算首選大小的優勢。您還需要初始化您的標籤:

labels[i] = new JLabel("10000000000000000000000000000000000000"); 

還要考慮javax.swing.Timer,而不是一個單獨的線程。

附錄:方便的是,每個Swing Timer共享一個公用後臺線程,並且在事件分派線程上調用actionPerformed()。替代方案是SwingWorker,圖示爲here

+0

另請參見[併發中的擺動](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 – trashgod 2012-03-25 18:57:36

+0

我應該定期爲我的問題調用setText(),所以我需要另一種解決方案(我的代碼只是示例) – eXXXXXXXXXXX2 2012-03-25 19:17:14