今天java作业,编了差不多一晚上,终于发现,原来一直我都把GridBagLayout给忽略掉了,本来想用绝对定位解决的,但是无论我怎么用FlouLayout和BordLayout,CardLayout我都无法将作业要求的界面通过编程呈现出来(如果是用c#编的话,几分钟就可以搞掂了,故本人比较不喜欢java这一点,java在界面编辑方面确实不如HTML和c#,c++等语言)。实在郁闷之极,无奈之下,本着有问题必要解决问题的决心,我上百度求救。功夫不负有心人。某同学向我推荐了GridBagLayout。本人觉得实在是感激万分,故此写此博文,以此记录一下今天的收获。
实训作业:
编辑一个小键盘运行界面:
代码如下:
import java.awt.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NumberPad {
private static final Insets
insets = new Insets(0, 0, 0, 0);
public static void main(final
String args[]) {
final JFrame frame = new JFrame("NumberPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口操作
frame.setLayout(new GridBagLayout());//框架布局
JButton button;
//下面利用设立的类对按键进行布局
//第一行
button = new JButton("Num");
addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("/");
addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("*");
addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("-");
addComponent(frame, button, 3, 0, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
//第二行
button = new JButton("1");
addComponent(frame, button, 0, 1, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("2");
addComponent(frame, button, 1, 1, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("3");
addComponent(frame, button, 2, 1, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("+");
addComponent(frame, button, 3, 1, 1, 2, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
// 第三行
button = new JButton("4");
addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("5");
addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("6");
addComponent(frame, button, 2, 2, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
//第四行
button = new JButton("7");
addComponent(frame, button, 0, 3, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("8");
addComponent(frame, button, 1, 3, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("9");
addComponent(frame, button, 2, 3, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton("Enter");
addComponent(frame, button, 3, 3, 1, 2, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
//第五行
button = new JButton("0");
addComponent(frame, button, 0, 4, 2, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
button = new JButton(".");
addComponent(frame, button, 2, 4, 1, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH);
frame.setSize(250,250);
frame.setVisible(true);
}
private static void
addComponent(Container container, Component component, int gridx,
int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, 0, 0);//建立网格包对象
container.add(component, gbc);//添加到容器中
}
}//运行结果: