1 / 21

Graphical User Interface

Graphical User Interface. IST 311 / MIS 307. GUI Feature. Tool tip – text displayed on a component when mouse over the item. May be used to describe the function JButton rightButton = new Jbutton (“Right”); rightButton.setToolTipText (“This is the Right button”);. GUI Feature.

Télécharger la présentation

Graphical User Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphical User Interface IST 311 / MIS 307

  2. GUI Feature • Tool tip – text displayed on a component when mouse over the item. • May be used to describe the function JButtonrightButton = new Jbutton(“Right”); rightButton.setToolTipText(“This is the Right button”);

  3. GUI Feature • Create borders around some objects • Titled border – creates a border that includes a title • Line border – creates a border around an object import javax.swing.border.*; panel1.setBorder(new TitledBorder(“Three Buttons”);

  4. JPanel • Divide a window into subcontainers, create JPanel’s • Jpanel’s may each contain their own layout JPanelmainPanel = new JPanel( ); mainPanel.setLayout(new GridLayout(4, 3));

  5. ItemEvents

  6. Basic GUI • ItemEvents • Menus, JCheckboxes, JRadioButtons, and other types of menus • Item events are handled by ItemListener interface which has one method itemStateChanged( )

  7. Basic GUI • JCheckboxes • A button that can be selected and deselected • Always displays it current state • Generate ItemEvents instead of ActionEvents • Must have a corresponding ItemListener • Objects which must be instantiated • Have corresponding labels

  8. Basic GUI private JCheckBox packages[ ] = new JCheckBox[NTITLES]; private String packageLabels[ ] = {"Package A12 - $42", "Package B12 - $39", "Package C12 - $34"}; for(int i = 0; i < packages.length; i++) { packages[i] = new JCheckBox(packageLabels[i]); packages[i].addItemListener(this); choicePanel.add(packages[i]); }

  9. Basic GUI • JRadioButton • A button that belongs to a group of mutually exclusive alternatives; only one button may be selected at a time • Generate ItemEvents and must have an ItemListener • Set the first button “on” as the default option • Radio buttons must be added to a ButtonGroup to enforce mutual exclusion

  10. Basic GUI private ButtonGroup optGroup = new ButtonGroup(); private JRadioButton options[ ] = new JRadioButton[NOPTIONS]; private String optionLabels[ ] = {"Credit Card", "Check", "Cash"}; for(int i = 0; i < options.length; i++) { options[i] = new JRadioButton(optionLabels[i]); options[i].addItemListener(this); optionPanel.add(options[i]); optGroup.add(options[i]); } options[0].setSelected(true);

  11. Basic GUI publicvoid itemStateChanged(ItemEvent e) { display.setText("Your order so far (Payment by: "); for(int i = 0; i < options.length; i++) { if (options[i].isSelected()) display.append(options[i].getText() + ")\n"); } for(int i = 0; i < packages.length; i++) { if (packages[i].isSelected()) display.append("\t" + packages[i].getText() + "\n"); } }

  12. Basic GUI • BoxLayout Manager • Can be used with any container • One-dimensional grid layout • Multiple components can be arranged vertically or horizontally in a row • This layout will not wrap around • Does not force all components to be the same size • Uses setLayout( ) method

  13. Basic GUI optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.Y_AXIS)); • BoxLayout( ) constructor has 2 parameters • First reference to container being managed • Second is constant to determine whether horizontal (X_AXIS) or vertical (Y_AXIS) alignment is used

  14. Basic GUI • Swing Borders • Border and BorderFactory classes • Used to put borders around GUI elements • Have titles, range of styles and colors • Attach a border to a component like a JPanel choicePanel.setBorder (BorderFactory.createTitledBorder("Packages")); • setBorder( ) method is defined in JComponent and inherited by all Swing components

  15. Scrollbars on Text Area JTextArea display = new JTextArea(10, 20 ); getContentPane( ).add(new JScrollPane(display));

  16. Menus

  17. Menus • Three steps to create a menu: • Create the individual JMenuItems • Create a JMenu and add the JMenuItems to it • Create a JMenuBar and add the JMenu to it

  18. Menus • JMenuBar is a horizontal list of names that appears at the top of a window JMenuBar menuBar = new JMenuBar( ); • JMenu is a clickable area on a menu bar that pops up and display the choices JMenu fileMenu = new JMenu(“File”);

  19. Menus • JMenuItem is each item on a menu JMenuItem clearMenu = new JMenuItem(“Clear”); • JSeparators may be used to draw a horizontal line between items on a menu for groupings

  20. Menus • Mnemonic key may be created for each menu • Shortcut to opening a menu • Appears underlined as part of the menu caption • Press mnemonic key and Alt key simultaneously fileMenu.setMnemonic(‘F’);

  21. Menus • Menu item click events require an ActionListener just like buttons use clearMenu.addActionListener(this); if(e.getSource( ) == clearMenu) …..

More Related