Results 1 to 3 of 3
Thread: Empty JFrame
- 03-25-2011, 03:06 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Empty JFrame
I'm developing a main menu for an application with two drop down menus and 5 normal control buttons. I set the buttons up first and had them displaying (and the exit button working) but when I added the combo boxes the JFrame just shows nothing. And I can't work out why.
Any help would be appreciated.Java Code:/* A rough working for a GUI interface */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // Import the GUI packages public class Layout { public static void main(String[] args) { JFrame window = new JFrame(); JPanel dropdowns = new JPanel(); JPanel menu = new JPanel(); JPanel content = new JPanel(); // Declare the JFrame and menu and content panels window.setLayout(new BorderLayout(10,10)); window.setVisible(true); window.setSize(600,200); window.setMinimumSize(new Dimension(600,200)); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the frame properties JComboBox to = new JComboBox(); to.addItem("Leicester"); to.addItem("Loughborough"); to.addItem("Nottingham"); to.addItem("Derby"); to.addItem("Lincoln"); JComboBox from = new JComboBox(); from.addItem("Leicester"); from.addItem("Loughborough"); from.addItem("Nottingham"); from.addItem("Derby"); from.addItem("Lincoln"); // Declare the drop down menus and add the options final JButton price = new JButton("Route Price"); final JButton time = new JButton("Route Time"); final JButton stops = new JButton("Route Stops"); final JButton admin = new JButton("Admin"); final JButton exit = new JButton("Exit"); // Declare the main menu buttons class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JButton clicked = (JButton) e.getSource(); if(clicked == exit) { System.exit(0); } // Get the JButton the user clicked } } // Create a class to handle the buttons ButtonHandler buttons = new ButtonHandler(); price.addActionListener(buttons); time.addActionListener(buttons); stops.addActionListener(buttons); admin.addActionListener(buttons); exit.addActionListener(buttons); // Call the ButtonHandler class and apply it to the buttons menu.setLayout(new FlowLayout()); menu.setVisible(true); // Set the menu properties dropdowns.setLayout(new FlowLayout()); dropdowns.setVisible(true); // Set the dropdowns properties dropdowns.add(to); dropdowns.add(from); menu.add(price); menu.add(time); menu.add(stops); menu.add(admin); menu.add(exit); window.add(dropdowns); window.add(menu); // Add the buttons to the menu, and add the menu to the frame } }Last edited by Wednesday Bass; 03-25-2011 at 03:15 PM.
- 03-25-2011, 04:19 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Or not. Got it solved by moving some of the code about. Not entirely sure why that worked.
- 03-25-2011, 07:30 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
By default a JFrame will use a BorderLayout (and you code also sets the layout be be a BorderLayout). When you don't specify where in the BorderLayout a component should be added, the component gets added to the CENTER.Not entirely sure why that worked.
You are trying to add two components to the CENTER which won't work. Read the section from the Swing tutorial on How to Use Border Layout for proper usage of this layout manager.Java Code:window.add(dropdowns); window.add(menu);
Similar Threads
-
Empty Form?
By Vorfin in forum New To JavaReplies: 13Last Post: 07-04-2010, 11:54 PM -
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
Empty ResultSet
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:36 PM -
BufferedReader empty
By Peter in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks