Java Swing组件布局管理器之FlowLayout(流式布局)入门教程

2025-05-27 0 31

本文实例讲述了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 的边之间的垂直间隙。


测试用例如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86
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.zzvips.com");

// 该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件

window.setPreferredSize(new Dimension(500, 200));

window.pack();

window.setVisible(true);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLocationRelativeTo(null); // 让窗体居中显示

}

}

运行效果如下:

Java Swing组件布局管理器之FlowLayout(流式布局)入门教程

希望本文所述对大家java程序设计有所帮助。

原文链接:http://www.cnblogs.com/pzy4447/p/4696703.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java Swing组件布局管理器之FlowLayout(流式布局)入门教程 https://www.kuaiidc.com/77460.html

相关文章

发表评论
暂无评论