java swing界面刷新

180浏览 · 2023-08-02 18:09:18
在 Java Swing 中,要刷新界面,你可以使用 `repaint()` 方法来触发重绘操作。`repaint()` 方法会调用组件的 `paintComponent()` 方法,从而重新绘制界面。 以下是一个简单的示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083),演示了如何刷新界面: ```java import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Graphics; public class [refresh](https://geek.csdn.net/educolumn/15b2f1f3252514d2f502f4993493744f?spm=1055.2569.3001.10083)Demo extends JPanel { private Color backgroundColor; public [refresh](https://geek.csdn.net/educolumn/15b2f1f3252514d2f502f4993493744f?spm=1055.2569.3001.10083)Demo() { this.backgroundColor = Color.WHITE; @Override protected void paintComponent(Graphics g) { su[per](https://geek.csdn.net/educolumn/2c6ed1be7edac16b3c9a0c3228eaff0c?spm=1055.2569.3001.10083).paintComponent(g); g.setColor(backgroundColor); g.fillRect(0, 0, [get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)Width(), getHeight()); public void setBackgroundColor(Color color) { this.backgroundColor = color; repaint(); // 刷新界面