📄 colorrect.java
字号:
//ColorRect
import java.awt.*;
import javax.swing.*;
public class ColorRect
{
public static void main(String[] args)
{
FillFrame frame = new FillFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class FillFrame extends JFrame
{
public FillFrame()
{
setTitle("颜色设置");
setSize(WIDTH, HEIGHT);
// 将panel加入到frame
FillPanel panel = new FillPanel();
//设置背景色为桌面颜色
panel.setBackground(SystemColor.desktop);
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 400;
public static final int HEIGHT = 250;
}
class FillPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//设置颜色,并绘制矩形、圆角矩形、椭圆
g.setColor(new Color(10, 10, 10));
g.drawRect(10, 10, 100, 30);
g.setColor(new Color(100, 100, 100));
g.drawRoundRect(150, 10, 100, 30, 15, 15);
g.setColor(new Color(150, 150, 150));
g.drawOval(280, 10, 80, 30);
//设置颜色,并填充矩形、圆角矩形、椭圆
g.setColor(new Color(10, 10, 10));
g.fillRect(10, 110, 100, 30);
g.setColor(new Color(100, 100, 100));
g.fillRoundRect(150, 110, 100, 30, 15, 15);
g.setColor(new Color(150, 150, 150));
g.fillOval(280, 110, 80, 30);
//设置颜色和字体,并显示文本信息
g.setColor(Color.white);
Font f = new Font("宋体", Font.BOLD + Font.ITALIC, 20);
g.setFont(f);
g.drawString("你好,Java!", 150, 200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -