jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes JFrame panels NOT showing. WHY?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame panels NOT showing. WHY??" Watch "JFrame panels NOT showing. WHY??" New topic
Author

JFrame panels NOT showing. WHY??

E Hall
Greenhorn

Joined: Apr 14, 2005
Posts: 2
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);
}



}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

The panels are showing; they're just really boring. You create a lot of buttons and things, and three panels, and you add the panels to the frame, but you've neglected to add the buttons and things to the panels!

Note that the variable "frame" and the object you create for it are unnecessary; you probably already know that. The setVisible() calls for the panels are unnecessary, too.

We've got a whole forum devoted to Swing and suchlike things; I'm going to move this thread over there for any further discussion.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame panels NOT showing. WHY??