在 Swing 中,您不负责绘画,该工作属于RepaintManager
. 它根据许多因素决定绘画的内容和时间。
执行自定义绘画的推荐机制是创建一个自定义类,从类似的东西扩展JPanel
并覆盖它的paintComponent
方法。如果您想更新组件,您可以通过调用组件的repaint
方法请求重新绘制组件。
别搞错了,这只是一个请求。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class QuickPaint {
public static void main(String[] args) {
new QuickPaint();
}
public QuickPaint() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
private int paints = 0;
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
paints++;
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.drawLine(width / 2, 0, width / 2, height);
g.drawLine(0, height / 2, width, height / 2);
g.drawLine(0, 0, width, height);
g.drawLine(0, height, width, 0);
String text = "Repainted " + paints + " times";
FontMetrics fm = g.getFontMetrics();
int x = (width - fm.stringWidth(text)) / 2;
int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
}
}
}
仔细查看在 AWT 和 Swing中执行自定义绘画和绘画以了解更多详细信息...