📄 exercise11_9.java
字号:
// Exercise11_9.java: Draw function
import java.awt.*;
import javax.swing.*;
public class Exercise11_9 extends JFrame {
public Exercise11_9() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new DrawPyramid(), BorderLayout.CENTER);
}
public static void main(String[] args) {
Exercise11_9 frame = new Exercise11_9();
frame.setSize(400, 400);
frame.setTitle("Exercise11_9");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Inner class
class DrawPyramid extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setFont(new Font("SansSerif", Font.BOLD, 17));
int width = getWidth();
int height = getHeight();
int xInterval = 20;
int yInterval = 20;
int x = 10;
int y = 10;
for (int row = 1; row <= 1 + (height - 20) / 20; row++) {
for (int column = 1; column < row; column++) {
g.drawString(column + "", x, y);
x += 20;
}
y += 20;
x = 20;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -