Java Swing面板布局之流式布局FlowLayout

本文详细介绍了Swing布局管理器FlowLayout的使用方法,包括其构造函数、常用方法,并通过实例演示了不同对齐方式和间距设置的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简介

FlowLayout应该是Swing布局管理器学习中最简单、最基础的一个。所谓流式,就是内部控件像水流一样,从前到后按顺序水平排列,直到达到容器的宽度时跳转到第二行。既然是水平排列,那么就存在三种基本的对齐方式:居中对齐(CENTER )、左对齐(LEFT )和右对齐(RIGHT )。然而,FlowLayout还提供两种对齐方式:LEADING,表示控件与容器方向开始边对应;TRAILING,控件与容器方向结束边对应。setAlignment(int align)用于设置对齐方式。在一般情况下,LEADING就是左对齐,TRAILING就是右对齐。除此之外,FlowLayout还可以对内部控件之间、内部控件与容器之间的间距进行设置,setHgap(int hgap)用于指定水平间距;setVgap(int vgap)用于指定垂直间距。

二、构造函数

1. FlowLayout()

 构造一个新的 FlowLayout,它是默认居中对齐的,默认的水平和垂直间隙是5个像素

2. 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之外的整数,则为左对齐

3. FlowLayout(int align, int hgap, int vgap)

创建一个新的流布局管理器,它具有指定的对齐方式以及指定的水平和垂直间隙。

三、常用方法

void setAlignment(int align):设置此布局的对齐方式。

void setHgap(int hgap):设置组件之间以及组件与 Container 的边之间的水平间隙。

void setVgap(int vgap):设置组件之间以及组件与 Container 的边之间的垂直间隙

四、举例

import java.awt.*;

import javax.swing.*;

public class TestFlowLayout01 extends JFrame{

    public static void main(String[] args) {

       new TestFlowLayout01();

    }

    private static final long serialVersionUID = 1L;

    public TestFlowLayout01(){

       initFrame();

    }

    private void initFrame(){

       this.setTitle("流式布局测试");

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       addComponents();

       this.setSize(600, 400);

       this.setLocationRelativeTo(null);

       this.setVisible(true);

    }

    private void addComponents(){

       this.setLayout(new GridLayout(3, 1));

       JPanel panel01 = new JPanel(new FlowLayout(FlowLayout.LEADING, 2, 2));

       panel01.setBackground(new Color(255, 228, 181));

       this.add(panel01);

       JPanel panel02 = new JPanel(new FlowLayout(FlowLayout.CENTER, 6, 6));

       panel02.setBackground(new Color(255, 250, 205));

       this.add(panel02);

       JPanel panel03 = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 10));

       panel03.setBackground(new Color(255, 228, 181));

       this.add(panel03);

       List<JButton> btns = new ArrayList<>();

       JButton btn = null;

       for(int i=1;i<=9;i++){

           btn = new JButton("按钮1" + i);

           panel01.add(btn);

           btns.add(btn);

       }

       for(int i=1;i<=9;i++){

           btn = new JButton("按钮2" + i);

           panel02.add(btn);

           btns.add(btn);

       }

       for(int i=1;i<=9;i++){

           btn = new JButton("按钮3" + i);

           panel03.add(btn);

           btns.add(btn);

       }

       for (JButton jButton : btns) {

           jButton.setFocusPainted(false);

           jButton.setOpaque(true);

           jButton.setBackground(new Color(144, 238, 144));

           jButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

       }

    }

}

Java中,可以使用Swing库中的JFrame和JButton组件来创建图形用户界面(GUI),并且使用FileReader和BufferedReader类来实现流读取文本文件。以下是一个简单的示例代码,展示了如何创建一个窗口,其中包含一个按钮,用户点击按钮后,程序将流读取一个txt文件并显示其内容。 ```java import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; public class ReadFileWithButton extends JFrame { private JTextArea textArea; private BufferedReader reader; private JButton readButton; public ReadFileWithButton() { // 创建文本区域用于显示文件内容 textArea = new JTextArea(); textArea.setEditable(false); // 创建按钮 readButton = new JButton("读取文件"); readButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { readFile(); } }); // 创建面板并添加组件 JPanel panel = new JPanel(); panel.add(readButton); add(panel, BorderLayout.NORTH); add(new JScrollPane(textArea), BorderLayout.CENTER); // 设置窗口属性 setTitle("读取文件示例"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 窗口居中 } private void readFile() { // 文件路径 String filePath = "example.txt"; try { // 使用FileReader和BufferedReader流读取文件 reader = new BufferedReader(new FileReader(filePath)); String line; textArea.setText(""); // 清空文本区域 while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); // 将读取的每一行添加到文本区域 } reader.close(); // 关闭流 } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "文件未找到,请检查路径。", "错误", JOptionPane.ERROR_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(this, "读取文件时发生错误。", "错误", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { // 在事件调度线程中运行GUI创建代码,以确保线程安全 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ReadFileWithButton readFileWithButton = new ReadFileWithButton(); readFileWithButton.setVisible(true); } }); } } ``` 这段代码创建了一个带有文本区域和按钮的简单GUI。点击按钮时,会触发`readFile`方法,该方法尝试打开一个名为"example.txt"的文件并将其内容读取到文本区域中。请注意,你需要将"example.txt"替换为你想要读取的文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值