简体   繁体   English

JLabel将文本从一件事更改为另一件事

[英]JLabel that changes text from one thing to another

I'm working on this program and I ran into another issue. 我正在研究此程序,却遇到了另一个问题。 I have a Jframe with a JLabel that I wish for it to change text from one thing to another. 我有一个带有JLabel的Jframe,希望它可以将文本从一件事更改为另一件事。 However, when I try to do that it doesnt show me the text changing, rather the last text I set it to. 但是,当我尝试执行此操作时,它不会显示文本更改,而是显示我将其设置为的最后一个文本。

How do I get my JLabel to cycle through text SLOWLY? 如何让我的JLabel缓慢地循环显示文本?

I'm trying a wait method to make the program go slowly so I can see if I can make it cycle through, but that doesnt seem to be working. 我正在尝试一种等待程序,以使程序缓慢运行,以便可以查看是否可以使程序循环执行,但这似乎不起作用。

it would be helpful if someone could edit my code or make their own example of how to do this, THANKS! 谢谢!如果有人可以编辑我的代码或制作自己的示例方法,这将很有帮助!

public class CreditGraphics {

    public String cardNum;
    public JFrame frame;
    public JPanel panel;
    public JLabel label;
    public JTextField text;

    public CreditGraphics() {
    synchronized(this){
    try {


        frame = new JFrame("HI");
        panel = new JPanel();
        label = new JLabel();

        text = new JTextField(16);

        panel.add(label);
        panel.add(text);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(500, 500));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true); 

        wait(4000);
        label.setText("Hi");
        wait(4000);
        frame.revalidate();
        frame.repaint();
        label.setText("Hello");
        frame.revalidate();
        frame.repaint();


        text.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardNum = text.getText();

            }

        });

         }
         catch(InterruptedException e) {
    e.printStackTrace();
    }}

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {


                new CreditGraphics();

               }




        });
    }

    public void checkCard(){


    }

}

As suggested by @trashgod use Swing Timer that is more suitable for swing application to perform a task once, after a delay or to perform a task repeatedly. 正如@trashgod所建议的那样,请使用Swing计时器 ,它更适合Swing应用程序执行一次任务,延迟后或重复执行任务。

sample code: 样例代码:

private Timer timer;
...
label.setText("Hi");
// delay of 4 seconds
timer=new Timer(4000,new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        label.setText("Hello");
        // timer.stop(); // stop the timer if repeated mode is on
    }
});
timer.setRepeats(false); // you can turn-on it if needed
timer.start();

Note: 注意:

  • There is no need to call frame.repaint() and frame.revalidate() in this case. 在这种情况下,无需调用frame.repaint()frame.revalidate()
  • Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting. 重写getPreferredSize()以在自定义绘制的情况下设置JPanel的首选大小。

sample code: 样例代码:

JPanel panel = new JPanel() {

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

read more... 阅读更多...

Do not use Thread.sleep() or wait() as it will freeze your Swing application. 不要使用Thread.sleep()方法或wait(),因为它会冻结你的Swing应用程序。

Instead you should use a javax.swing.Timer 相反,您应该使用javax.swing.Timer

See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples. 有关更多信息和示例,请参见Java教程如何使用Swing计时器课程:Swing中的并发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM