JLabel draws dots

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

I see this effect when running this program. Is it possible to eliminate it (zeros should be instead of dots)?

enter image description here

+8
java swing jlabel
source share
2 answers

my code in the answer is just an example,

 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(); } } 
+3
source share

Instead of setSize() use pack() to take advantage of the carefully calculated preferred component size. You will also need to initialize your shortcut:

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

Also consider javax.swing.Timer instead of a separate thread.

Addition. Conveniently, each Swing Timer has a common background thread, and actionPerformed() is called in the event dispatch thread. An alternative to SwingWorker is illustrated here .

+4
source share

All Articles