importjava.awt.*;importjava.awt.event.*;publicclasstuxingextendsFrameimplementsActionListener{Panelp,p1,p2,p3;CheckboxGroupg1;Checkboxr1,r2;Buttonb1,b2,b3,b4;publicvoidtu...
import java.awt.*;
import java.awt.event.*;
public class tuxing extends Frame implements ActionListener
{
Panel p,p1,p2,p3;
CheckboxGroup g1;
Checkbox r1,r2;
Button b1,b2,b3,b4;
public void tuxing()
{Frame f=new Frame();
p=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
g1=new CheckboxGroup();
r1=new Checkbox("圆",false,g1);
r2=new Checkbox("矩形",false,g1);
b1=new Button("上移");b1.addActionListener(this);
b2=new Button("下移");b2.addActionListener(this);
b3=new Button("左移");b3.addActionListener(this);
b4=new Button("右移");b4.addActionListener(this);
p1.add(r1);p1.add(r2);
p3.add(b1);p3.add(b2);p3.add(b3);p3.add(b4);
p.setLayout(new BorderLayout());
p.add("north",p1);p.add("center",p2);p.add("south",p3);
p.setSize(300,300);
p.setVisible(true);
f.add(p);
}
public void paint(Graphics g)
{
g=p2.getGraphics();
if (r1.getState())
g.drawOval(50,50,30,30);
if(r2.getState())
g.drawRect(50, 50, 50, 50);
}
public static void main(String args[])
{
new tuxing();
}
@SuppressWarnings("null")
public void actionPerformed(ActionEvent e)
{
if(r1.getState()&&e.getSource()==b1)
{
Graphics g = null;
g.clearRect(50,50,30,30);
g.drawOval(50,30,30,30);
}
if(r1.getState()&&e.getSource()==b2)
{
Graphics g = null;
g.clearRect(50,50,30,30);
g.drawOval(50,70,30,30);
}
if(r1.getState()&&e.getSource()==b3)
{
Graphics g = null;
g.clearRect(50,50,30,30);
g.drawOval(30,50,30,30);
}
if(r1.getState()&&e.getSource()==b4)
{
Graphics g = null;
g.clearRect(50,50,30,30);
g.drawOval(70,50,30,30);
}
if(r2.getState()&&e.getSource()==b1)
{
Graphics g = null;
g.clearRect(50,50,50,50);
g.drawRect(50,30,50,50);
}
if(r2.getState()&&e.getSource()==b2)
{
Graphics g = null;
g.clearRect(50,50,50,50);
g.drawRect(50,70,50,50);
}
if(r2.getState()&&e.getSource()==b3)
{
Graphics g = null;
g.clearRect(50,50,50,50);
g.drawRect(30,50,50,50);
}
if(r2.getState()&&e.getSource()==b4)
{
Graphics g = null;
g.clearRect(50,50,50,50);
g.drawRect(70,50,50,50);
}
}
}
题目要求用面板完成,当选择圆时,在面板中画圆,选择矩形时画矩形,下面是四个按钮,分别是上下左右四个方向移动,要求能够完成相应的功能。
展开