2
该程序有2个单选按钮。 1:一个圆圈,2:输出一个正方形。Java-使用AWT添加单选按钮?
这个程序基本上是设计为输出圆形或方形的单选按钮组。
我的问题是,我不知道如何在按钮上实现一个动作侦听器,以便输出形状。当我调用方法fm时,在初始化单选按钮时出现错误。显然我认为我需要一个主要方法。
请记住,这是java AWT。
如果您想要我添加更多细节或澄清请发表评论。
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet;
public class RadioButton extends Applet
implements ActionListener
{
int choice;
Frame fm = new Frame ("RadioButton Group");
Label la = new Label ("What shape do you want to draw?:");
fm.setLayout (new GridLayout (0, 1));
CheckboxGroup cg1 = new CheckboxGroup();
fm.add (la);
fm.add (new Checkbox ("CIRCLE", cg1, true));
fm.add (new Checkbox ("SQUARE", cg1, true));
fm.setSize (250, 200);
fm.setVisible (true);
fm.addWindowListener (new WindowAdapter()
{
public void paint (Graphics g) // How can you 'update the drawing' or repaint it?
{
switch (choice) // Maybe for colors if all else fails you can add a switch 'within' a switch. Inefficient-yes but helps.
{
case 1:
if (choice == 1)
g.fillOval (30, 40, 20, 20);
case 2:
if (choice == 2)
g.fillRect (20, 40, 20, 20);
break;
}
}
public void actionPerformed (ActionEvent evt)
{
if (evt.getSource() == "CIRCLE")
choice = 1;
else
choice = 2;
}
public void windowClosing (WindowEvent we)
{
System.exit (0);
}
}
);
}
}
我该如何解决这个问题? – 2013-03-06 00:49:34