TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
//一个作画程序。 运行图如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChenWin29 extends JFrame implements ActionListener{
JPanel p;
CanvasPanel cp;
JLabel l1,l2;
JTextField t1,t2;
JButton ok;
int r=50, R=80;
public ChenWin29(){
cp=new CanvasPanel();
cp.setBorder(BorderFactory.createLoweredBevelBorder());
p=new JPanel();
l1=new JLabel("r");
l2=new JLabel("R");
t1=new JTextField("50",4);
t1.addActionListener(this);
t2=new JTextField("80",4);
t2.addActionListener(this);
ok=new JButton("OK");
ok.addActionListener(this);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(ok);
p.setBorder(BorderFactory.createTitledBorder("Control"));
getContentPane().add("South",p);
getContentPane().add(cp,BorderLayout.CENTER);
}
class CanvasPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
for(double a=0;a<=2*Math.PI;a+=Math.PI/18)
{
int x=(int)(r*Math.cos(a))+250;
int y=(int)(r*Math.sin(a))+150;
g.drawOval(x-R,y-R,2*R,2*R);
}
}
}
public void actionPerformed(ActionEvent e){
Object temp=e.getSource();
if(temp instanceof JButton){
if(temp==ok){
try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
System.out.println(r);
}catch(NumberFormatException ee){}
cp.repaint();
}
}
if(temp instanceof JTextField){
if(temp==t1){
try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
}catch(NumberFormatException ee){}
cp.repaint();
}
if(temp==t2){
try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
}catch(NumberFormatException ee){}
cp.repaint();
}
}
}
public static void main(String args[]){
ChenWin29 aa=new ChenWin29();
aa.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
aa.setSize(500,400);
aa.show();
}
} |
|