定期更改 JButton 文本

发布于 2024-11-27 06:51:46 字数 96 浏览 0 评论 0原文

我想创建一个 JButton,在第一次单击后定期更改其文本。我对 Swing 库不太熟悉。什么是一个好的起点?我可以在不执行任何操作的情况下更新其文本吗?

谢谢。

I would like to create a JButton that changes its text periodically after the first click. I'm not really familiar with Swing library. What would be a good starting point? May I update its text without an action?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独行侠 2024-12-04 06:51:46

对于 Swing 中的所有周期性事件,我只建议 javax.swing。 计时器

使用 Timer 的 输出应该是,对于例子

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class CrazyButtonTimer {

    private JFrame frame = new JFrame(" Crazy Button Timer");
    private JButton b = new JButton("Crazy Colored Button");
    private Random random;

    public CrazyButtonTimer() {
        b.setPreferredSize(new Dimension(250, 35));
        frame.getContentPane().add(b);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        javax.swing.Timer timer = new Timer(500, new TimerListener());
        timer.setInitialDelay(250);
        timer.start();
    }

    private class TimerListener implements ActionListener {

        private TimerListener() {
        }

        @Override
        public void actionPerformed(final ActionEvent e) {
            Color c = b.getForeground();
            if (c == Color.red) {
                b.setForeground(Color.blue);
            } else {
                b.setForeground(Color.red);
            }
        }
    }

    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CrazyButtonTimer crazyButtonTimer = new CrazyButtonTimer();
            }
        });
    }
}

for all periodical events in Swing I only suggest javax.swing.Timer

output by using Timer should be, for example

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class CrazyButtonTimer {

    private JFrame frame = new JFrame(" Crazy Button Timer");
    private JButton b = new JButton("Crazy Colored Button");
    private Random random;

    public CrazyButtonTimer() {
        b.setPreferredSize(new Dimension(250, 35));
        frame.getContentPane().add(b);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        javax.swing.Timer timer = new Timer(500, new TimerListener());
        timer.setInitialDelay(250);
        timer.start();
    }

    private class TimerListener implements ActionListener {

        private TimerListener() {
        }

        @Override
        public void actionPerformed(final ActionEvent e) {
            Color c = b.getForeground();
            if (c == Color.red) {
                b.setForeground(Color.blue);
            } else {
                b.setForeground(Color.red);
            }
        }
    }

    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CrazyButtonTimer crazyButtonTimer = new CrazyButtonTimer();
            }
        });
    }
}
江城子 2024-12-04 06:51:46

如果您要在每个固定的时间量更改它,那么您可以使用 Swing Timer 或线程来执行此操作。但为此,您必须至少侦听一个操作,以便可以初始化并启动它。

您还可以使用 java 中的 TimerTask 类。实用程序如下:

java.util.TimerTask timerTask = new java.util.TimerTask() {
    @Override
    public void run() {
        //change button text here using button.setText("newText"); method
    }
};

java.util.Timer myTimer = new java.util.Timer();
myTimer.schedule(timerTask, 3 * 1000, 3* 1000); // This will start timer task after 3 seconds and repeat it on every 3 seconds.

If you to change it on every fixed amount of time then you can use Swing Timer or Thread to do this. But for this you have to listen at least one action so that you can initialize and start it.

You can also use TimerTask class from java.util like follow:

java.util.TimerTask timerTask = new java.util.TimerTask() {
    @Override
    public void run() {
        //change button text here using button.setText("newText"); method
    }
};

java.util.Timer myTimer = new java.util.Timer();
myTimer.schedule(timerTask, 3 * 1000, 3* 1000); // This will start timer task after 3 seconds and repeat it on every 3 seconds.
纵性 2024-12-04 06:51:46

我建议您创建一个计时器(此处你可以找到一些文档)

Timer timer = new Timer(100,this);

你的类必须扩展动作侦听器ed实现以下方法,该方法允许你更改JButton的文本(我称之为“按钮”)。

public void actionPerformed(ActionEvent e) {
  if(e.getSource.equals(timer)){
    button.setText("newText");
  }
}

卢卡

I suggest you to create a timer (here you can find some doc)

Timer timer = new Timer(100,this);

Your class has to extend action listener ed implements the following method which allow you to change the text of your JButton(I called it ``button).

public void actionPerformed(ActionEvent e) {
  if(e.getSource.equals(timer)){
    button.setText("newText");
  }
}

Luca

一场信仰旅途 2024-12-04 06:51:46

所有其他答案都没有提及如何非定期更新。如果您需要不定期更新,您可以在 GUI 类中创建一个名为 updateButton() 的方法;每次你想要它改变你的文本时就调用它。

public void updateButton(String newText)
{
     Button.setText(newText);
}

只是想我会添加这个以防有人想要不规则地设置它。

All the other answers fail to mention how to update non-periodically. If you need it to update irregularly, you can make a method in your GUI class called something like: updateButton(); and just call that every time you want it to change your text.

public void updateButton(String newText)
{
     Button.setText(newText);
}

Just thought I'd add this in case someone wanted to set it irregularly.

情域 2024-12-04 06:51:46

如果您想定期更改它(例如每 5 秒),您可以创建一个新线程,将按钮的文本设置为所需的值并重新绘制它(如果需要)。

If you want to change it periodically (e.g. every 5th second) you could create a new Thread which sets the text of the button to the desired value and repaints it (if necessary).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文