GUI building with SwingHow to build a GUIImport the necessary packagesMake a ContainerAdd a layout managerAdd components to containersCreate componentsBorderLayoutFlowLayoutGridLayoutBoxLayoutNested layoutsAn example nested layoutCreate and attach listenersUsing an anonymous inner classSuggested program arrangement 1Suggested program arrangement 2Components use various listenersGetting valuesEnabling and disabling componentsDialogsMessage dialogsConfirm dialogsInput dialogsOption dialogsLoad file dialogsSave file dialogsQuitting the programThe EndJan 14, 2019GUI building with Swing2How to build a GUICreate a window in which to display things—usually a JFrame (for an application), or a JAppletUse the setLayout(LayoutManager manager) method to specify a layout managerCreate some Components, such as buttons, panels, etc.Add your components to your display area, according to your chosen layout managerWrite some Listeners and attach them to your ComponentsInteracting with a Component causes an Event to occurA Listener gets a message when an interesting event occurs, and executes some code to deal with itDisplay your window3Import the necessary packagesThe Swing components are in javax.swing.*, so you always need to import that for a Swing applicationSwing is built on top of AWT and uses a number of AWT packages, including most of the layout managers, so you need to import java.awt.*Most listeners also come from the AWT, so you also need to import java.awt.event.*A few listeners, such as DocumentListener and ListSelectionListener, are specific to Swing, so you may need to import javax.swing event.*For more complex GUIs, there are additional java.awt.something and javax.swing.something packages that you may need to import4Make a ContainerFor an application, your container is typically a JFrameJFrame frame = new JFrame();JFrame frame = new JFrame("Text to put in title bar");You can create a JFrame in your “main class”It’s often more convenient to have your “main class” extend JFrameFor an applet, your “main class” must extend JAppletOnce your application or applet is up and running, it create and display various dialogs5Add a layout managerThe most important layout managers are:BorderLayoutProvides five areas into which you can put componentsThis is the default layout manager for both JFrame and JAppletFlowLayoutComponents are added left to right, top to bottomGridLayoutComponents are put in a rectangular gridAll areas are the same size and shapeBoxLayoutCreates a horizontal row or a vertical stackThis can be a little weird to useGridBagLayoutToo complex and a danger to your sanitySpringLayoutNew, very flexible, I don’t have experience with this one yet6Add components to containersThe usual command is container.add(component);For FlowLayout, GridLayout, and BoxLayout, this adds the component to the next available locationFor BorderLayout, this puts the component in the CENTER by defaultFor BorderLayout, it’s usually better to use container.add(component, BorderLayout.position);position is one of NORTH, SOUTH, EAST, WEST, or CENTER7Create componentsJButton button = new JButton("Click me!");JLabel label = new JLabel("This is a JLabel");JTextField textField1 = new JTextField("This is the initial text"); JTextField textField2 = new JTextField("Initial text", columns);JTextArea textArea1 = new JTextArea("Initial text");JTextArea textArea2 = new JTextArea(rows, columns);JTextArea textArea3 = new JTextArea("Initial text", rows, columns);JCheckBox checkbox = new JCheckBox("Label for checkbox");JRadioButton radioButton1 = new JRadioButton("Label for button");ButtonGroup group = new ButtonGroup();group.add(radioButton1); group.add(radioButton2); etc.This is just a sampling of the available constructors; see the javax.swing API for all the rest8BorderLayoutpublic class BorderLayoutExample extends JApplet { public void init () { setLayout(new BorderLayout ()); add(new JButton("One"), BorderLayout.NORTH); add(new JButton("Two"), BorderLayout.WEST); add(new JButton("Three"), BorderLayout.CENTER); add(new JButton("Four"), BorderLayout.EAST); add(new JButton("Five"), BorderLayout.SOUTH); add(new JButton("Six")); }}9FlowLayoutpublic class FlowLayoutExample extends JApplet { public void init () { setLayout(new FlowLayout ()); add(new JButton("One")); add(new JButton("Two")); add(new JButton("Three")); add(new JButton("Four")); add(new JButton("Five")); add(new JButton("Six")); }}10GridLayoutpublic class GridLayoutExample extends JApplet { public void init() { setLayout(new GridLayout(2, 4)); add(new JButton("One")); add(new JButton("Two")); add(new JButton("Three")); add(new JButton("Four")); add(new JButton("Five")); }}11BoxLayoutpublic class BoxLayoutExample extends JApplet { public void init () { Box box = new Box(BoxLayout.Y_AXIS); add(box); box.add(new JButton("One")); box.add(new JButton("Two")); box.add(new JButton("Three")); box.add(new JButton("Four")); box.add(new JButton("Five")); box.add(new JButton("Six")); }}12Nested layoutsA JPanel is both a JContainer and a ComponentBecause it’s a container, you can put other components into itBecause it’s a component, you can put it into other containersAll but the very simplest GUIs are built by creating several JPanels, arranging them, and putting components (possibly other JPanels) into themA good approach is to draw (on paper) the arrangement you want, then finding an arrangement of JPanels and their layout managers that accomplishes this13An example nested layoutContainer container = new JFrame() or JApplet(); JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout());p1.add(new JButton("A"), BorderLayout.NORTH); // also add buttons B, C, D, EJPanel p2 = new JPanel();p2.setLayout(new GridLayout(3, 2));p2.add(new JButton("F")); // also add buttons G, H, I, J, KJPanel p3 = new JPanel();p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));p3.add(new JButton("L")); // also add buttons M, N, O, P container.setLayout(new BorderLayout()); container.add(p1, BorderLayout.CENTER); container.add(p2, BorderLayout.SOUTH); container.add(p3, BorderLayout.EAST);14Create and attach listenersJButton okButton = new
View Full Document