62,635
社区成员




private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i = 0; i < 5; i++){
jPanel1.repaint();
try {
Thread.sleep(500);
}
catch (InterruptedException e){
e.printStackTrace();
}
jPanel1.repaint();
}
class DrawPanel extends JPanel{
private Color c = null;
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (c == null || c == Color.blue)
c = Color.red;
else
c = Color.blue;
g.setColor(c);
g.fillRect(50, 50, 100, 100);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Test extends JPanel {
private Color[] colors = {Color.RED, Color.BLACK, Color.BLUE, Color.WHITE, Color.GREEN};
private int currentColorIndex;
public void randomPaint() {
Random random = new Random();
currentColorIndex = random.nextInt(colors.length);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(colors[currentColorIndex]);
g2d.fillRect(10, 10, 200, 200);
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
final Test test = new Test();
JButton startButton = new JButton("Start");
frame.getContentPane().add(startButton, BorderLayout.NORTH);
frame.getContentPane().add(test, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500, 500));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < 50; ++i) {
test.randomPaint();
Thread.sleep(1000);
}
} catch (Exception ex) { }
}
});
thread.start();
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}