10/24/20151 java gui programming. 10/24/20152 what is a gui? java has standard packages for creating...
TRANSCRIPT
![Page 1: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/1.jpg)
04/20/23 1
Java GUI Programming
![Page 2: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/2.jpg)
04/20/23 2
What is a GUI?
• Java has standard packages for creating custom Graphical User Interfaces
• Some of the fundamental GUI components:
Button Label Text Field CheckBox
Radio Buttons ComboBox
![Page 3: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/3.jpg)
04/20/23 3
What is AWT?
• The Abstract Window Toolkit was a part of Java from the beginning
import java.awt.*;
• All AWT components must be mapped to platform specific components using peers– The look and feel of these components is tied to the native components of the window manager
• AWT components are considered to be very error prone and should not be used in modern Java applications
![Page 4: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/4.jpg)
04/20/23 4
What is AWT?
• The same application using only AWT components, running on X-Windows:
![Page 5: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/5.jpg)
04/20/23 5
What is Swing?
• With the release of Java 2, the AWT user interface components were replaced with Swing
• Swing is built on top of AWT to give a more flexible, robust library– Lightweight components don’t rely on the native GUI
– Heavyweight components do depend on the target platform because they extend AWT components
• Swing components are directly painted onto the canvas using Java code
![Page 6: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/6.jpg)
04/20/23 6
Java GUI API
Object
DimensionFont
FontMetricsColorGraphics
Component Container
LayoutManager
Panel
Applet
Window Frame
Dialog
JComponent
JDialog
JFrame
JApplet
AWT: java.awt
Swing: javax.swing
Heavyweight
Lightweight
![Page 7: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/7.jpg)
04/20/23 7
Java GUI API
JComponent
AbstractButton
JLabel
JList
JComboBox JPanel
JOptionPane
JScrollBar
JSlider
JTabbedPane
JSplitPane
JLayeredPane
JSeparator
JScrollPane
JRootPane
JToolBar
JMenuBar JPopupMenu
JFileChooser
JColorChooser
JToolTip
JTree
JTable
JTableHeader
JInternalFrame
JProgressBar
JSpinner
JTextComponent
JEditorPane
JTextFieldJTextArea
JMenuItem
JButton
JToggleButton
JCheckBoxMenuItem
JMenu
JRadioButtonMenuItem
JCheckBox
JRadioButton
JPasswordField
![Page 8: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/8.jpg)
04/20/23 8
Container Classes
• Container classes are GUI components that are used as containers to contain other GUI components– For Swing use: Component, Container, JFrame, JDialog, JApplet, Jpanel
– JFrame is a window not contained inside another window
– JDialog is a temporary popup window or message box
– JApplet is an applet that can run in a web browser
– JPanel is an invisible, nest-able container used to hold UI components or canvases to draw graphics
• A layout manager is used to position and place components in a container
![Page 9: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/9.jpg)
04/20/23 9
Frames
• You need a frame to hold the UI components
Title bar
Contentpane
400 pixels wide
300 pixels high
(100, 100)
![Page 10: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/10.jpg)
04/20/23 10
Content Pane
• The content pane is the part of the frame where the UI components will be placed
• It is a java.awt.Container object
Glass pane
Layered pane
Menu bar
Content pane
![Page 11: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/11.jpg)
04/20/23 11
Adding Components to a Frame
• UI components can be add’ed to the content pane after they are created
• Here, the OK button is centered in the frame and occupies the whole frame, no matter how it is resized
![Page 12: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/12.jpg)
04/20/23 12
Layout Managers
• There are three basic layout managers which control how UI components are organized on the frame– FlowLayout– GridLayout– BorderLayout
• Once created, the layout can be set in the content pane using setLayout
• As the window is resized, the UI components reorganize themselves based on the rules of the layout
![Page 13: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/13.jpg)
04/20/23 13
FlowLayout
• With flow layout, the components arrange themselves from left to right in the order they were added
Rows/buttons are left aligned using FlowLayout.LEFT
Vertical gap of 20 pixels
Horizontal gapof 10 pixels
![Page 14: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/14.jpg)
04/20/23 14
Extending JFrame
public class GUIMain extends JFrame {// construct GUI interface with componentspublic GUIMain() {
// set the layout managerContainer container = getContentPane();container.setLayout(…);
// create UI components and addcontainer.add(…)
} // GUIMain
// create instance of GUIMain and set // frame behaviorspublic static void main(String args[]) {
GUIMain frame = new GUIMain();
frame.setTitle(…);…
} // main} // GUIMain
![Page 15: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/15.jpg)
04/20/23 15
GridLayout
• With grid layout, the components arrange themselves in a matrix formation (rows, columns)
• Either the row or column must be non-zero
• The non-zero dimension is fixed and the zero dimension is determined dynamically
• The dominating parameter is the rows
![Page 16: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/16.jpg)
04/20/23 16
GridLayout
2,4
0,4
4,4
10, 10
![Page 17: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/17.jpg)
04/20/23 17
BorderLayout
• With border layout, the window is divided into five areas:
• Components are added to the frame using a specified index:
container.add(new JButton(“East”), BorderLayout.EAST);
BorderLayout.NORTH
BorderLayout.WEST BorderLayout.CENTER BorderLayout.EAST
BorderLayout.SOUTH
![Page 18: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/18.jpg)
04/20/23 18
BorderLayout
![Page 19: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/19.jpg)
04/20/23 19
BorderLayout
• The components stretch in this manner:– North and South stretch horizontally– East and West stretch vertically– Center can stretch in both directions to fill space
• The default location for a component is BorderLayout.CENTER
• If you add two components to the same location, only the last one will be displayed
• It is unnecessary to place components to occupy all areas
![Page 20: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/20.jpg)
04/20/23 20
Color
• The color of GUI components can be set using the java.awt.Color class
• Colors are made of red, green and blue components which range from 0 (darkest shade) to 255 (lightest shade)
• Each UI component has a background and foreground:
Color color = new Color(128, 0, 0);JButton button = new JButton();button.setBackground(color); // reddishbutton.setForeground(new Color(0, 0, 128)); // blueish
![Page 21: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/21.jpg)
04/20/23 21
Color
• There are 13 constant colors defined in Color:– BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN,
LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW
![Page 22: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/22.jpg)
04/20/23 22
Panels
• Write a program to organize the components for a microwave oven:
• The problem is we want to use different layouts for different components
text field
12 buttonsbutton
![Page 23: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/23.jpg)
04/20/23 23
Panels
• The window can be subdivided into different panels
• The panels act as sub-containers for grouping UI components
12 buttons
text field
button
Panel 1 uses a grid layout
Panel 2 uses a border layout:text: NorthPanel 1: Center
The content paneuses a border layout:Panel2: EastButton: Center
![Page 24: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/24.jpg)
04/20/23 24
Fonts
• You can create a font using the Font class
public Font(String name, int style, int size);
• The standard fonts are “SansSerif”, “Serif”, “Monospaced”, “Dialog”, or “DialogInput”
• The styles are Font.PLAIN, Font.BOLD, Font.ITALIC, and Font.BOLD + Font.ITALIC
Font font1 = new Font(“SansSerif”, Font.BOLD, 16);
Font font2 = new Font(“Serif”, Font.ITALIC, 12);
JButton button = new JButton(“OK”);
button.setFont(font1);
![Page 25: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/25.jpg)
04/20/23 25
Graphics
• Graphics can be drawn using a class which extends JPanel
• Swing will call the paintComponent method to draw:
protected void paintComponent(Graphics g);
• There are a variety of drawing methods:drawString(String string, int x, int y);
drawLine(int x1, int y1, int x2, int y2);
drawRect(int x, int y, int w, int h);
drawOval(int x, int y, int w, int h);
drawPolygon(int[] xpoints, int[] ypoints, int npoints);
![Page 26: 10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental](https://reader035.vdocuments.mx/reader035/viewer/2022062422/56649ef45503460f94c07064/html5/thumbnails/26.jpg)
04/20/23 26
Graphics
filled rectangle
unfilled rectangle
linefilledoval
filledpolygon
unfilledoval
filledarc
unfilledarc
string