想让自己编写的窗口更符合自己的需要,有必要用到一些布局管理。有四种常用简单的布局,分别是
流式布局、边框布局、网格布局和卡片布局。
一、流式布局
在流式布局中,就像在文档中打字一样,当一行的空间不够时,才会将内容显示在下一行。但是,无论是
一行满了还是没满,每行的内容都会显示在行中间。
<1>用 Applet 实现(不需要 main() 方法)
import java.awt.*;
import java.applet.*;
public class FlowLayout1 extends Applet{
public void init()
{
setLayout(new FlowLayout());
for(int i = 0; i < 20; i++)
add(new Button("Button" + i));
}
}
<2>普通实现(需要 main() 方法)
package test;
import java.awt.Button;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class FlowLayoutFrame extends JFrame{
public FlowLayoutFrame(){
setLayout(new FlowLayout());
for(int i = 1; i <= 10; i++)
add(new Button("Button" + i));
pack();
}
}
二、边框布局
在边框布局中,分东南西北中五个部分,东南西北部分会以适合组件的大小占据窗口的边缘,而其余空间就是中的地盘了。
即使放大或缩小窗口,东南西北部分还是只占据其一小部分,扩大或缩小主要体现在中间部分。设置布局时需要指明是 NORTH 等,
不指明默认是中部。
<1>普通实现
package test;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayoutTest");
frame.add(new JButton("北"), BorderLayout.NORTH);
frame.add(new JButton("南"), BorderLayout.SOUTH);
frame.add(new JButton("西"), BorderLayout.WEST);
frame.add(new JButton("东"), BorderLayout.EAST);
frame.add(new JButton("中"), BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
<2> 用 Applet 实现
import java.awt.*;
import java.applet.*;
public class BorderLayout1 extends Applet{
public void init()
{
int i = 0;
setLayout(new BorderLayout());
add("North", new Button("Button" + i++));
add("South", new Button("Button" + i++));
add("West", new Button("Button" + i++));
add("East", new Button("Button" + i++));
add("Center", new Button("Button" + i++));
}
}
三、网格布局
网格布局就像表格,需指定要几行几列。
<1>用 Applet 实现
import java.applet.*;
import java.awt.*;
public class GridLayout1 extends Applet{
public void init()
{
setLayout(new GridLayout(7, 3));
for(int i = 0; i < 20; i++)
add(new Button("Button" + i));
}
}
<2>普通实现(实例:计算器)
package calculator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CalculatorPanel extends JPanel{
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel(){
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(panel, BorderLayout.CENTER);
}
private void addButton(String string, ActionListener listener) {
JButton button = new JButton(string);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if(start){
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(start){
if(command.equals("-")){
display.setText(command);
start = false;
}
else lastCommand = command;
}
else{
calculator(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
private void calculator(double x) {
if(lastCommand.equals("+")) result += x;
else if(lastCommand.equals("-")) result -= x;
else if(lastCommand.equals("*")) result *= x;
else if(lastCommand.equals("/")) result /= x;
else if(lastCommand.equals("=")) result = x;
display.setText("" + result);
}
}
}
package calculator;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class CalculatorFrame extends JFrame{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
public CalculatorFrame(){
EventQueue.invokeLater(new Runnable(){
public void run(){
add(new CalculatorPanel());
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
});
}
}
package calculator;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
但是这里我阴差阳错地写到了框架类 CalculatorFrame 中,测试类中就没写了。程序运行之后,一样是预想的结果出来了。
我不知道这两种写法是否等同,其中的差别自己还没区弄清楚。
四、卡片布局
感觉卡片布局有点像放 PPT,在界面上一次只能看一个页面;像是一堆卡片,拿开一张才能看到下一张。
import java.awt.*;
import java.applet.Applet;
class ButtonPanel extends Panel {
ButtonPanel(String id) {
setLayout(new BorderLayout());
add("Center", new Button(id));
}
}
public class CardLayout1 extends Applet {
Button first = new Button("First"), second = new Button("Second"),
third = new Button("Third");
Panel cards = new Panel();
CardLayout cl = new CardLayout();
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(first);
p.add(second);
p.add(third);
add("North", p);
cards.setLayout(cl);
cards.add("First card", new ButtonPanel("The first one"));
cards.add("Second card", new ButtonPanel("The second one"));
cards.add("Third card", new ButtonPanel("The third one"));
add("Center", cards);
}
public boolean action(Event evt, Object arg) {
if (evt.target.equals(first)) {
cl.first(cards);
} else if (evt.target.equals(second)) {
cl.first(cards);
cl.next(cards);
} else if (evt.target.equals(third)) {
cl.last(cards);
} else
return super.action(evt, arg);
return true;
}
}