測試結果:
附上詳解代碼
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);
}
}