This is a program that implements a simple GUI.
I usually replace the show Method with setVisible(true)
however, I'm not sure how to handle this problem when the show()
method takes arguments, I've tried @SuppressWarnings("deprecated")
but my compiler was unimpressed. Thanks in Advance
import java.awt.*;
import java.awt.event.*;
GUI object class
public class UI2 extends Frame
{
Declaring objects of Frame class
public Button b;
public TextField t;
public FirstPanel p1;
public SecondPanel p2;
Choice c;
Constructor
UI2()
{
setLayout(new GridLayout(2,1));
p1 = new FirstPanel(this);
add(p1);
p2 = new SecondPanel(this);
add(p2);
validate();
}
}
FirstPanel class
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();
}
Create the GUI Method
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);
}
This method will be invoked when a menu item is selected
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);
}
}
called once the myUI object is instantiated
void createTheGUI()
{
b = new Button("Click to see a Popup Menu");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
myUI.p1.pm1.show(myUI.p1, 0, 0);
myUI.p1.pm2.show(myUI.p1, 180, 0);
}