

















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































More GUI JAR file Javadoc More advanced topic XML parser SQL access
To become familiar with common UI components such as menus, combo boxes, buttons, text To understand the use of layout manager to arrange components To build programs that handles events from UI components How to use Java document
Event: user input, click button, mouse move, hit keyboard…etc. Java window manager sends a notification to the program that an Event occurred. Huge number of events Program has no interest in most of them Should not be flooded by the boring events Program must indicate which events it likes to receive: event listener
EventObject AWTEvent ActionEvent ComponetEvent InputEvent WindowEvent MouseEvent KeyEvent Event Classes EventObject has getSource() method, which returns the object that generated this event. The subclasses have other methods that describe the event further
A listener must implement all the methods in the corresponding listener interface Sometimes too tedious Provide Adapter class so that user don’t need to define every method, only the one interested in public interface MouseListener{ void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); } public class MouseAdapter implements MouseListener{ //define all the five methods above, but all do nothing } Class MyMouseListener extends MouseAdapter { … }
Border layout Content pane Flow layout: simply arranges its components in a row and starts a new row when there is no more room JPanel by default is flow layout panel.setLayout(new BorderLayout()); Grid layout: arrange components in a grid with fixed number of rows and columns, each has same size panel.setLayout(new GridLayout(4, 3)); More: BoxLayout, GridBagLayout, SpringLayout
Grid Layout
Can be constructed with a label, an icon or both new JButton(“Next”); new JButton(new ImageIcon(“next.gif”); new JButton(“Next”, new ImageIcon(“next.gif”); Listener needs to implements ActionListener interface, actionPerformed() method If multiple buttons use the same listener, need to use getSource() method to use which one been clicked When multiple buttons do similar task
JTextField: a single line of text Specify number of characters in constructor Hitting Enter key generates ActionEvent setEditable(false) makes it only for display setText() to set the content Usually putting a JLabel next to it JTextArea: multiple lines of text Specify number of rows and columns in constructor Hitting Enter key just start a new line. Need to add a button to generate the event
To create a set of radio buttons, first create each button individually, then add to the set The location of each button is not necessarily be close to each other setSelected(true) to turn on a button isSelected() to find out whether a button is on or off JRadioButton smallButton = new JRadioButton(“Small”); JRadioButton mediumButton = new JRadioButton(“Medium”); ButtonGroup sizeGroup = new ButtonGroup(); sizeGroup.add(smallButton); sizeGroup.add(mediumButton);
Check boxes are separate from each other JCheckBox check = new JCheckBox(“bold”); Do not place check boxes inside a button group
You can add a border to a panel to make it visible Many times of border Can add border to any component, but usually to panel Borders can be layered also. JPanel myPanel = new JPanel(); myPanel.setBorder(new EtchedBorder()); myPanel.setBorder(new TitledBorder(new EtchedBorder(), “size”);
ChoiceTest.java