1.先在一个固定的范围内做好规划,先别考虑拉伸;
把能固定的先固定了;
2.在水平方向和垂直方向上找一个最适合用来拉伸的组件,将其设置为可拉伸;
3.然后可以调整原先设定好的参数,设定容器的bounds
补充:
0.组件的GridWidth GridHeight针对的是自身的宽高
1.JPanel 没有默认的宽高,所以一般用来占位,填充空隙,拉伸;
2.JPanel 必须设置为某一方向上的拉伸,要不然就无用了
3.JPanel 因为没有宽高,所以JPanel之间布局时,只有设置其约束的iPadX iPadY,固定其宽高
4.JPanel因为用来拉伸,所以其约束GridWidth 和 GridHeight 有一个规范的作用,比如
JPanel (2,1),另外同一列的组件为(1,1),那么这个组件是不会超过JPanel的
5.组件布局直接在一个容器中进行,不要再划分为JPanel布局,因为JPanel布局难
确定宽高,拉伸复杂
6.一个可拉伸的组件被水平拉伸时,垂直方向上与之有交集的组件也会被拉伸
7.一个可拉伸的组件被垂直拉伸时,水平方向上与之有交集的组件也会被拉伸
Test
package Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame{
public Test (){
super();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
this.setLayout(new GridBagLayout());
this.setBounds(200,200,300,300);
this.initComponent();
// this.pack();
}
private void initComponent(){
//上侧的工具选择面板
JPanel toolSelectPanel = new JPanel();
toolSelectPanel.setBackground(Color.green);
this.add(toolSelectPanel, new GBC(0,0,2,1).setIpad(200,50).
setFill(GBC.BOTH).setWeight(100, 0));
//左侧的具体工具面板
JPanel toolConcretePanel = new JPanel();
toolConcretePanel.setBackground(Color.YELLOW);
this.add(toolConcretePanel,new GBC(0,1,1,1).setIpad(50,100).
setFill(GBC.BOTH).setWeight(0, 100));
//右侧的绘图面板
JPanel drawPanel = new JPanel();
drawPanel.setBackground(Color.pink);
this.add(drawPanel,new GBC(1,1,1,1).setFill(GBC.BOTH).setWeight(100, 100));
/*这里面的GridWidth和GridHeight是起到的一个规范的作用,比如:
右侧的绘图面板(1,1),上侧的工具选择面板(2,1),那么(1,1)是不会超过(2,1)的*/
//下侧的颜色选择面板
JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.LIGHT_GRAY);
this.add(colorPanel,new GBC(0,2,2,1).setIpad(200,20).
setFill(GBC.BOTH).setWeight(100, 0));
//下侧的状态面板
JPanel statePanel = new JPanel();
statePanel.setBackground(Color.CYAN);
this.add(statePanel,new GBC(0,3,2,1).setIpad(200,50).
setFill(GBC.BOTH).setWeight(100, 0));
}
public static void main(String[] args) {
new Test ().setVisible(true);;
}
}
Test2
package Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class Test2 extends JFrame{
public Test2 (){
super();
this.setLayout(new GridBagLayout());
this.initComponents();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
this.setBounds(200,200,500,400);
// this.pack();
}
private void initComponents (){
//第一行
this.add(new JButton("打开"),new GBC(0,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
this.add(new JButton("保存"),new GBC(1,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
this.add(new JButton("另存为"),new GBC(2,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
this.add(new JPanel(),new GBC(3,0,1,1).setFill(GBC.BOTH).setWeight(100, 0));
//第二行
JLabel label = new JLabel("JLabel");
label.setOpaque(true);//设置为不透明,才能设置背景颜色
label.setBackground(Color.cyan);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
this.add(label,new GBC(0,1,2,1).setFill(GBC.BOTH).setWeight(0, 0));
this.add(new JTextField("JTextField"),new GBC(2,1,5,1).setFill(GBC.BOTH).setWeight(100, 0));
this.add(new JButton("清空"),new GBC(7,1,1,1).setFill(GBC.BOTH).setWeight(0, 0));
//第三行
JTextArea textArea = new JTextArea("");
textArea.setLineWrap(true);
textArea.setForeground(Color.BLACK);
textArea.setMargin(new Insets(7,7,7,7));
textArea.setBackground(Color.PINK);
String arr[] = {"java","android","ios","php","python","c++"};
this.add(new JList(arr),new GBC(0,2,2,1).setFill(GBC.BOTH).setWeight(0, 100));
this.add(textArea,new GBC(2,2,6,1).setFill(GBC.BOTH).setWeight(100, 100));
}
public static void main(String[] args) {
new Test2().setVisible(true);
}
}
Test3
package Calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame{
private JLabel _label;
private JPanel _containerP;
private StringBuffer _strBuffer;
public Calculator (){
super();
this.setBounds(200,200,300, 350);
_containerP = new JPanel ();
_containerP.setLayout(new GridBagLayout());
this.add(_containerP);
this.initComponents();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
private void initComponents(){
//第0行
_label = new JLabel("结果");
_label.setHorizontalAlignment(SwingConstants.CENTER);
_containerP.add(_label, new GBC(0,0,4,1).setWeight(100, 100).setFill(GBC.BOTH));
//第一行
_containerP.add(new JButton("AC"),new GBC(0,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("+/-"),new GBC(1,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("%"),new GBC(2,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("÷"),new GBC(3,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
//第二行
_containerP.add(new JButton("7"),new GBC(0,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("8"),new GBC(1,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("9"),new GBC(2,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("x"),new GBC(3,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
//第三行
_containerP.add(new JButton("4"),new GBC(0,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("5"),new GBC(1,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("6"),new GBC(2,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("-"),new GBC(3,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
//第四行
_containerP.add(new JButton("1"),new GBC(0,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("2"),new GBC(1,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("3"),new GBC(2,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("+"),new GBC(3,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
//第五行
_containerP.add(new JButton("0"),new GBC(0,5,2,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("."),new GBC(2,5,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_containerP.add(new JButton("="),new GBC(3,5,1,1).setWeight(100, 100).setFill(GBC.BOTH));
_strBuffer = new StringBuffer();
for (int i = 0;i<_containerP.getComponents().length;i++){
Object obj = _containerP.getComponent(i);
if (obj instanceof JButton){
((JButton) obj).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
switch (btn.getText()){
case "=":
_strBuffer.setLength(0);
_label.setText("哈哈,功能没实现!");
break;
default:
_strBuffer.append(((JButton) obj).getText());
_label.setText(_strBuffer.toString());
break;
}
}
});
}
}
}
public static void main(String[] args) {
new Calculator().setVisible(true);
}
}
GBC
package 文件选择器;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class GBC extends GridBagConstraints{
//初始化左上角位置
public GBC(int gridx, int gridy)
{
this.gridx = gridx;
this.gridy = gridy;
}
//初始化左上角位置和所占行数和列数
public GBC(int gridx, int gridy, int gridwidth, int gridheight)
{
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}
//对齐方式
public GBC setAnchor(int anchor)
{
this.anchor = anchor;
return this;
}
//是否拉伸及拉伸方向
public GBC setFill(int fill)
{
this.fill = fill;
return this;
}
//x和y方向上的增量
public GBC setWeight(double weightx, double weighty)
{
this.weightx = weightx;
this.weighty = weighty;
return this;
}
//外部填充
public GBC setInsets(int distance)
{
this.insets = new Insets(distance, distance, distance, distance);
return this;
}
//外填充
public GBC setInsets(int top, int left, int bottom, int right)
{
this.insets = new Insets(top, left, bottom, right);
return this;
}
//内填充
public GBC setIpad(int ipadx, int ipady)
{
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}