I am fairly new to
Java. I cannot work out why Im getting a JFrame but none of the panels I created are showing. Can anyone help me out? This is what I have............
public class statecap extends JFrame implements ActionListener {
JTextField display = new JTextField(8);
JButton exit = new JButton("Exit");
JButton check = new JButton("Check Answer");
public statecap() {
super ("States & Capitals");
JFrame frame = new JFrame();
setSize(430,430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create first panel
JPanel pstates = new JPanel();
pstates.setLayout(new BoxLayout(pstates, BoxLayout.Y_AXIS));
JRadioButton cal = new JRadioButton("California");
cal.setMnemonic(KeyEvent.VK_1);
cal.setActionCommand("California, Sacremento");
cal.setSelected(true);
JRadioButton col = new JRadioButton("Colorado");
col.setMnemonic(KeyEvent.VK_2);
col.setActionCommand("Colorado, Denver");
JRadioButton ill = new JRadioButton("Illinois");
ill.setMnemonic(KeyEvent.VK_3);
ill.setActionCommand("Illinois, ");
JRadioButton ore = new JRadioButton("Oregon");
ore.setMnemonic(KeyEvent.VK_4);
ore.setActionCommand("Oregon, Salem");
JRadioButton wis = new JRadioButton("Wisconsin");
wis.setMnemonic(KeyEvent.VK_5);
wis.setActionCommand("Wisconsin");
pstates.setVisible(true);
//Group the radio buttons.
ButtonGroup cgroup = new ButtonGroup();
cgroup.add(cal);
cgroup.add(col);
cgroup.add(ill);
cgroup.add(ore);
cgroup.add(wis);
//Register a listener for the radio buttons.
cal.addActionListener(this);
col.addActionListener(this);
ill.addActionListener(this);
ore.addActionListener(this);
wis.addActionListener(this);
//create second panel
JPanel pcapitals = new JPanel();
pcapitals.setLayout(new BoxLayout(pcapitals, BoxLayout.Y_AXIS));
JRadioButton den = new JRadioButton("Denver", true);
den.setMnemonic(KeyEvent.VK_1);
den.setSelected(true);
JRadioButton mad = new JRadioButton("Madison");
den.setMnemonic(KeyEvent.VK_2);
JRadioButton sac = new JRadioButton("Sacramento");
den.setMnemonic(KeyEvent.VK_3);
JRadioButton sal = new JRadioButton("Salem");
den.setMnemonic(KeyEvent.VK_4);
JRadioButton spr = new JRadioButton("Springfield");
den.setMnemonic(KeyEvent.VK_5);
pcapitals.setVisible(true);
//button group creation
ButtonGroup sgroup = new ButtonGroup();
sgroup.add(den);
sgroup.add(mad);
sgroup.add(sac);
sgroup.add(sal);
sgroup.add(spr);
//create third panel
JPanel finalpanel = new JPanel();
finalpanel.setLayout(new BoxLayout(finalpanel, BoxLayout.Y_AXIS));
//create and add the items
check.addActionListener(this);
exit.addActionListener(this);
finalpanel.add(display);
finalpanel.add(check);
finalpanel.add(exit);
finalpanel.setVisible(true);
//put panels together
Container contentPane = getContentPane();
contentPane.add(pstates, BorderLayout.WEST);
contentPane.add(pcapitals, BorderLayout.CENTER);
contentPane.add(pcapitals, BorderLayout.EAST);
setVisible(true);
frame.show();
}
//event handler
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == check) {
display.setText(e.getActionCommand());
}
if (source == exit) {
System.exit(0);
}
}
public static void main(
String[] args) {
statecap
test = new statecap();
test.setVisible(true);
}
}
