SEEM3460 Tutorial
description
Transcript of SEEM3460 Tutorial
![Page 1: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/1.jpg)
SEEM3460 Tutorial
GUI in Java
![Page 2: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/2.jpg)
GUI
Graphical User Interfaces
![Page 3: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/3.jpg)
Some Basic GUI Terms Component (Control in some languages)
the basic GUI unit something visible something that user can make action with
Container component that can hold other components a component hierarchy can be formed
Frame (Form in some languages) window that brings components to the display
![Page 4: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/4.jpg)
Typical Components Button Label TextField (TextBox) Panel List (ListBox) Choice (ComboBox) CheckBox CheckBoxGroup (RadioButton) Menu …
![Page 5: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/5.jpg)
Typical Properties of a GUI unit location size border (color, width, style) background color foreground color text font (name, size, style) components (only for containers) visible: determine whether it is displayed graphic: for custom drawing
![Page 6: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/6.jpg)
Action and Event Different components captures different actions
(e.g. a click, a key press, a mouse move) Actions or program statements raise events Event handler/action listener are classes
which contain methods (actionPerformed(Event) for Java) registered to be invoked when events are raised. This is called event handling
A component can register an action listener to handle all actions it captures
The flow of GUI programs are event-based
![Page 7: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/7.jpg)
Layout: Special from Java
Save programmer’s effort to arrange components by automatically setting the right size and position
Most typical Layout Managers: FlowLayout: Left to right GridLayout: N by M BorderLayout: N, E, S, W, Center
![Page 8: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/8.jpg)
Layout: Examples
![Page 9: SEEM3460 Tutorial](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813a17550346895da1f56f/html5/thumbnails/9.jpg)
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Counter extends JFrame {
private int count = 0; private JButton myButton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count);
public Counter () { setLocationRelativeTo(null); // disply to the middle of the screen setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myButton); //add components add(label);
myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count++; label.setText("Count: " + count); } }); pack(); setVisible(true); }
public static void main(String[] args) { new Counter();
} }
Example: Counter.java