动态JLabel文本

时间:2014-05-14 23:41:16

标签: java multithreading swing jframe jpanel

我是一个超级Java菜鸟,需要一些帮助。我正在尝试编写一个代码,显示" on"在JPanel for 5(或者我传入y变量)中,然后将单词更改为" off"在同一JPanel上。想象一下在一段时间内显示绿色然后变为红色的红绿灯。我在下面编写的代码打开了两个单独的JFrame来显示不同的单词。任何帮助或想法将不胜感激。

import javax.swing.*;

public class practice extends JFrame implements Runnable {

int  x;
int y;
JLabel show = new JLabel("on");

JLabel show2 = new JLabel("off");
boolean yes;


public practice(boolean on, int x){
    x=y;
    yes = on;
    setTitle("Stoplight");
    setSize(500, 500);
    setResizable(true);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}



public void test(){
    try {

        Thread.sleep(y);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (yes == true){
        add(show2);
    }else if (yes == false)
         add(show);

}

public void run() {
    test();
}

public static void main (String[] args){

    Thread t1 = new Thread(new practice(true, 50000));
    Thread t2 = new Thread(new practice(false, 0));
    t1.start();
    t2.start();

}



    }

2 个答案:

答案 0 :(得分:1)

您需要删除标签' on'在添加标签' off'之前用方法remove(jcomponent)

答案 1 :(得分:1)

正如已经暗示的那样,您应该使用javax.swing.Timer,这将允许您在指定的时间段后安排回调。

除非您有特殊需要,否则更改标签文本以删除旧标签并添加新标签(恕我直言)

更为简单

例如......

Dynamic Label

import java.awt.BorderLayout;
import java.awt.EventQueue;
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;
import javax.swing.border.EmptyBorder;

public class DynamicLabel {

    public static void main(String[] args) {
        new DynamicLabel();
    }

    public DynamicLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane(5000));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane(int delay) {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(8, 8, 8, 8));
            label = new JLabel("On");
            add(label);

            Timer timer = new Timer(delay, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    label.setText("Off");
                }
            });
            timer.setRepeats(false);
            timer.start();
        }

    }

}