import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class javaguitest extends JPanel
{
private static int numClick = 0, num1 = 0, num2= 0, numadd = 0;
private static JLabel label1;
private static JComboBox bbox1;
public static void main(String[] args)
{
JFrame frame = new JFrame("Testing GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setResizable(false);
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 1, 8, 8));
main.setBorder(BorderFactory.createLineBorder(Colo r.black));
JPanel p1 = new JPanel();
JButton Exit = new JButton("Exit");
Exit.setPreferredSize(new Dimension(150, 30));
Exit.setBorder(BorderFactory.createLineBorder(Colo r.black));
p1.setBorder(BorderFactory.createLineBorder(Color. black));
Exit.addActionListener(new exitButtonListener());
p1.add(Exit);
JPanel p2 = new JPanel();
p2.setBorder(BorderFactory.createLineBorder(Color. black));
JButton Count = new JButton("Click to count");
JLabel label1 = new JLabel("Number of clicks: " + numClick);
label1.setBorder(BorderFactory.createLineBorder(Co lor.black));
Count.addActionListener(new countButtonListener());
p2.add(Count);
p2.add(label1);
p2.setBorder(BorderFactory.createLineBorder(Color. black));
JPanel p3 = new JPanel();
String[] box1 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
JComboBox bbox1 = new JComboBox(box1);
String[] box2 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
JComboBox bbox2 = new JComboBox(box2);
JLabel label2 = new JLabel("Box 1");
JLabel label3 = new JLabel("Box 2");
JLabel label4 = new JLabel("You have chosen " + num1 + " from Box 1 and chosen " + num2 + " from box 2");
JLabel label5 = new JLabel("The total of box 1 + box 2 is: " + numadd);
p3.add(label2);
p3.add(bbox1);
p3.add(label3);
p3.add(bbox2);
p3.add(label4);
p3.add(label5);
p3.setBorder(BorderFactory.createLineBorder(Color. black));
main.add(p1);
main.add(p2);
main.add(p3);
frame.add(main);
frame.pack();
frame.setSize(600, 530);
frame.setVisible(true);
}
public static class exitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
static class countButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
numClick++;
label1.setText("Number of Clicks: " + numClick);
}
}
static class dish2r implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int Selection;
Selection = bbox1.getSelectedIndex();
if (Selection == 0)
{
num1 = 0;
}
else if (Selection == 1)
{
num1 = 1;
numadd = numadd + num1;
}
else if (Selection == 2)
{
num1 = 2;
numadd = numadd + num1;
}
}
}
}