Implementing GUIs in Java
description
Transcript of Implementing GUIs in Java
![Page 1: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/1.jpg)
Implementing GUIs in Java
● The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs:– Abstract Window Toolkit (AWT): native GUI
components– Swing: lightweight GUI components– 2D: rendering two-dimensional shapes, text, and
images– Accessibility: allowing compatibility with, for
example, screen readers and screen magnifiers
![Page 2: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/2.jpg)
Abstract Window Toolkit (AWT)
● Provides basic UI components:– Buttons, lists, menus, textfields, etc– Event handling mechanism– Clipboard and data transfer– Image manipulation– Font manipulation– Graphics
● Platform independence is achieved through peers, or native GUI components
![Page 3: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/3.jpg)
AWT Packages
java.awt Basic component functionalityjava.awt.accessibility Assistive technologiesjava.awt.color Colors and color spacesjava.awt.datatransfer Clipboard and data transfer supportjava.awt.dnd Drag and dropjava.awt.event Event classes and listenersjava.awt.font 2D API font packagejava.awt.geom 2D API geometry packagejava.awt.im Input methodsjava.awt.image Fundamental image manipulation classesjava.awt.peer Peer interfaces for component peersjava.awt.print 2D API support for printingjava.awt.swing Swing components
![Page 4: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/4.jpg)
Peers and Platform Independence
● The first AWT (Java 1.0) was rolled out in an incredible 6 weeks using peers
● Thus an AWT menu on the Solaris platform, for example, actually creates a Motif menu object as its peer
● UI components that have peers are called heavyweight because– they are rendered in their own (opaque) windows and
thus are expensive to use, – they must be rectangular and cannot have transparent
backgrounds, and– they are not amenable to being subclassed
![Page 5: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/5.jpg)
Using Peers
Java Program
Java AWT
NativeWindowSystemPeers
A Java program creates and displays an AWT component,which creates and displays a native component, or peer.
![Page 6: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/6.jpg)
Lightweight Components● AWT 1.1 introduced the notion of lightweight
components which:– are contained within a heavyweight component's
window– do not have peers– are rendered in their container's window rather than
one of their own– do not incur performance penalties and can have
transparent backgrounds● Almost all Swing components are lightweight
ones that extend either java.awt.Component or java.awt.Container
![Page 7: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/7.jpg)
Some AWT Components
Object
Component
Container ButtonList
Scrollbar
Label
Canvas
JComponent
![Page 8: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/8.jpg)
AWT vs. Swing● Swing does not replace the AWT; it is built on
top of it● All 1.0 AWT components are heavyweight;
corresponding Swing components are lightweight● Swing component names begin with ``J'':
– Component (AWT) vs. JComponent (Swing)– Button (AWT) vs. JButton (Swing)
● Always use Swing components; however, since Swing is built on top of AWT, you will need to know some AWT methods
![Page 9: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/9.jpg)
Some Swing Components
JComponent
AbstractButton
JButtonJMenuItem
JToggleButton
JCheckBox
JLabel
JList
JScrollBar
JFileChooser
![Page 10: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/10.jpg)
JComponents
● Note that JComponents are containers● JComponents do not extend their AWT
counterparts:– For example, the JButton class is not a subclass
(direct or indirect) of Button ● However, some Swing components are not JComponents– For example, some Swing containers are direct
subclasses of their AWT counterparts
![Page 11: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/11.jpg)
Some AWT Containers
Container
JComponent Panel
ScrollPane
Window
Dialog Frame
Applet
![Page 12: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/12.jpg)
Swing Components That Are Not JComponents (in red)
Container
JComponent Panel
ScrollPaneWindow
Dialog Frame Applet
JDialog
JWindow
JFrame JApplet
![Page 13: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/13.jpg)
Some More Swing Components That Are JComponents
JComponent
JLayeredPane JPanel
JScrollPane
JInternalFrame
JDesktopPane
JTable
JTree
![Page 14: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/14.jpg)
Some AWT Component Methods
● void setBackground(Color c)● void setForeground(Color c)● void setEnabled(boolean b)● void setVisible(boolean b)● void setFont(Font f)● void setSize(Dimension d)● void setLocation(int x, int y)
All but setSize and setLocation are overridden by the JComponent class.
![Page 15: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/15.jpg)
Example: A Simple Framed Window
import java.awt.*;import javax.swing.*;
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); frame.setVisible(true); }
}
![Page 16: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/16.jpg)
Notes on the Example
● setSize and setLocation require java.awt.*; the rest require javax.swing.*
● The JFrame constructor argument is used as a title● The Dimension constructor takes an integer width
and height, respectively● The setLocation method takes a pair of integer
coordinates (x,y) where (0,0) is the upper left corner of the display
● The visibility of a JFrame is set to false by default
![Page 17: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/17.jpg)
Example Output Display
● This window was managed by the K Desktop Environment (KDE)
● Clicking the Close button (X) will cause the display to be hidden, but the program will continue since no listeners are set up yet
● Can use ctl-C to kill the Java Virtual Machine
![Page 18: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/18.jpg)
Adding Color
– Color.black– Color.blue– Color.cyan– Color.darkGray– Color.gray– Color.green– Color.lightGray
– Color.magenta– Color.orange– Color.pink– Color.red– Color.white– Color.yellow
The java.awt.Color class has the following staticfields (data members):
![Page 19: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/19.jpg)
Changing Background Color
import java.awt.*;import javax.swing.*;
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); contentPane.setBackground(Color.red); frame.setVisible(true); }
}
![Page 20: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/20.jpg)
Content Panes● Q: Why not just: frame.setBackground(Color.red); ?● A: In order to be lightweight, Swing's top-level
window objects must be built on top of a lightweight AWT Container object introduced in version 1.1
● This container is called a content pane● Swing top-level window classes:
– JWindow– JFrame– JApplet– JDialog– JInternalFrame
![Page 21: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/21.jpg)
Adding a Label and Button
import java.awt.*;import javax.swing.*;
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH); frame.setVisible(true); }
}
![Page 22: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/22.jpg)
New Display
Resized
![Page 23: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/23.jpg)
Notes on the Code
● Since the frame is a top-level Swing window, components must be added to its content pane
● When components are added to a container, how they are placed is dependent upon the container's layout manager
● The default layout manager for a JFrame is a BorderLayout manager (described later)
● When adding to a container whose layout manager is BorderLayout, the second parameter should be a location defined in the BorderLayout class
![Page 24: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/24.jpg)
Adding a List of Optionsimport java.awt.*;import javax.swing.*;
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH); String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER); frame.setVisible(true); }}
![Page 25: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/25.jpg)
New Display
Note that "Option 3" has been selected.
![Page 26: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/26.jpg)
Adding a Check Box and Slider
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(400,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH);
String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER);
JCheckBox cbox = new JCheckBox("Check"); contentPane.add(cbox, BorderLayout.WEST);
JSlider slider = new JSlider(); contentPane.add(slider, BorderLayout.EAST);
frame.setVisible(true); }}
![Page 27: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/27.jpg)
New Display
![Page 28: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/28.jpg)
Layout Management
● A layout manager determines the location and size of components placed into a container
● Different layout managers use different algorithms for determining size and location:– BorderLayout: places at compass locations and center– FlowLayout: places components in rows, left to right– GridLayout: places in rectangular grid– BoxLayout: places in a single row or column
![Page 29: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/29.jpg)
Changing the Layout
public class SwingTest {
public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout()); JLabel label = new JLabel("HERE IS A LABEL"); JButton button = new JButton("BUTTON"); String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); JCheckBox cbox = new JCheckBox("Check"); JSlider slider = new JSlider(); contentPane.add(label); contentPane.add(button); contentPane.add(list); contentPane.add(cbox); contentPane.add(slider);
frame.setVisible(true); }
}
![Page 30: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/30.jpg)
New Display
Resized
![Page 31: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/31.jpg)
Default Layout Managers
● The default layout manager for content panes is BorderLayout. Recall that the following Swing components have content panes:– JWindow– JFrame– JDialog– JApplet– JInternalFrame
● The other Swing container is the JPanel, whose default layout manager is FlowLayout.
![Page 32: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/32.jpg)
JPanels
● A JPanel object can be used for grouping components into a container, which can then be added to another container
● The JPanel constructor with no arguments creates a panel with a FlowLayout manager
● Another JPanel constructor takes any layout manager as an argument
● A JPanel can also be used a a blank area for drawing custom graphics
![Page 33: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/33.jpg)
JPanel Example
JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane();
JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER);
JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); panel.add(button3);
contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
![Page 34: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/34.jpg)
JPanel Example Output
Note use of SwingConstants.CENTER argumentin JLabel constructor.
![Page 35: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/35.jpg)
Changing JPanel Layout
JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane();
JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER);
JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.setLayout (new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3);
contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
![Page 36: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/36.jpg)
New Output
● The button panel is to the west because no other component was placed there
● The BoxLayout constructor requires both the component being laid out and either:– BoxLayout.X_AXIS– BoxLayout.Y_AXIS
![Page 37: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/37.jpg)
Tweaking Layouts
● Some layout constructors allow hgap and vgap, integers specifying the number of pixels separating components horizontally and vertically
● FlowLayout allows the specification of whether the line of components should be left-justified, right-justified, or centered
new FlowLayout(int align)new FlowLayout(int align, int hgap, int vgap)
new BorderLayout(int hgap, int vgap)
new GridLayout(int rows, int cols)new GridLayout(int rows, int cols, int hgap, int vgap)
![Page 38: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/38.jpg)
Tweaking Example JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); LayoutManager lm = contentPane.getLayout(); ((BorderLayout)lm).setHgap(25);
JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER);
JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3);
contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
![Page 39: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/39.jpg)
Tweaking Example Output
● The LayoutManager returned by getLayout() is an interface type that the BorderLayout class implements
● The setHgap method we want is in the BorderLayout class
● So we must cast the LayoutManager to BorderLayout in order to use setHgap
![Page 40: Implementing GUIs in Java](https://reader036.fdocuments.net/reader036/viewer/2022062309/5681554f550346895dc31bc1/html5/thumbnails/40.jpg)
Sizing Hints● Layout managers often need to resize their
components to make things fit– For example, the widths and heights of components in
a BoxLayout are adjusted according to both preferred and maximum heights and widths
● If you don't like the size of the components a layout manager comes up with, you may have to give sizing hints using the following methods from the JComponent class:– void setMinimumSize(Dimension d)– void setPreferredSize(Dimension d)– void setMaximumSize(Dimension d)