Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created June 1, 2009 20:04
Show Gist options
  • Save twopoint718/121709 to your computer and use it in GitHub Desktop.
Save twopoint718/121709 to your computer and use it in GitHub Desktop.
import java.awt.Container; //import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Menu extends JApplet {
private static final long serialVersionUID = 1L;
private String[] drinks = { "water", "soda", "fruit juice" };
private String[] plates = { "sushi", "pasta", "salad" };
private String[] desserts = { "cake", "ice cream", "pastry" };
private JTextField t = new JTextField(15);
private JTextField t1 = new JTextField(15);
private JTextField t2 = new JTextField(15);
private JComboBox c = new JComboBox();
private JComboBox c1 = new JComboBox();
private JComboBox c2 = new JComboBox();
private JButton b = new JButton("Add items");
private int count = 0;
private int count1 = 0;
private int count2 = 0;
public void init() {
for (int i = 0; i < 3; i++)
c.addItem(drinks[count++]);
t.setEditable(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (count < drinks.length)
c.addItem(drinks[count++]);
}
});
for (int j = 0; j < 3; j++)
c1.addItem(plates[count1++]);
t1.setEditable(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (count < plates.length)
c1.addItem(plates[count1++]);
}
});
for (int k = 0; k < 3; k++)
c2.addItem(desserts[count2++]);
t2.setEditable(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (count < desserts.length)
c2.addItem(desserts[count2++]);
}
});
// link the combo boxes to the corresponding text fields
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText((String) c.getSelectedItem());
}
});
c1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t1.setText((String) c1.getSelectedItem());
}
});
c2.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
t2.setText((String) c2.getSelectedItem());
}
});
// link some kind of action to pressing the button
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame("Alert");
f.add(new JLabel("You said: " + c.getSelectedItem() + ", " +
c1.getSelectedItem() + ", " + c2.getSelectedItem()));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
});
Container cp = getContentPane();
cp.setLayout(new GridLayout(4, 2, 5, 5));
// lay these out in a grid (optional)
cp.add(c); cp.add(t);
cp.add(c1); cp.add(t1);
cp.add(c2); cp.add(t2);
cp.add(b);
}
public static void main(String[] args) {
run(new Menu(), 200, 125);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment