Beruflich Dokumente
Kultur Dokumente
UTP2008
Standard Classes
UTP2008
java.awt
1. These standard classes are organized under the following packages:
2. java.awt is a package. Contains all of the classes for creating GUI and
for painting graphics and images.
Component
Button, Canvas,
TextComponent, Label, etc..
Container
Window
Frame
JFrame
UTP2008
Panel
Dialog
JWindow
java.aw
t
JDialog
Applet
JApplet
javax.swin
g
javax.swing
1. AWT provides only basic GUI components.
2. Enhanced GUI components are in a different package:
javax.swing.*
javax.
swing
JPanel
JMenuBar
JComponent
AbstractButton
JTextComponent
JLabel
JButton
JTextArea
JTextField
JToggleButton
JCheckBox
UTP2008
JRadioButton
JPasswordField
2.
3.
UTP2008
Demo
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
import java.awt.*;
import javax.swing.*;
public class HelloGUI {
JFrame myFrame;
JLabel myLabel;
Container myPane;
public HelloGUI() {
myFrame = new JFrame("Hello GUI");
myPane = myFrame.getContentPane();
myLabel = new JLabel("Hello Java GUI! Welcome to my life.");
}
public void launchFrame(){
myPane.add(myLabel);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
public static void main(String[] args) {
HelloGUI myHelloGUIObject = new HelloGUI();
myHelloGUIObject.launchFrame();
}
}
UTP2008
A SIMPLE EXAMPLE
Create the following GUI:
UTP2008
UML
Instantiate the
objects.
Assemble and
show the objects.
We also want to make this class the application entry
class. How?
UTP2008
Code Explained
1.
import java.awt.*;
1.
2.
import javax.swing.*;
2.
3.
3.
4.
5.
6.
7.
5.
myPanel.setBackground(Color.YELLOW);
7.
myPanel.add(myButton1);
8.
myPanel.add(myButton2);
9.
public MyGUI() {
10.
11.
myPane = myFrame.getContentPane();
12.
13.
10.
myFrame.pack();
11.
myFrame.setVisible(true);
12.
13.
14.
15.
myDemo.createAndShowGUI();
14.
16.
15.
17.
16.
myPane.add(myPanel);
6.
8.
9.
4.
}
}
UTP2008
10
Code Explained
UTP2008
11
setDefaultLookAndFeelDecorated(true)
Applies decorative
2.
setDefaultCloseOperationJFrame.EXIT_ON_CLOSE)
3.
pack()
size.
4.
setVisible(true)
5.
add(myButton)
UTP2008
12
Containers
1.
2.
UTP2008
13
Panel Example
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Demo_Panel {
5.
private JFrame myFrame;
6.
private Container myPane;
7.
private JPanel myPanel1,myPanel2;
8.
9.
public Demo_Panel() {
10.
myFrame = new JFrame("My Window");
11.
myPane = myFrame.getContentPane();
12.
myPanel1 = new JPanel();
13.
myPanel2 = new JPanel();
14.
}
15. //see next page
UTP2008
14
Panel Example
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
UTP2008
JLabel
1. JLabel myLabel1 = new JLabel(Hello);
2. panel.add(myLabel1);
3. myLabel1.setName(Login name:);
UTP2008
16
JTextField
1.
2.
3.
UTP2008
JTextField textField;
textField = new JTextField(jTextField1");
panel.add(textField);
17
JCheckBox
1.
2.
3.
4.
5.
6.
7.
UTP2008
18
JRadioButton
1.
2.
3.
4.
5.
6.
7.
UTP2008
19
ButtonGroup
1.
2.
3.
4.
5.
6.
7.
UTP2008
20
Combo Box
1.
2.
3.
UTP2008
private String[] s1 =
{"None","Diploma","Bachelor","Master",PhD"};
cb1 = new JComboBox(s1);
cb1.setSelectedItem("None");
21
Scrollpane
1.
2.
a = new JTextArea();
scrol = new JScrollPane(a);
JTextArea
JScrollPane
UTP2008
22
Layout Manager
1.
2.
3.
4.
UTP2008
23
UTP2008
24
BorderLayout
1. BorderLayout is the default layout of the frame. Hence dont have to set
the layout.
2. The frame is divided into 5 regions:- CENTER, PAGE_START,
PAGE_END, LINE_START, and LINE_END.
3. Each region can store only one component. For example, if I try to
add more than one button in any one region, only the last added
button will be shown.
UTP2008
25
FlowLayout
1.
2.
3.
Hence have to set the layout explicitly if we want the frame to have flow
layout instead of borderlayout.
4.
Example:
myFrame.setLayout(new FlowLayout());
5.
UTP2008
26
GridLayout
1.
GridLayout is not the default layout of the frame. Hence have to set the
layout explicitly.
2.
Syntax:
SetLayout(new GridLayout(int rows, int cols));
3.
Examples:
UTP2008
27
BoxLayout
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
import javax.swing.JFrame;
//Set up the content pane.
public class BoxLayoutDemo {
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
launchGUI();
addAButton("5", pane);
}
}
}
The above code generates Box Layout arrangement of buttons. Run and
study the code.
UTP2008
28
import javax.swing.*;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
public MyGUI()
{
button1 = new JButton(Exit");
panel = new JPanel();
panel.add(button1);
}
JPanel getPanel()
{
return panel;
}
Initiate components.
Then assemble.
UTP2008
29
import javax.swing.*;
import java.awt.*;
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Resides in
Resides in
Initialize JFrame
object with parameter
Demo.
Use JFrame method
to retrieve the content
pane.
Instantiate the other
class MyGUI as
object.
UTP2008
30