Swing布局介绍
Java 的Swing 布局模式
-
流式布局
-
网格布局
-
网格袋布局
-
箱式布局
-
分组布局
-
卡片布局
-
边界布局
-
绝对布局
效果展示
功能实现
我们今天将用到 网格袋 布局来实现以上功能。
由此可见我们分析以上效果图有8个控件
分别是:
- Jlabel(标签) 3个
- JRadioButton(单选按钮) 2个
- JCheckBox(多选框) 2个
- JButton(按钮) 1个
网格袋布局(GridBagLayout)
- GridBagLayout(布局)
- GridBagConstraints(约束)
代码如下:
//窗口
JFrame jf = new JFrame("测试窗体");
//网格袋布局
GridBagLayout gridBagLayout = new GridBagLayout();
//约束
GridBagConstraints gridBagConstraints = null;
jf.setLayout(new FlowLayout(FlowLayout.LEFT));
重点聊聊约束
网格袋布局的约束方式实际上就是给每个控件定义相应的布局规则,即在”显示区域“内进行一个相应布局方法。
说的很抽象,我们看看约束相关属性
-
gridx, gridy
单元格区域内的坐标轴,比如(0,0). -
gridwidth, gridheight
在相关单元格显示区域的宽高 -
weightx, weighty
分布占比额外空间(单元格区域外,容器边缘内)
常用属性值:REMAINDER(占满所有区域)、.RELATIVE(填充所剩区域)
问题来了
关于显示区域内的控件比显示区域要大于或小于组件大小的时候我们该怎么办?
答案
这点网格袋布局也想到了,所以有着组件的约束属性。
fill 分别是:
- NONE (默认)
- HORIZONTAL (加宽)
- VERTICAL(加高)
- BOTH(自适应填充)
根据上面的学习,我们对Swing的网格袋布局有了一个了解。那么我们开始动手把!
代码实现
1.1 定义网格袋布局
//窗口
JFrame jf = new JFrame("测试窗体");
//大小
jf.setSize(300,350);
//网格袋布局
GridBagLayout gridBagLayout = new GridBagLayout();
//网格袋约束
GridBagConstraints gridBagConstraints = null;
//设置布局模式
jf.setLayout(gridLayout);
1.2 控件操作
//定义板
JPanel jPanel = new JPanel(gridBagLayout);
//标签Component
JLabel label = new JLabel();
//设置字符串文本
label.setText("个人信息管理");
gridBagConstraints = new GridBagConstraints();
//显示区域宽度
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
//填充方式
gridBagConstraints.fill = GridBagConstraints.BOTH;
//添加约束
gridBagLayout.addLayoutComponent(label,gridBagConstraints);
label.setFont(new Font(null,Font.BOLD,25));
//按钮组
ButtonGroup group = new ButtonGroup();
JLabel sexMsg = new JLabel();
label.setText("个人信息管理");
//xy width height
label.setBounds(10,30,100,100);
label.setFont(new Font(null,Font.BOLD,25));
sexMsg.setText("性别是:");
sexMsg.setFont(new Font(null,Font.BOLD,25));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridheight = 2;
gridBagLayout.addLayoutComponent(sexMsg,gridBagConstraints);
JRadioButton sexMan = new JRadioButton();
sexMan.setText("男");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagLayout.addLayoutComponent(sexMan,gridBagConstraints);
sexMan.setFont(new Font(null,Font.BOLD,29));
JRadioButton sexLady = new JRadioButton();
sexLady.setText("女");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagLayout.addLayoutComponent(sexLady,gridBagConstraints);
sexMan.setSelected(true);
sexLady.setFont(new Font(null,Font.BOLD,29));
JButton submit = new JButton("提交");
submit.setBackground(Color.lightGray);
submit.setForeground(Color.WHITE);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被按下");
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagLayout.addLayoutComponent(submit,gridBagConstraints);
JLabel students = new JLabel("编程学习语言");
students.setFont(new Font(null,Font.BOLD,25));
students.setForeground(Color.PINK);
//网格带
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridheight = 2;
gridBagLayout.addLayoutComponent(students,gridBagConstraints);
JCheckBox java = new JCheckBox("Java");
JCheckBox c = new JCheckBox("c");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridheight = 2;
gridBagLayout.addLayoutComponent(java,gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridheight = 2;
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagLayout.addLayoutComponent(c,gridBagConstraints);
java.setFont(new Font(null,Font.BOLD,25));
c.setFont(new Font(null,Font.BOLD,25));
java.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JCheckBox checkBox = (JCheckBox)e.getSource();
System.out.println("文本:" + checkBox.getText() + " 是否被选中:" + checkBox.isSelected());
}
});
submit.setPreferredSize(new Dimension(100, 30));
group.add(sexMan);
group.add(sexLady);
jPanel.add(label);
jPanel.add(sexMsg);
jPanel.add(sexMan);
jPanel.add(sexLady);
jPanel.add(students);
jPanel.add(java);
jPanel.add(c);
jPanel.add(submit);
jf.setContentPane(jPanel);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}catch (Exception e){
e.printStackTrace();
}
结束语
注意事项
网格袋布局布局方式非常简单,主要了解清楚约束相关属性。
参考文献:
CSDN有着很经典的教学文章
- xietansheng(大神)