这是一个实现简单GUI的程序。
我通常会使用setVisible(true)
替换show方法但是,当show()
方法接受参数时,我不确定如何处理此问题,我已尝试{{1}但是我的编译器没有打动。在此先感谢
@SuppressWarnings("deprecated")
GUI对象类
import java.awt.*;
import java.awt.event.*;
声明Frame类的对象
public class UI2 extends Frame
{
构造
public Button b;
public TextField t;
public FirstPanel p1;
public SecondPanel p2;
Choice c;
FirstPanel类
UI2()
{
setLayout(new GridLayout(2,1));
p1 = new FirstPanel(this);
add(p1);
p2 = new SecondPanel(this);
add(p2);
validate();
}
}
创建GUI方法
class FirstPanel extends Panel implements ActionListener
{
UI2 myUI;
public PopupMenu pm1;
public PopupMenu pm2;
public MenuItem pm1_1, pm1_2, pm2_1, pm2_2;
FirstPanel(UI2 myUI)
{
this.myUI = myUI;
createTheGUI();
}
选择菜单项时将调用此方法
public void createTheGUI()
{
pm1 = new PopupMenu("First Menu");
pm2 = new PopupMenu("Second Menu");
pm1_1 = new MenuItem("First Menu First Choice");
pm1_2 = new MenuItem("First Menu Second Choice");
pm2_1 = new MenuItem("Second Menu First Choice");
pm2_2 = new MenuItem("Second Menu Second Choice");
pm1_1.addActionListener(this);
pm1_2.addActionListener(this);
pm2_1.addActionListener(this);
pm2_2.addActionListener(this);
pm2_2.setEnabled(false);
pm1.add(pm1_1);
pm1.add(pm1_2);
pm2.add(pm2_1);
pm2.add(pm2_2);
pm1.addActionListener(this);
pm2.addActionListener(this);
add(pm1);
add(pm2);
}
一旦myUI对象被实例化就被调用
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source instanceof MenuItem)
{
handleMenuItems(e, (MenuItem)source);
}
}
public void handleMenuItems(ActionEvent e, MenuItem source)
{
String selected = source.getLabel();
System.out.println("String Selected is: " + selected);
}
}