测试结果:
附上详解代码
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import javax.swing.*;
public class GridBagDemo extends JFrame {
public static void main(String args[]) {
//调用构造参数
GridBagDemo demo = new GridBagDemo();
}
//重写构造参数
public GridBagDemo() {
//设置设计布局
init();
//设置框架大小
this.setSize(600,600);
//设置框架位置
this.setLocation(500, 200);
//设置框架可见性
this.setVisible(true);
closing();
}
//设置关闭窗口
public void closing() {
//设置动作监听
this.addWindowListener(new WindowAdapter() {
//重写closing方法
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
public void init() {
//设置打开按钮
JButton button1 = new JButton("打开");
//设置保存按钮
JButton button2 = new JButton("保存");
//设置另存为按钮
JButton button3 = new JButton("另存为");
//设置panel布局
JPanel button4 = new JPanel();
//设置可选择栏
String[] str = { "java笔记", "C#笔记", "HTML5笔记" };
JComboBox button5 = new JComboBox(str);
//设置清空前文本框
JTextField button6 = new JTextField();
//设置清空前文本框背景颜色
button6.setBackground(Color.white);
//设置清空按钮
JButton button7 = new JButton("清空");
//设置左侧栏目选择
JList jList = new JList(str);
//设置是否可以拖拉文本框中的文字
jList.setDragEnabled(true);
//设置文本
JTextArea button9 = new JTextArea();
//设置可编辑文本背景颜色
button9.setBackground(Color.gray);
//设置 GridBagLayout 分布管理器
GridBagLayout layout = new GridBagLayout();
//设置框架的分布管理器为 GridBagLayout
this.setLayout(layout);
//将按钮都添加到Frame框架中去
this.setTitle("筱某佳同学的笔记");
//设置图标LOGO
this.setIconImage(new ImageIcon("LOGO.png").getImage());
this.add(button1);
this.add(button2);
this.add(button3);
this.add(button4);
this.add(button5);
this.add(button6);
this.add(button7);
this.add(jList);
this.add(button9);
//定义GridBagConstraints作为控制组件显示位置的对象
GridBagConstraints s= new GridBagConstraints();
//设置如果组件所在的区域比组件本身要大时的显示情况
s.fill = GridBagConstraints.BOTH;
/**
//该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况
//NONE:不调整组件大小。
//HORIZONTAL:加宽组件,使它在水平上填满其显示区域,但是不改变高度。
//VERTICAL:加高组件,使它在垂直方向上填满其显示区域,但是不改变宽度。
//BOTH:使组件完全填满其显示区域。
*/
//设置组件水平所占用的格子数为1.
//如果为0,就说明该组件是该行的最后一个
s.gridwidth=1;
//设置组件水平的拉伸幅度。
//如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
s.weightx = 0;
//设置组件垂直的拉伸幅度。
//如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间
s.weighty=0;
//调整完组件显示区域后
//添加组件到layout分布管理器
layout.setConstraints(button1, s);
s.gridwidth=1;
s.weightx = 0;
s.weighty=0;
layout.setConstraints(button2, s);
s.gridwidth=1;
s.weightx = 0;
s.weighty=0;
layout.setConstraints(button3, s);
//该方法是设置组件水平所占用的格子数。
//如果为0,就说明该组件是该行的最后一个
s.gridwidth=0;
//不能为1,j4是占了4个格,并且可以横向拉伸,
//但是如果为1,后面行的列的格也会跟着拉伸,导致j7所在的列也可以拉伸,//所以应该是跟着j6进行拉伸
s.weightx = 0;
s.weighty=0;
layout.setConstraints(button4, s);
s.gridwidth=2;
s.weightx = 0;
s.weighty=0;
layout.setConstraints(button5, s);
s.gridwidth=4;
s.weightx = 1;
s.weighty=0;
layout.setConstraints(button6, s);
s.gridwidth=0;
s.weightx = 0;
s.weighty=0;
layout.setConstraints(button7, s);
s.gridwidth=2;
s.weightx = 0;
s.weighty=1;
layout.setConstraints(jList, s);
s.gridwidth=5;
s.weightx = 0;
s.weighty=1;
layout.setConstraints(button9, s);
}
}