编写程序在窗体上输出如下图形。 * ** *** **** java
时间: 2023-11-14 22:10:10 浏览: 90
您可以使用以下代码在窗体上输出如下图形:
```
public class PrintTriangle extends JFrame {
public PrintTriangle() {
setTitle("Print Triangle");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new TrianglePanel());
}
public static void main(String[] args) {
PrintTriangle frame = new PrintTriangle();
frame.setVisible(true);
}
}
class TrianglePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 10;
int y = 30;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
g.drawString("*", x, y);
x += 10;
}
x = 10;
y += 20;
}
g.drawString("java", x, y);
}
}
```
阅读全文