正如许多优秀的 Stackoverflow 用户所说,这里的正确想法是使用javax.swing.TImer。这是一个小代码片段,可以为您提供帮助。请提出任何超出您能力范围的问题,我也会尝试提供信息。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class DrawStringWithTimer
{
private final int WIDTH = 400;
private final int HEIGHT = 300;
private Timer timer;
private int x;
private int y;
private int counter;
private Random random;
private String[] messages = {
"Bingo, I am ON",
"Can you believe this !!",
"What else you thinking about ?",
"Ahha, seems like you are confused now !!",
"Lets Roll and Conquer :-)"
};
private CustomPanel contentPane;
private ActionListener timerAction = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
if (counter == 5)
counter = 0;
/*
* Creating random numbers where
* x will be equal to zero or
* less than WIDTH and y will be
* equal to zero or less than HEIGHT.
* And we getting the value of the
* messages Array with counter variable
* and passing this to setValues function,
* so that it can trigger a repaint()
* request, since the state of the
* object has changed now.
*/
x = random.nextInt(WIDTH);
y = random.nextInt(HEIGHT);
contentPane.setValues(x, y, messages[counter]);
counter++;
}
};
public DrawStringWithTimer()
{
counter = 0;
x = y = 10;
random = new Random();
}
private void displayGUI()
{
JFrame frame = new JFrame("Drawing String Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new CustomPanel(WIDTH, HEIGHT);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
/*
* javax.swing.Timer is what you need
* when dealing with Timer related
* task when using Swing.
* For more info visit the link
* as specified by @trashgod, in the
* comments.
* Two arguments to the constructor
* specify as the delay and the
* ActionListener associated with
* this Timer Object.
*/
timer = new Timer(2000, timerAction);
timer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new DrawStringWithTimer().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private final int GAP = 10;
private int width;
private int height;
private int x;
private int y;
private String message = "";
public CustomPanel(int w, int h)
{
width = w;
height = h;
setOpaque(true);
setBackground(Color.WHITE);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
}
public void setValues(int x, int y, String msg)
{
this.x = x;
this.y = y;
message = msg;
/*
* As the state of the variables will change,
* repaint() will call the paintComponent()
* method indirectly by Swing itself.
*/
repaint();
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(width, height));
}
@Override
protected void paintComponent(Graphics g)
{
/*
* Below line is used to draw the JPanel
* in the usual Java way first.
*/
super.paintComponent(g);
/*
* This line is used to draw the dynamic
* String at the given location.
*/
g.drawString(message, x, y);
}
}