我这样做是为了生成一个棋盘,并每隔10秒翻转瓷砖(切换颜色)10次。但是,图形要么不加载,要么不更新。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class ShapeTest extends JPanel implements ActionListener {
private static int FRAME_HEIGHT = 500;
private static int FRAME_WIDTH = 500;
private static JFrame frame = new JFrame();
Color color1 = Color.BLACK;
Color color2 = Color.WHITE;
javax.swing.Timer timer = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setAttributes();
}
});
}
public static void setAttributes() {
frame.add(new ShapeTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("FrameTest");
frame.pack();
frame.setVisible(true);
}
/* Constructor */
public ShapeTest() {
timer = new Timer(5000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (color1 == Color.BLACK) {
color1 = Color.WHITE;
color2 = Color.BLACK;
repaint();
} else {
color1 = Color.BLACK;
color2 = Color.WHITE;
repaint();
}
}
});
timer.start();
}
protected void painComponent(Graphics g) {
super.paintComponent(g);
for (int x = 0; x < FRAME_WIDTH; x = x + 10) {
for (int y = 0; y < FRAME_HEIGHT; y = y + 10) {
if (x % 20 == 0) {
g.setColor(color2);
g.fillRect(x, y, 10, 10);
y = y + 10;
g.setColor(color1);
g.fillRect(x, y, 10, 10);
} else {
g.setColor(color1);
g.fillRect(x, y, 10, 10);
y = y + 10;
g.setColor(color2);
g.fillRect(x, y, 10, 10);
}
}
}
}
public Dimension getPreferredSize() {
return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
这是根据下面的答案修改的代码,然而,它似乎仍然没有运行。
不要在Swing程序中使用线程sleep thread.sleep()。使用Swing Timer.
并全局定义Color
对象,以便可以从定时器和paintComponent
访问它。然后在计时器中重新绘制试试这个:
import java.awt.*;
import javax.swing.*;
public class ShapeTest extends JPanel {
private static int FRAME_HEIGHT = 500;
private static int FRAME_WIDTH = 500;
private static JFrame frame = new JFrame();
Color color1 = Color.BLACK; <-- Color objects
Color color2 = Color.WHITE;
javax.swing.Timer timer = null; <-- Declare Timer object
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
setAttributes();
}
});
}
public static void setAttributes() {
frame.add(new ShapeTest()); <-- Add the panel
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("FrameTest");
frame.pack(); <--- Pack.
frame.setVisible(true);
}
/* Constructor */
public ShapeTest(){
timer = new Timer(5000, new ActionListener(){ <-- "sleeps" 5 seconds (real word is "delay")
public void actionPerformed(ActionEvent e){
if (color1 == Color.BLACK){
color1 = Color.WHITE; <-- Alternate colors
color2 = Color.BLACK;
reapaint(); <-- repaint
} else {
color1 = Color.BLACK; <-- Alternate colors
color2 = Color.WHITE;
repaint();
}
}
});
timer.start(); <-- Start timer
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (int x = 0; x < FRAME_WIDTH; x = x + 10) {
for (int y = 0; y < FRAME_HEIGHT; y = y + 10) {
if (x % 20 == 0) {
g.setColor(color2); <-- Just us the color Object
g.fillRect(x, y, 10, 10);
y = y + 10;
g.setColor(color1);
g.fillRect(x, y, 10, 10);
} else {
g.setColor(color1);
g.fillRect(x, y, 10, 10);
y = y + 10;
g.setColor(color2);
g.fillRect(x, y, 10, 10);
}
}
}
}
public Dimension getPreferredSize(){
return new Dimension(FRAME_WIDTH, FRAME_HEIGHT); <-- preferred size of panel
}
}
两件事。
首先,您应该从JComponent#paintComponent
调用super.paintComponent
,以确保您不会得到任何绘画伪像,特别是因为JComponent
是透明的
其次,你应该先添加你的组件,然后向你展示框架。。。
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
addComponents();
setAttributes();
}
});
}
如果在框架可见后需要向其添加新组件,则可能需要调用revalidate
和repaint
以确保内容正确更新。。。