Академический Документы
Профессиональный Документы
Культура Документы
Topics
Graphics
Topics
Layout Managers
A JFrame Example
A JOptionPane Example
Similarities:
AWT
Platform-dependent
Swing
Platform-independent
import java.awt.*;
4
5
sf.setVisible(true);
}
8
9
}
I nt roduct ion t o Program m ing 2
import java.awt.*;
GraphicPanel() {
setBackground(Color.black);
4
5
g.setColor(new Color(0,255,0));
g.setFont(new Font("Helvetica",Font.PLAIN,16));
10
11
12
13
//green
}
//continued...
10
15
16
17
18
f.add(gp);
19
f.setSize(600, 300);
20
f.setVisible(true);
}
21
22
11
AWT controls
12
import java.awt.*;
fwc.setLayout(new FlowLayout());
fwc.setSize(600, 600);
fwc.add(new Label("Labe"));
fwc.add(new TextField());
10
11
12
13
14
//continued...
FrameWControls();
13
16
list.add("MTV");
17
list.add("V");
18
fwc.add(list);
19
20
chooser.add("Avril");
21
chooser.add("Monica");
22
chooser.add("Britney");
23
fwc.add(chooser);
24
fwc.add(new Scrollbar());
25
fwc.setVisible(true);
}
26
27
}
I nt roduct ion t o Program m ing 2
14
Layout Managers
Definition:
FlowLayout
BorderLayout
GridLayout
GridBagLayout
CardLayout
I nt roduct ion t o Program m ing 2
15
16
Layout Managers:
The FlowLayout Manager
17
Layout Managers:
The FlowLayout Manager
18
Layout Managers:
The FlowLayout Manager
Gap
Measured in pixels
19
Layout Managers:
The FlowLayout Manager
1
import java.awt.*;
3
4
fld.setLayout(new FlowLayout(FlowLayout.RIGHT,
10, 10));
6
7
fld.add(new Button("ONE"));
fld.add(new Button("TWO"));
fld.add(new Button("THREE"));
10
fld.setSize(100, 100);
11
fld.setVisible(true);
}
12
13
20
Layout Managers:
The FlowLayout Manager
Sample output:
21
Layout Managers:
The BorderLayout Manager
North
- stretch horizontally
East
- adjust vertically
West
- adjust vertically
22
Layout Managers:
The BorderLayout Manager
23
Layout Managers:
The BorderLayout Manager
Valid regions:
BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
I nt roduct ion t o Program m ing 2
24
Layout Managers:
The BorderLayout Manager
1
import java.awt.*;
3
4
10
11
bld.setSize(200, 200);
12
bld.setVisible(true);
}
13
14
25
Layout Managers:
The BorderLayout Manager
Sample output:
After resizing:
26
Layout Managers:
The GridLayout Manager
Like FlowLayout
27
Layout Managers:
The GridLayout Manager
28
Layout Managers:
The GridLayout Manager
1
import java.awt.*;
3
4
gld.add(new Button("ONE"));
gld.add(new Button("TWO"));
gld.add(new Button("THREE"));
gld.add(new Button("FOUR"));
10
gld.add(new Button("FIVE"));
11
gld.setSize(200, 200);
12
gld.setVisible(true);
}
13
14
29
Layout Managers:
The GridLayout Manager
Sample output:
After resizing:
30
Recall:
31
import java.awt.*;
/* North Panel */
10
panelNorth.add(new Button("ONE"));
11
panelNorth.add(new Button("TWO"));
12
panelNorth.add(new Button("THREE"));
13
//continued...
I nt roduct ion t o Program m ing 2
32
/* Center Panel */
15
panelCenter.setLayout(new GridLayout(4,4));
16
panelCenter.add(new TextField("1st"));
17
panelCenter.add(new TextField("2nd"));
18
panelCenter.add(new TextField("3rd"));
19
panelCenter.add(new TextField("4th"));
20
/* South Panel */
21
panelSouth.setLayout(new BorderLayout());
22
23
24
25
26
27
//continued...
I nt roduct ion t o Program m ing 2
33
29
30
cl.add(panelNorth, BorderLayout.NORTH);
31
cl.add(panelCenter, BorderLayout.CENTER);
32
cl.add(panelSouth, BorderLayout.SOUTH);
33
cl.setSize(300,300);
34
cl.setVisible(true);
}
35
36
34
Sample output:
35
Have the same look and feel even when executed on different
platforms
Color chooser
Option pane
36
Example:
AWT: Button class
Corresponding Swing component: JButton class
37
38
39
40
import javax.swing.*;
import java.awt.*;
class SwingDemo {
JFrame frame;
JPanel panel;
JTextField textField;
JButton button;
Container contentPane;
10
11
sd.launchFrame();
12
13
}
//continued...
I nt roduct ion t o Program m ing 2
41
14
15
/* initialization */
16
17
18
19
20
contentPane = frame.getContentPane();
21
22
panel.add(textField);
23
panel.add(button);
24
25
contentPane.add(panel, BorderLayout.CENTER);
26
frame.pack();
27
frame.setVisible(true);
}
28
29
42
Giving a title to the frame and packing the components within the
frame is applicable for AWT frames too
Coding convention:
43
Sample output:
44
import javax.swing.*;
class JOptionPaneDemo {
JOptionPane optionPane;
void launchFrame() {
optionPane.showMessageDialog(null,
8
9
10
"Greeting...",optionPane.PLAIN_MESSAGE);
System.exit(0);
11
12
13
14
new JOptionPaneDemo().launchFrame();
I nt roduct ion t o Program m ing 2
}
45
Sample output:
46
Summary
Differences
Graphics
Met hods and t he Color class
47
Summary
Layout Managers
48