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.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
}
}
