Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building a Swing Interface

Similar presentations


Presentation on theme: "Building a Swing Interface"— Presentation transcript:

1 Building a Swing Interface
Dept. of CSIE, National University of Tainan 10/21/2012

2 Standard Dialog Boxes The four standard dialog boxes offered by JOptionPane are as follows: ConfirmDialog—Asks a question, with buttons for Yes, No, and Cancel responses InputDialog—Prompts for text input MessageDialog—Displays a message OptionDialog—Comprises all three of the other dialog box types

3 Confirm Dialog Boxes showConfirmDialog(Component, Object) method.
The Component argument specifies the container that should be considered to be the parent of the dialog box. The second argument, Object, can be a string, a component, or an Icon object. int response = JOptionPane.showConfirmDialog(null, “Should I delete all of your irreplaceable personal files?”); response can be YES_OPTION or NO_OPTION or CANCEL_OPTION.

4 Confirm Dialog Boxes showConfirmDialog(Component, Object, String, int, int). The first two arguments are the same as those in other showConfirmDialog() methods. The last three arguments are the following: A string that will be displayed in the dialog box’s title bar. An integer that indicates which option buttons will be shown; it should be equal to one of the class constants: YES_NO_CANCEL_OPTION or YES_NO_OPTION. An integer that describes the kind of dialog box it is, using the class constants ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE.

5 Confirm Dialog Boxes For example:
int response = JOptionPane.showConfirmDialog(null, “Error reading file. Want to try again?”, “File Input Error”, JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);

6 Input Dialog Boxes showInputDialog(Component, Object) method
String response = JOptionPane.showInputDialog(null, “Enter your name:”);

7 Message Dialog Boxes JOptionPane.showMessageDialog(null, “The program has been uninstalled.”);

8 Using Dialog Boxes import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*; public class FeedInfo extends JFrame { private JLabel nameLabel = new JLabel(“Name: “, SwingConstants.RIGHT); private JTextField name; private JLabel urlLabel = new JLabel(“URL: “, SwingConstants.RIGHT); private JTextField url; private JLabel typeLabel = new JLabel(“Type: “, private JTextField type;

9 Using Dialog Boxes public FeedInfo() { super(“Feed Information”); setSize(400, 105); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLookAndFeel(); // Site name String response1 = JOptionPane.showInputDialog(null, “Enter the site name:”); name = new JTextField(response1, 20); // Site address String response2 = JOptionPane.showInputDialog(null, “Enter the site address:”); url = new JTextField(response2, 20);

10 Using Dialog Boxes // Site type String[] choices = { “Personal”, “Commercial”, “Unknown” }; int response3 = JOptionPane.showOptionDialog(null, “What type of site is it?”, “Site Type”, 0, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]); type = new JTextField(choices[response3], 20); setLayout(new GridLayout(3, 2)); add(nameLabel); add(name); add(urlLabel);

11 Using Dialog Boxes add(url); add(typeLabel); add(type); setLookAndFeel(); setVisible(true); } private void setLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(this);

12 Using Dialog Boxes } catch (Exception e) { System.err.println(“Couldn’t use the system “ + “look and feel: “ + e); } } public static void main(String[] arguments) { FeedInfo frame = new FeedInfo(); }

13 Scroll Panes The following example creates a text area in a scroll pane and adds the scroll pane, scroller, to a container called mainPane: textBox = new JTextArea(7, 30); JScrollPane scroller = new JScrollPane(textBox); mainPane.add(scroller); JScrollPane(Object, int, int) constructor HORIZONTAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_NEVER VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_AS_NEEDED VERTICAL_SCROLLBAR_NEVER

14 Toolbars A toolbar, created in Swing with the JToolBar class, is a container that groups several components into a row or column. These components are most often buttons. Constructor methods include the following: JToolBar()—Creates a new toolbar JToolBar(int)—Creates a new toolbar with the specified orientation the orientation can be explicitly set with the HORIZONTAL or VERTICAL class variables of the SwingConstants interface

15 FeedBar.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FeedBar extends JFrame { public FeedBar() { super(“FeedBar”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create icons ImageIcon loadIcon = new ImageIcon(“load.gif”); ImageIcon saveIcon = new ImageIcon(“save.gif”); ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”); ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”);

16 FeedBar.java // create buttons JButton load = new JButton(“Load”, loadIcon); JButton save = new JButton(“Save”, saveIcon); JButton subscribe = new JButton(“Subscribe”, subscribeIcon); JButton unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon); // add buttons to toolbar JToolBar bar = new JToolBar(); bar.add(load); bar.add(save); bar.add(subscribe); bar.add(unsubscribe);

17 FeedBar.java // prepare user interface JTextArea edit = new JTextArea(8, 40); JScrollPane scroll = new JScrollPane(edit); BorderLayout bord = new BorderLayout(); setLayout(bord); add(“North”, bar); add(“Center”, scroll); pack(); setVisible(true); } public static void main(String[] arguments) { FeedBar frame = new FeedBar(); }

18 Menus JMenuItem—An item on a menu
JMenu—A drop-down menu that contains one or more JMenuItem components, other interface components, and separators, lines displayed between items JMenuBar—A container that holds one or more JMenu components and displays their names A JMenuItem component is like a button and can be set up using the same constructor methods as a JButton component. Call it with JMenuItem(String) for a text item, JMenuItem(Icon) for an item that displays a graphics file, or JMenuItem(String, Icon) for both.

19 Menus The following statements create seven menu items: JMenuItem j1 = new JMenuItem(“Open”); JMenuItem j2 = new JMenuItem(“Save”); JMenuItem j3 = new JMenuItem(“Save as Template”); JMenuItem j4 = new JMenuItem(“Page Setup”); JMenuItem j5 = new JMenuItem(“Print”); JMenuItem j6 = new JMenuItem(“Use as Default Message Style”); JMenuItem j7 = new JMenuItem(“Close”); A JMenu container holds all the menu items for a drop-down menu. After you have created a JMenu container, call its add(JMenuItem) to add a menu item to it.

20 Menus To add a line separator to the end of the menu, call the addSeparator() method. JMenu m1 = new JMenu(“File”); m1.add(j1); m1.add(j2); m1.add(j3); m1.addSeparator(); m1.add(j4); m1.add(j5); m1.add(j6); m1.add(j7);

21 Menus JMenuBar bar = new JMenuBar(); bar.add(m1); gui.setJMenuBar(bar); Example

22 FeedBar2.java public class FeedBar2 extends JFrame { public FeedBar2() { super(“FeedBar 2”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create icons ImageIcon loadIcon = new ImageIcon(“load.gif”); ImageIcon saveIcon = new ImageIcon(“save.gif”); ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”); ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”); // create buttons JButton load = new JButton(“Load”, loadIcon);

23 FeedBar2.java JButton save = new JButton(“Save”, saveIcon); JButton subscribe = new JButton(“Subscribe”, subscribeIcon); JButton unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon); // add buttons to toolbar JToolBar bar = new JToolBar(); bar.add(load); bar.add(save); bar.add(subscribe); bar.add(unsubscribe);

24 FeedBar2.java // create menu JMenuItem j1 = new JMenuItem(“Load”); JMenuItem j2 = new JMenuItem(“Save”); JMenuItem j3 = new JMenuItem(“Subscribe”); JMenuItem j4 = new JMenuItem(“Unsubscribe”); JMenuBar menubar = new JMenuBar(); JMenu menu = new JMenu(“Feeds”); menu.add(j1); menu.add(j2); menu.addSeparator(); menu.add(j3); menu.add(j4); menubar.add(menu);

25 FeedBar2.java // prepare user interface JTextArea edit = new JTextArea(8, 40); JScrollPane scroll = new JScrollPane(edit); BorderLayout bord = new BorderLayout(); setLayout(bord); add(“North”, bar); add(“Center”, scroll); setJMenuBar(menubar); pack(); setVisible(true); } public static void main(String[] arguments) { FeedBar2 frame = new FeedBar2(); }

26


Download ppt "Building a Swing Interface"

Similar presentations


Ads by Google