Week 7 - GUI 1
Week 7 - GUI 1
Week 7 - GUI 1
GUI Principles
• Components: GUI building blocks.
– Buttons, menus, sliders, etc.
• Layout: arranging components to form a
usable GUI.
– Using layout managers.
• Events: reacting to user input.
– Button presses, menu selections, etc.
Java GUI: AWT and Swing
• Sun’s initial idea: create a set of classes/methods that can
be used to write a multi-platform GUI (Abstract Windowing
Toolkit, or AWT)
– problem: not powerful enough; limited; a bit clunky to use
Menu bar
Content pane
Onscreen GUI elements
• Windows: actual first-class citizens of desktop;
also called top-level containers
– examples: frame, dialog box
• Components: GUI widgets
– examples: button, text box, label
• Containers: logical grouping for components
– example: panel
6
Onscreen GUI elements
• Windows: actual first-class citizens of desktop; also
called top-level containers
– examples: frame, dialog box
• Components: GUI widgets
– examples: button, text box, label
• Containers: logical grouping for components
– example: panel
8
Creating a Window in Swing
import javax.swing.*;
9
Creating a Window in Swing
• setSize and setLocation require
java.awt.*; the rest require javax.swing.*
import javax.swing.*;
public Basic2() {
setTitle("Basic Test2!"); //set the title
//quit Java after closing the window
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200); //set size in pixels
setVisible(true); //show the window
}
}
11
Methods of all Swing components
• public int getWidth()
public int getHeight()
Allow access to the component's current width and height in pixels.
12
More JComponent methods
• public void setBackground(Color c)
Sets the background color of the component to be the given color.
• public void setFont(Font f)
Sets the font of the text on the given component to be the given
font.
//create new Font
Font font = new Font("Courier", Font.BOLD,12);
• public void setEnabled(boolean b)
Sets whether the component is enabled (can be interacted with).
• public void setVisible(boolean b)
Sets whether the component is visible (can be seen on the screen).
Set to true to show the component, or set to false to hide the
component. 13
JFrame
A frame is a graphical window that can be used to hold other components
• public JFrame() or public JFrame(String title)
Creates a frame with an optional title.
15
JPanel
• A panel is our container of choice; it inherits the
methods from the previous slide and defines these
additional methods (among others):
• public JPanel()
16
JButton, JLabel
• The most common component a button is a clickable
onscreen region that the user interacts with to
perform a single command
• A text label is simply a string of text displayed on
screen in a graphical program. Labels often give
information or describe other components
• public JButton(String btnText);
// Creates a new button with given string as its text
• public Label(String strLabel);
// Creates a new button with given string as its text..
• public ButtonGroup()
• public void add(JRadioButton button)
20