Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

swing - java label setText and setBounds clashing?

I would like to have a JLabel changint color to a random one, while jumping to a random position, and while changing its text.

but the setText and setBounds seem to clash and i don't know why. if you comment out the setText then the setBounds will work, but they won't work together.

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class test2 extends JFrame { 

private static JLabel label = new JLabel("0");
private static Random gen = new Random();

public test2() {
    JPanel panel = new JPanel();
    panel.add(label);
    this.add(panel);
}

public static void move() {
    for (int i = 0; i < 10; i++) {
        int n = gen.nextInt(254)+1;
        int nn = gen.nextInt(254)+1;
        int nnn = gen.nextInt(254)+1;
        label.setText(""+i);
        //the setBounds command will not work with the setText command. why?
        label.setBounds(n*2, nn*2, 20, 20);
        label.setForeground(new Color(n, nn, nnn));
        try {
            Thread.sleep(200);
        } catch (Exception e) {}
    }
}

public static void main(String[] args) {
    test2 frame = new test2();
    frame.setVisible(true);
    frame.setSize(600, 600);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    move();
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

setBounds shouldn't work with a non-null layout, and your problem is happening because changing the text in the JLabel stimulates a re-layout of the JPanel, and since JPanels by default use FlowLayout, the JLabel stays at the top in the middle. So if you really need this functionality, then consider setting the layout of the JPanel to null, or else use a JLayeredPane.

  public test2() {
     JPanel panel = new JPanel(null);
     panel.add(label);
     this.add(panel);
  }

Also you shouldn't be calling Thread.sleep(...) on the EDT, the Swing thread. Instead consider using a Swing Timer.

Since I disagreed with the other example, I will post an example of what I was recommending in its place, one that uses a Swing Timer:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class Test3 extends JPanel {
    private static final int SIDE = 600;
    public static final int MAX_COUNTER = 20;
    private static final int TIMER_DELAY = 400;
    private static final int MAX_RAND = 220;
    private int counter = 0;
    private JLabel label = new JLabel(String.valueOf(counter));
    private Random gen = new Random();
    private int[] randNumb = new int[3];

    public Test3() {
        setPreferredSize(new Dimension(SIDE, SIDE));
        setLayout(null);
        add(label);

        new Timer(TIMER_DELAY, new TimerListener()).start();
    }

    private void myMove() {
        for (int i = 0; i < randNumb.length; i++) {
            randNumb[i] = gen.nextInt(MAX_RAND);
        }
        label.setText(String.valueOf(counter));
        label.setSize(label.getPreferredSize());
        label.setLocation((SIDE * randNumb[0])/MAX_RAND, (SIDE*randNumb[1])/MAX_RAND);
        label.setForeground(new Color(randNumb[0], randNumb[1], randNumb[2]));
        repaint();
    }

    private class TimerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (counter < MAX_COUNTER) {
                myMove();
                counter++;
            } else {
                ((Timer)e.getSource()).stop();
            }
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Test3");
        frame.getContentPane().add(new Test3());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...