210 likes | 233 Views
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.
E N D
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 JButtonrightButton = new Jbutton(“Right”); rightButton.setToolTipText(“This is the Right button”);
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”);
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));
Basic GUI • ItemEvents • Menus, JCheckboxes, JRadioButtons, and other types of menus • Item events are handled by ItemListener interface which has one method itemStateChanged( )
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
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]); }
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
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);
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"); } }
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
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
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
Scrollbars on Text Area JTextArea display = new JTextArea(10, 20 ); getContentPane( ).add(new JScrollPane(display));
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
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”);
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
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’);
Menus • Menu item click events require an ActionListener just like buttons use clearMenu.addActionListener(this); if(e.getSource( ) == clearMenu) …..