📄 testrect.java
字号:
// TestRect.java: Demonstrate drawing rectangles
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class TestRect extends JFrame
{
// Default constructor
public TestRect()
{
setTitle("Show Rectangles");
getContentPane().add(new RectPanel());
}
// Main method
public static void main(String[] args)
{
TestRect frame = new TestRect();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,250);
frame.setVisible(true);
}
}
class RectPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Set new color
g.setColor(Color.red);
// Draw a rectangle
g.drawRect(30, 30, 100, 100);
// Draw a rounded rectangle
g.drawRoundRect(140, 30, 100, 100, 60, 30);
// Change the color to cyan
g.setColor(Color.cyan);
// Draw a 3D rectangle
g.fill3DRect(30, 140, 100, 100, true);
// Draw a raised 3D rectangle
g.fill3DRect(140, 140, 100, 100, false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -