Java AWT Swing
Java AWT Swing
Button Canvas
Checkbox CheckboxGroup
Choice List
Menu Label
TextField TextArea
Scrollbar ScrollPane
COMPONENT HIERARCHY IN AWT
Object
Component Scrollbar
Canvas
Checkbox
Choice
Label
Text Component
Text Area
List
Text Field
Container
AWT (CONTAINER)
Component
Container
Panel Window
Frame Dialog
FileDialog
EXAMPLE: A SIMPLE FRAMED WINDOW
import java.awt.*;
import javax.swing.*;
}
JAVA
Swing Programming
SWING COMPONENTS
Swing is a collection of libraries that
contains primitive widgets or controls
used for designing Graphical User
Interfaces (GUIs).
Object
Component
Container
JComponent Window
JPanel Frame
JFrame
USING SWING COMPONENTS
Very simple, just create object from appropriate
class – examples:
JButton but = new JButton();
JTextField text = new JTextField();
JTextArea text = new JTextArea();
JLabel lab = new JLabel();
Many more classes. Don’t need to know every one
to get started.
See ch. 9 Hortsmann
ADDING COMPONENTS
Oncea component is created, it can be
added to a container by calling the
container’s add method:
import javax.swing.JFrame;
class SimpleGUI extends JFrame{
SimpleGUI(){
setSize(400,400); //set frames size in pixels
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
}
}
ADD SECOND BUTTON/EVENT
}
GOOD WAY
class MyActionListener implents ActionListener{
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == b2){
System.exit(1);
}
else if (ae.getSource() == b1){
JOptionPane.showMessageDialog(null, “I’m clicked”);
}
}