Java: Creating Basic Gui
This is a short example of how to create a basic Swing gui application in Java. (The tip consists mainly of the code shown below.) This example demonstrates a border layout and uses buttons inside of panels to fill in the layout. To build an application from this, simply replace the buttons with gui components of your choosing.
You will notice a method is used to create and launch a frame. We do not extend JFrame. In theory, by not doing this, it allows us to inherit from another object thus giving us more flexibility. Well, in theory anyway.
I initialize components as attributes of the class. Since Swing components used to build the UI do not change, this seems like a good place to define them. The constructor is then used to assemble the components you have defined.
Sample Output
BasicGui.java
1:import javax.swing.*; 2:import javax.swing.event.*; 3:import java.awt.*; 4:import java.awt.event.*; 5:/** 6: This class demonstrates the basics of setting up a Java Swing GUI uisng the 7: BorderLayout. You should be able to use this program to drop in other 8: components when building a GUI 9:*/ 10:public class BasicGui{ 11: // Initialize all swing objects. 12: private JFrame f = new JFrame("Basic GUI"); //create Frame 13: private JPanel pnlNorth = new JPanel(); // North quadrant 14: private JPanel pnlSouth = new JPanel(); // South quadrant 15: private JPanel pnlEast = new JPanel(); // East quadrant 16: private JPanel pnlWest = new JPanel(); // West quadrant 17: private JPanel pnlCenter = new JPanel(); // Center quadrant 18: 19: // Buttons some there is something to put in the panels 20: private JButton btnNorth = new JButton("North"); 21: private JButton btnSouth = new JButton("South"); 22: private JButton btnEast = new JButton("East"); 23: private JButton btnWest = new JButton("West"); 24: private JButton btnCenter = new JButton("Center"); 25: 26: // Menu 27: private JMenuBar mb = new JMenuBar(); // Menubar 28: private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar 29: private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item 30: private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry 31: private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry 32: 33: /** Constructor for the GUI */ 34: public BasicGui(){ 35: // Set menubar 36: f.setJMenuBar(mb); 37: 38: //Build Menus 39: mnuFile.add(mnuItemQuit); // Create Quit line 40: mnuHelp.add(mnuItemAbout); // Create About line 41: mb.add(mnuFile); // Add Menu items to form 42: mb.add(mnuHelp); 43: 44: // Add Buttons 45: pnlNorth.add(btnNorth); 46: pnlSouth.add(btnSouth); 47: pnlEast.add(btnEast); 48: pnlWest.add(btnWest); 49: pnlCenter.add(btnCenter); 50: 51: // Setup Main Frame 52: f.getContentPane().setLayout(new BorderLayout()); 53: f.getContentPane().add(pnlNorth, BorderLayout.NORTH); 54: f.getContentPane().add(pnlSouth, BorderLayout.SOUTH); 55: f.getContentPane().add(pnlEast, BorderLayout.EAST); 56: f.getContentPane().add(pnlWest, BorderLayout.WEST); 57: f.getContentPane().add(pnlCenter, BorderLayout.CENTER); 58: 59: // Allows the Swing App to be closed 60: f.addWindowListener(new ListenCloseWdw()); 61: 62: //Add Menu listener 63: mnuItemQuit.addActionListener(new ListenMenuQuit()); 64: } 65: 66: public class ListenMenuQuit implements ActionListener{ 67: public void actionPerformed(ActionEvent e){ 68: System.exit(0); 69: } 70: } 71: 72: public class ListenCloseWdw extends WindowAdapter{ 73: public void windowClosing(WindowEvent e){ 74: System.exit(0); 75: } 76: } 77: 78: public void launchFrame(){ 79: // Display Frame 80: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 81: f.pack(); //Adjusts panel to components for display 82: f.setVisible(true); 83: } 84: 85: public static void main(String args[]){ 86: BasicGui gui = new BasicGui(); 87: gui.launchFrame(); 88: } 89:}