某小应用程序的界面有两个按钮,点击“画圆”按钮,程序在窗口画一个圆,点击“画矩形”按钮,程序在窗口画一个矩形。
import java.applet.*; import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class Class l extends Applet implements ActionListener {
boolean c = false; int r = 50,a = 50, b = 50;
JButton bl, b2;
public void init(){
setSize(200, 200); setBackground(Color.blue);
b1 = new JButton("画 圆"); b2 = new JButton("画矩形");
b1.addActionListener(this); b2.addActionListener(this);
add(b1); add(b2); setVisible(true);
}
public void (Graphics g){
g.clearRect(70, 100, 130, 200);
g.setColor(Color. red);
if(c) g.drawRoundRect(70, 100, r, r, r, r);
else g.drawRect(70, 100, a, b);
}
public void update(Graphics g){ paint(g); }
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1) { c = true; }
else if(e.getSource()==b2){ c = false; }
;
}
}