标签:swing textfield draw exit visible 图片 dac result pac
package result;
//随机绘制多边形图案
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class F8 extends JFrame {
JButton j1 = new JButton("确认");
JTextField j2 = new JTextField(5);
public F8() {
super("多边形绘制");
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(new JLabel("请输入边数"));
add(j2);
add(j1);
MyCanvas m = new MyCanvas();
add(m);
j1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = Integer.parseInt(j2.getText());
int x[] = new int[n];
int y[] = new int[n];
for (int i = 0; i < n; i++) {
x[i] = (int) (Math.random() * 200);
y[i] = (int) (Math.random() * 200);
}
m.setPolygon(x, y, n);
m.repaint();
}
});
validate();
}
public static void main(String[] args) {
new F8();
}
}
class MyCanvas extends Canvas {
int N = 10;
int x[] = new int[N];
int y[] = new int[N];
public MyCanvas() {
setSize(400, 300);
setBackground(Color.cyan);
}
public void setPolygon(int[] x, int[] y, int N) {
this.N = N;
for (int i = 0; i < N; i++) {
this.x[i] = x[i];
this.y[i] = y[i];
}
}
public void paint(Graphics g) {
g.drawPolygon(x, y, N);
}
}
标签:swing textfield draw exit visible 图片 dac result pac
原文地址:https://www.cnblogs.com/nanfengnan/p/13679123.html