展开全部
代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JFrame {
public App() {
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel topBar = new JPanel();
topBar.setLayout(new FlowLayout());
this.add(topBar, BorderLayout.NORTH);
JPanel canvas = new JPanel();
this.add(canvas, BorderLayout.CENTER);
JButton btnTriangle = new JButton("三角形");
btnTriangle.addActionListener(e -> {
Graphics g = canvas.getGraphics();
g.setColor(Color.RED);
Polygon polygon = new Polygon();
polygon.addPoint(10, 10);
polygon.addPoint(10, 50);
polygon.addPoint(100, 50);
g.fillPolygon(polygon);
});
topBar.add(btnTriangle);
JButton btnRectangle = new JButton("矩形");
btnRectangle.addActionListener(e -> {
Graphics g = canvas.getGraphics();
g.setColor(Color.RED);
g.fillRect(150, 10, 100, 50);
});
topBar.add(btnRectangle);
JButton btnCircle = new JButton("椭圆");
btnCircle.addActionListener(e -> {
Graphics g = canvas.getGraphics();
g.setColor(Color.GREEN);
g.fillOval(10, 100, 250, 90);
});
topBar.add(btnCircle);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |