Java Swing组件布局管理器之FlowLayout(流式布局)入门教程
作者:pzy4447 时间:2021-11-08 05:33:06
本文实例讲述了Java Swing组件布局管理器之FlowLayout(流式布局)。分享给大家供大家参考,具体如下:
FlowLayout应该是Swing布局管理器学习中最简单、最基础的一个。所谓流式,就是内部控件像水流一样,从前到后按顺序水平排列,直到达到容器的宽度时跳转到第二行。既然是水平排列,那么就存在三种基本的对齐方式:居中对齐(CENTER )、左对齐(LEFT )和右对齐(RIGHT )。然而,FlowLayout还提供两种对齐方式:LEADING,表示控件与容器方向开始边对应;TRAILING,控件与容器方向结束边对应。setAlignment(int align)用于设置对齐方式。在一般情况下,LEADING就是左对齐,TRAILING就是右对齐。除此之外,FlowLayout还可以对内部控件之间、内部控件与容器之间的间距进行设置,setHgap(int hgap)用于指定水平间距;setVgap(int vgap)用于指定垂直间距。
FlowLayout常用方法如下:
构造函数 | ||
名称 | 用途 | |
FlowLayout() | 构造一个新的 FlowLayout,它是默认居中对齐的,默认的水平和垂直间隙是5个像素 | |
FlowLayout(int align) | 构造一个新的 FlowLayout,它具有指定的对齐方式,默认的水平和垂直间隙是 5 个像素 五个参数值及含义如下: 0或FlowLayout.lEFT ,控件左对齐 1或FlowLayout.CENTER ,居中对齐 2或FlowLayout.RIGHT ,右对齐 3或FlowLayout.LEADING,控件与容器方向开始边对应 4或FlowLayout.TRAILING,控件与容器方向结束边对应 如果是0、1、2、3、4之外的整数,则为左对齐 | |
FlowLayout(int align, int hgap, int vgap) | 创建一个新的流布局管理器,它具有指定的对齐方式以及指定的水平和垂直间隙。 |
基本方法 | |
名称 | 用途 |
Void setAlignment(int align) | 设置此布局的对齐方式。 |
void setHgap(int hgap) | 设置组件之间以及组件与 Container 的边之间的水平间隙。 |
void setVgap(int vgap) | 设置组件之间以及组件与 Container 的边之间的垂直间隙。 |
测试用例如下:
package awtDemo;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* @功能:演示FlowLayout布局管理器的用法
* @版本:20150609
*/
@SuppressWarnings("serial")
public class FlowLayoutDemo extends JFrame {
FlowLayout contentPanelLayout = new FlowLayout();
Map<String, Integer> alignmentMap = new HashMap<String, Integer>();
JPanel configPanel = new JPanel();
JPanel contentPanel = new JPanel();
JComboBox<String> alignmentComboBox = new JComboBox<String> ();
JTextField textHgap = new JTextField("10");
JTextField textVgap = new JTextField("20");
MyListener myListener = new MyListener();
public FlowLayoutDemo() {
//init
alignmentMap.put("LEFT", 0);
alignmentMap.put("CENTER", 1);
alignmentMap.put("RIGHT", 2);
alignmentMap.put("LEADING", 3);
alignmentMap.put("TRAILING", 4);
//设置面板
configPanel.setLayout(new FlowLayout());
configPanel.add(new JLabel("对齐方式"));
for (String alignment : alignmentMap.keySet()) {
alignmentComboBox.addItem(alignment);
}
configPanel.add(alignmentComboBox);
configPanel.add(new JLabel("水平间距"));
configPanel.add(textHgap);
configPanel.add(new JLabel("垂直间距"));
configPanel.add(textVgap);
JButton actionBtn = new JButton("Action!!!");
actionBtn.addActionListener(myListener);
configPanel.add(actionBtn);
//展示面板
contentPanel.setLayout(contentPanelLayout);
contentPanel.add(new JButton("Button 1"));
contentPanel.add(new JButton("Button 2"));
contentPanel.add(new JButton("Button 3"));
contentPanel.add(new JButton("Button 4"));
//主窗体
setLayout(new BorderLayout());
add("North",configPanel);
add("South", contentPanel);
}
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String alignmentStr = alignmentComboBox.getSelectedItem().toString();
int alignment = alignmentMap.get(alignmentStr);
contentPanelLayout.setAlignment(alignment);
int hgap = Integer.valueOf(textHgap.getText());
int vgap = Integer.valueOf(textVgap.getText());
contentPanelLayout.setHgap(hgap);
contentPanelLayout.setVgap(vgap);
contentPanel.updateUI();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FlowLayoutDemo window = new FlowLayoutDemo();
window.setTitle("FlowLayoutDemo - www.jb51.net");
// 该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
window.setPreferredSize(new Dimension(500, 200));
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null); // 让窗体居中显示
}
}
运行效果如下:
希望本文所述对大家java程序设计有所帮助。
来源:http://www.cnblogs.com/pzy4447/p/4696703.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android应用开发SharedPreferences存储数据的使用方法
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
Spring Boot整合Mybatis并完成CRUD操作的实现示例
![](https://img.aspxhome.com/file/2023/7/59377_0s.png)
常用Maven库,镜像库及maven/gradle配置(小结)
用代码更新你的jar包
Java多线程Atomic包操作原子变量与原子类详解
SpringBoot线程池和Java线程池的使用和实现原理解析
Java如何解析html中的内容并存到数据库详解
android 6.0 写入SD卡的权限申请实例讲解
DUCC配置平台实现一个动态化线程池示例代码
![](https://img.aspxhome.com/file/2023/4/60354_0s.png)
linux下C语言中的mkdir函数与rmdir函数
详解Spring框架下向异步线程传递HttpServletRequest参数的坑
Java 实现判定顺序表中是否包含某个元素(思路详解)
![](https://img.aspxhome.com/file/2023/4/58474_0s.png)
解决idea爆红 cant resolve symbol String的问题解析
![](https://img.aspxhome.com/file/2023/5/58615_0s.png)
Java Object类中的常用API介绍
Android实现双曲线折线图
![](https://img.aspxhome.com/file/2023/2/85462_0s.jpg)
详解ThreadLocal为什么会内存溢出原理
![](https://img.aspxhome.com/file/2023/3/59183_0s.png)
解决Mybatis中foreach嵌套使用if标签对象取值的问题
SpringBoot项目@Async方法问题解决方案
![](https://img.aspxhome.com/file/2023/2/58872_0s.png)
Java实现FTP文件与文件夹的上传和下载
Maven学习----Maven安装与环境变量配置教程
![](https://img.aspxhome.com/file/2023/8/60678_0s.png)