• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JFrame panels NOT showing. WHY??

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}



}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic