java给框体加背景
2024-02-15 17:11:53
```java
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Background Example");
frame.setSize(, );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.PINK);
g.fillRect(, , this.getWidth(), this.getHeight());
}
};
frame.add(panel);
frame.setVisible(true);
}
}
```
上述代码创建了个新的JFrame,在这个框架中添加了个自定义的JPanel。这个自定义的面板覆盖了`paintComponent()`方法,在这里我司设置了背景颜色为粉红色。