Im trying to make a GUI for a docking control program. It comprises of 4 text boxes at the top, then 8 buttons under these.
The text boxes have to be done using a BoxLayout, and the buttons using a GridBagLayout. They must then both be put in a JPanel.
Now here it the problem..
I have got the text boxes sorted, and have been doing a lot of reading on how to make GridBagLayouts. I think the code I have should have the layout made. I belive the problem is that the GridBagLayout is hiding somwhere behind the panel I wish to put it on.
I have tried a few ways of putting the GridBagLayout on the panel, but it isn't having any of it!
Any help would be great Thanks
Here is a mock up of the layout i want
----------------------------------------------------- | Text Box 1 | |---------------------------------------------------| | Text Box 2 | |---------------------------------------------------| | Text Box 3 | |---------------------------------------------------| | Text Box 4 | |---------------------------------------------------| | Button | Button | Button | Button | |---------------------------------------------------| | Button | Button | Button | |------------------------------| | | Button | | |---------------------------------------------------|
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; import java.applet.Applet; public class GUI extends Applet { JFrame frame; // TEXT FIELDS JTextField seaShipsID; JTextField queueShipsID; JTextField dockShipsID; JTextField shipMessages; // BUTTONS JButton dockFromSea; JButton dockFromQueue; JButton setSail; JButton queue; JButton admit; JButton dequeue; JButton refuse; JButton bye; // PANELS JPanel textFieldPanel; JPanel mainPanel; // GRID BAG LAYOUT GridBagLayout buttonLayout; GridBagConstraints buttonConstraints; public static void main(String[] args) { GUI gui = new GUI(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonLayout = new GridBagLayout(); buttonConstraints = new GridBagConstraints(); textFieldPanel = new JPanel(); textFieldPanel.setBackground(Color.lightGray); textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.Y_AXIS)); mainPanel = new JPanel(); mainPanel.setBackground(Color.lightGray); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)); // BUTTONS dockFromSea = new JButton("Dock From Sea"); dockFromQueue = new JButton("Dock From Queue"); setSail = new JButton("Set Sail"); queue = new JButton("Queue"); admit = new JButton("Admit"); dequeue = new JButton("Dequeue"); refuse = new JButton("Refuse"); bye = new JButton("Bye"); // BUTTON LAYOUT buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dockFromSea, 0,0,2,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(queue, 0,2,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(admit, 0,3,1,1); buttonConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent(admit, 0,4,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dockFromQueue, 1,0,2,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dequeue, 1,2,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(refuse, 1,3,2,2); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(setSail, 2,1,3,1); // TEXT FIELDS seaShipsID = new JTextField("ID of boats at sea"); queueShipsID = new JTextField("ID of boats in queue to dock"); dockShipsID = new JTextField("ID of the boats in dock"); shipMessages = new JTextField("No Message"); // TEXT FIELD PANEL IMPLEMENTATION frame.getContentPane().add(BorderLayout.NORTH, textFieldPanel); textFieldPanel.add(seaShipsID); textFieldPanel.add(queueShipsID); textFieldPanel.add(dockShipsID); textFieldPanel.add(shipMessages); // MAIN PANEL IMPLEMENTATION frame.getContentPane().add(BorderLayout.NORTH, mainPanel); mainPanel.add(textFieldPanel); frame.setSize(300,200); frame.setVisible(true); } private void addComponent(Component component, int row, int column, int width, int height) { buttonConstraints.gridx = column; buttonConstraints.gridy = row; buttonConstraints.gridwidth = width; buttonConstraints.gridheight = height; buttonLayout.setConstraints(component, buttonConstraints); add(component); } }