21xrx.com
2024-11-22 10:13:03 Friday
登录
文章检索 我的文章 写文章
Java语言常用布局管理器及其案例
2023-06-17 10:05:05 深夜i     --     --
Java语言 布局管理器 案例

Java语言中常用的布局管理器有FlowLayout、BorderLayout、GridLayout、CardLayout、GridBagLayout等。下面分别介绍这几种布局管理器的使用方法和案例。

1. FlowLayout

FlowLayout是最简单的一种布局管理器,它会按照组件的加入顺序自左向右排列,直到一行排满为止,然后自动换行。示例代码如下:


import javax.swing.*;

import java.awt.*;

public class FlowLayoutDemo {

  public static void main(String[] args) {

    JFrame frame = new JFrame("FlowLayoutDemo");

    JPanel panel = new JPanel(new FlowLayout());

    panel.add(new JButton("Button1"));

    panel.add(new JButton("Button2"));

    panel.add(new JButton("Button3"));

    panel.add(new JButton("Button4"));

    panel.add(new JButton("Button5"));

    frame.add(panel);

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

2. BorderLayout

BorderLayout将容器分为五个区域:北、南、东、西、中。添加组件时需要指定组件位于哪一个方位。示例代码如下:


import javax.swing.*;

import java.awt.*;

public class BorderLayoutDemo {

  public static void main(String[] args) {

    JFrame frame = new JFrame("BorderLayoutDemo");

    JPanel panel = new JPanel(new BorderLayout());

    panel.add(new JButton("North"), BorderLayout.NORTH);

    panel.add(new JButton("South"), BorderLayout.SOUTH);

    panel.add(new JButton("East"), BorderLayout.EAST);

    panel.add(new JButton("West"), BorderLayout.WEST);

    panel.add(new JButton("Center"), BorderLayout.CENTER);

    frame.add(panel);

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

3. GridLayout

GridLayout将容器按照行列格子划分,每个格子放置一个组件。示例代码如下:


import javax.swing.*;

import java.awt.*;

public class GridLayoutDemo {

  public static void main(String[] args) {

    JFrame frame = new JFrame("GridLayoutDemo");

    JPanel panel = new JPanel(new GridLayout(2, 3));

    panel.add(new JButton("Button1"));

    panel.add(new JButton("Button2"));

    panel.add(new JButton("Button3"));

    panel.add(new JButton("Button4"));

    panel.add(new JButton("Button5"));

    panel.add(new JButton("Button6"));

    frame.add(panel);

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

4. CardLayout

CardLayout将容器中的组件按照卡片的形式堆叠起来,只显示最上层的组件,其余组件被遮挡。示例代码如下:


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class CardLayoutDemo {

  public static void main(String[] args) {

    JFrame frame = new JFrame("CardLayoutDemo");

    JPanel panel = new JPanel(new CardLayout());

    JButton button1 = new JButton("Button1");

    JButton button2 = new JButton("Button2");

    JButton button3 = new JButton("Button3");

    button1.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        CardLayout layout = (CardLayout) panel.getLayout();

        layout.show(panel, "Button2");

      }

    });

    button2.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        CardLayout layout = (CardLayout) panel.getLayout();

        layout.show(panel, "Button3");

      }

    });

    button3.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        CardLayout layout = (CardLayout) panel.getLayout();

        layout.show(panel, "Button1");

      }

    });

    panel.add(button1, "Button1");

    panel.add(button2, "Button2");

    panel.add(button3, "Button3");

    frame.add(panel);

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

5. GridBagLayout

GridBagLayout是最复杂的一种布局管理器,通过使用行和列上的网格来定位组件。示例代码如下:


import javax.swing.*;

import java.awt.*;

public class GridBagLayoutDemo {

  public static void main(String[] args) {

    JFrame frame = new JFrame("GridBagLayoutDemo");

    JPanel panel = new JPanel(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.weightx = 1.0;

    gbc.weighty = 1.0;

    JButton button1 = new JButton("Button1");

    gbc.gridx = 0;

    gbc.gridy = 0;

    gbc.gridwidth = 2;

    gbc.fill = GridBagConstraints.BOTH;

    panel.add(button1, gbc);

    JButton button2 = new JButton("Button2");

    gbc.gridx = 2;

    gbc.gridy = 0;

    gbc.gridwidth = 1;

    gbc.fill = GridBagConstraints.BOTH;

    panel.add(button2, gbc);

    JButton button3 = new JButton("Button3");

    gbc.gridx = 0;

    gbc.gridy = 1;

    gbc.gridwidth = 1;

    gbc.fill = GridBagConstraints.BOTH;

    panel.add(button3, gbc);

    JButton button4 = new JButton("Button4");

    gbc.gridx = 1;

    gbc.gridy = 1;

    gbc.gridwidth = 1;

    gbc.fill = GridBagConstraints.BOTH;

    panel.add(button4, gbc);

    JButton button5 = new JButton("Button5");

    gbc.gridx = 2;

    gbc.gridy = 1;

    gbc.gridwidth = 1;

    gbc.fill = GridBagConstraints.BOTH;

    panel.add(button5, gbc);

    frame.add(panel);

    frame.setSize(300, 200);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复