Programming Language2: Java GUI - Part 1 Loredana STANCIU Room B616
Programming Language2: Java GUI - Part 1 Loredana STANCIU Room B616
language 2
Java GUI – part 1
Loredana STANCIU
[email protected]
Room B616
What is a user interface
Button()
Creates a button
Button(String label)
Creates a labeled button
void addActionListener(ActionListener l)
Adds the specified action listener to receive
action events from this button.
Button
String getLabel()
Gets the label of the current button.
Checkbox()
Creates a check box with no label
Checkbox(String label)
Creates a check box with the specified label
Checkbox(String label, boolean state)
Creates a check box with the specified label
and sets the specified state
Checkbox(String label,
CheckboxGroup group, boolean state,)
Creates a check box with the label, set to the
state, and in the check box group
CheckBox
void setCheckboxGroup(CheckboxGroup g)
Sets this check box's group to be the
specified check box group.
void setLabel(String label)
Sets this check box's label to be the string
argument.
void setState(boolean state)
Sets the state of this check box to the specified
state.
CheckboxGroup
int getItemCount()
Returns the number of items in this Choice
menu.
int getSelectedIndex()
Returns the index of the currently selected
item.
String getSelectedItem()
Gets a representation of the current choice
as a string.
Chioce
setLayout(new
FlowLayout(FlowLayout.CENTER, 10, 10));
add(new Label("Hi There!"));
add(new Label("Another Label"));
Label
Label(String text);
Creates a new instance of Label
String getText();
Returns a label’s text
void setText(String text);
Sets a label’s text to the specified text
Scrollbar
redSlider=new
Scrollbar(Scrollbar.VERTICAL, 0, 1, 0,
255);
add(redSlider);
ranger = new
Scrollbar(Scrollbar.HORIZONTAL, 0, 60, 0,
300);
add(ranger);
Scrollbar
Scrollbar()
Constructs a new vertical scroll bar.
Scrollbar(int orientation)
Constructs a new scroll bar with the specified
orientation.
Scrollbar
TextField()
Creates a new instance of TextField
TextField(String text)
Creates a new instance of TextField with
the specified text
void setColumns(int columns)
Sets the number of columns in a textbox
void setText(String text)
Sets a textfield text to the specified text
List
List()
Creates a new scrolling list.
List(int row)
Creates a new scrolling list initialized with the
specified number of visible lines.
List(int rows, boolean multipleMode)
Creates a new scrolling list initialized to display
the specified number of rows.
List
Window
A top-level display surface (a window)
An instance of the Window class has no border
and no title
Frame
A top-level display surface (a window) with a
border and title
An instance of the Frame class may have a
menu bar
Types of containers
Dialog
A top-level display surface (a window) with a
border and title
An instance of the Dialog class cannot exist
without an associated instance of the Frame
class.
Panel
A generic container for holding
components.
An instance of the Panel class provides a
container to which to add components.
FRAMES
Frame()
Creates a new instance of Frame that is
initially invisible
Frame(String title)
Creates a new instance of Frame that is
initially invisible with the specified title
boolean isResizable()
Returns a boolean value indicating whether a
frame is resizable or not
void setResizable(boolean resizable)
Sets whether or not a frame is resizable
void isVisible(boolean isVisible)
Sets whether or not a frame is visible
FRAMES
String getTitle()
Returns the title of a frame
void setTitle(String title)
Sets the title of a frame
void setSize(int width, int height)
Sets the size of a frame
void setLocation(int xCoord, int
yCoord)
Sets the location of the window
Frames
import java.awt.*;
class MyFrame {
public static void main(){
Frame frame = new Frame("My
title");
frame.setSize(200, 200);
frame.setVisible(true);
}}
Dialog
Dialog(Dialog owner)
Constructs an initially invisible, non-modal
Dialog with an empty title and the specified
owner dialog.
Dialog(Dialog owner, String title)
Constructs an initially invisible, non-modal
Dialog with the specified owner dialog and
title.
Dialog(Frame owner)
Constructs an initially invisible, non-modal
Dialog with an empty title and the specified
owner frame.
Dialog
void show()
Makes the Dialog visible.
void setResizable(boolean resizable)
Sets whether this dialog is resizable by the
user.
void setTitle(String title)
Sets the title of the Dialog.
Panel
Panel()
Creates a new panel using the default layout
manager.
Panel(LayoutManager layout)
Creates a new panel with the specified
layout manager.
void add(…)
Add a component to the panel
void remove(…)
Remove a component from the panel
Java event handling
mouseEntered(MouseEvent e)
Called when the mouse enters a source
components area
Methods of the
MouseMotionListener interface
mouseMoved(MouseEvent e)
Called when the mouse is moved while in a
components area
mouseDragged(MouseEvent e)
Called when the mouse moves while a mouse
button is down while within a components area
Handling key events
Example:
//create a button
Button aButton = new Button("This is a
button");
//specify that this button listens for
action events
aButton.addActionListener(this);
Listen for action events