JAVA Swing常见的布局管理器
来源:互联网 发布:淘宝旺铺助手 编辑:程序博客网 时间:2024/05/16 02:09
JAVA Swing常见的布局管理器
1.GroupLayout
GroupLayout是以组为单位来管理布局的,把多个组件划分到不容的组,再根据各个组相对于水平轴和垂直轴的排列方式来进行组的管理。
使用GroupLayout布局管理器进行布局
import java.awt.Container;import java.awt.HeadlessException;import javax.swing.GroupLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;public class GroupLayoutDemo extends JFrame {private static final long serialVersionUID = 1L;public GroupLayoutDemo() throws HeadlessException { Container c = getContentPane();//创建一个中间容器,并且创建一个GroupLayout布局管理器对象 GroupLayout layout = new GroupLayout(c); JButton b1 = new JButton("按钮 1");//创建两个普通按钮组件、文本框组件 JButton b2 = new JButton("按钮 2"); JTextField text = new JTextField("文本"); GroupLayout.SequentialGroup hsg = layout.createSequentialGroup();//创建一个hsg组,将两个按钮一个一个的添加到组里面 hsg.addComponent(b1); hsg.addComponent(b2); GroupLayout.ParallelGroup hpg = layout.createParallelGroup(GroupLayout.Alignment.CENTER); //创建一个hpg组,将文本框组件和上面的那个组添加到其中,并且居中排列 hpg.addComponent(text).addGroup(hsg); layout.setHorizontalGroup(hpg); //沿水平线来确定hpg组中两个按钮组件的位置 GroupLayout.ParallelGroup vpg = layout.createParallelGroup();//创建一个vpg组,按照水平线来排列两个按钮组件的位置 vpg.addComponent(b1); vpg.addComponent(b2); GroupLayout.SequentialGroup vsg = layout.createSequentialGroup();// 将文本框组件和前面的容纳两个按钮组件的组同时添加到vsg组中 vsg.addComponent(text).addGroup(vpg); layout.setVerticalGroup(vsg); //沿垂直线来确定vsg中vpg和文本框组件的位置 this.setLayout(layout); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack();}public static void main(String[] args) {GroupLayoutDemo demo = new GroupLayoutDemo();demo.setVisible(true);}}
2.BoxLayout
BoxLayout是箱式布局,它可以创建水平箱和垂直箱两种箱子
import javax.swing.*;import java.awt.*;public class BoxLayoutDemo{ public static void main(String[] args) { BoxLayoutFrame frame1=new BoxLayoutFrame(); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setVisible(true); }}class BoxLayoutFrame extends JFrame{ private static final int WIDTH=300; private static final int HEIGHT=200; public BoxLayoutFrame() { setTitle("测试箱式布局管理器");//设置顶层容器名称、大小 setSize(WIDTH,HEIGHT); Container con=getContentPane();//创建一个中间容器 JLabel label1=new JLabel(" 姓名:");//创建标签组件、文本框组件 JTextField textField1=new JTextField(10); textField1.setMaximumSize(textField1.getPreferredSize()); Box hbox1=Box.createHorizontalBox();//创建一个水平箱子 hbox1.add(label1); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox1.add(Box.createHorizontalStrut(20)); hbox1.add(textField1); JLabel label2=new JLabel(" 密码:");//创建标签组件、文本框组件 JTextField textField2=new JTextField(10); textField2.setMaximumSize(textField2.getPreferredSize()); Box hbox2=Box.createHorizontalBox();//创建一个水平箱子 hbox2.add(label2); //在水平箱子上添加一个标签组件,并且创建一个不可见的、20个单位的组件。在这之后再添加一个文本框组件 hbox2.add(Box.createHorizontalStrut(20)); hbox2.add(textField2); JButton button1=new JButton("确定");//创建两个普通按钮组件,并且创建一个水平箱子,将两个按钮添加到箱子中 JButton button2=new JButton("取消"); Box hbox3=Box.createHorizontalBox(); hbox3.add(button1); hbox3.add(button2); Box vbox=Box.createVerticalBox();//创建一个垂直箱子,这个箱子将两个水平箱子添加到其中,创建一个横向 glue 组件。 vbox.add(hbox1); vbox.add(hbox2); vbox.add(Box.createVerticalGlue()); vbox.add(hbox3); con.add(vbox,BorderLayout.CENTER); // 将垂直箱子添加到BorderLayout布局管理器中的中间位置 }}
3.CardLayout
CardLayout将容器中的每一个组件当做一个卡片,每次仅有一个卡片可见,卡片之间可以来回切换
import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.UIManager;public class CardLayoutDemo extends JFrame { private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout private JPanel p = null; // 放按钮的JPanel private CardLayout card = null; // CardLayout布局管理器 private JButton button_1 = null; // 上一步 private JButton button_2 = null; // 下一步 private JButton b_1 = null, b_2 = null, b_3 = null; // 三个可直接翻转到JPanel组件的按钮 private JPanel p_1 = null, p_2 = null, p_3 = null; // 要切换的三个JPanel public CardLayoutDemo () { super("CardLayout Test"); try { // 将LookAndFeel设置成Windows样式 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { ex.printStackTrace(); } card = new CardLayout(5, 5); //创建一个具有指定的水平和垂直间隙的新卡片布局 pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayout p = new JPanel(); // 构造放按钮的JPanel button_1 = new JButton("< 上一步"); button_2 = new JButton("下一步 >"); b_1 = new JButton("1"); b_2 = new JButton("2"); b_3 = new JButton("3"); b_1.setMargin(new Insets(2,2,2,2)); b_2.setMargin(new Insets(2,2,2,2)); b_3.setMargin(new Insets(2,2,2,2)); p.add(button_1); p.add(b_1); p.add(b_2); p.add(b_3); p.add(button_2); p_1 = new JPanel(); p_2 = new JPanel(); p_3 = new JPanel(); p_1.setBackground(Color.RED); p_2.setBackground(Color.BLUE); p_3.setBackground(Color.GREEN); p_1.add(new JLabel("JPanel_1")); p_2.add(new JLabel("JPanel_2")); p_3.add(new JLabel("JPanel_3")); pane.add(p_1, "p1"); pane.add(p_2, "p2"); pane.add(p_3, "p3"); //下面是翻转到卡片布局的某个组件的动作事件处理,当单击某个普通按钮组件,就会触发出现下一个组件 button_1.addActionListener(new ActionListener(){ /// 上一步的按钮动作 public void actionPerformed(ActionEvent e) { card.previous(pane); } }); button_2.addActionListener(new ActionListener(){// 下一步的按钮动作 public void actionPerformed(ActionEvent e) { card.next(pane); } }); b_1.addActionListener(new ActionListener() {// 直接翻转到p_1 public void actionPerformed(ActionEvent e) { card.show(pane, "p1"); } }); b_2.addActionListener(new ActionListener() { // 直接翻转到p_2 public void actionPerformed(ActionEvent e) { card.show(pane, "p2"); } }); b_3.addActionListener(new ActionListener() { // 直接翻转到p_3 public void actionPerformed(ActionEvent e) { card.show(pane, "p3"); } }); this.getContentPane().add(pane); this.getContentPane().add(p, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } public static void main(String[] args) { new CardLayoutDemo (); }}
4.GridLayout
GridLayout将整个空间划分成若干行乘若干列的网络区域,组件就放于这些小区域内。
import javax.swing.*;import java.awt.*;public class GridLayoutDemo1{ static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); JButton b6=new JButton("主板"); JButton b7=new JButton("内存"); JButton b8=new JButton("硬盘"); JButton b9=new JButton("显示器"); GridLayout gird=new GridLayout(3,3); //创建一个 GridLayout布局管理器对象,将之行数设为3,列数设为3,并且将之作为中间容器的布局管理器 contentPane.setLayout(gird); contentPane.add(b1); //将九个普通按钮组件一一添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); contentPane.add(b6); contentPane.add(b7); contentPane.add(b8); contentPane.add(b9); jf.pack(); } }
5.FlowLayout
FlowLayout按照组件加入的先后顺序从左到右排列,一行徘满了,再换下一行,然后继续从左到右排列,每一行的组件都是居中排列的。
import javax.swing.*;import java.awt.*;public class FlowLayoutDemo1{ static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args) { JFrame jf=new JFrame("测试程序"); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); JPanel contentPane=new JPanel(); jf.setContentPane(contentPane); JButton b1=new JButton("港币"); JButton b2=new JButton("人民币"); JButton b3=new JButton("美元"); JButton b4=new JButton("欧元"); JButton b5=new JButton("英镑"); contentPane.setLayout(new FlowLayout());//将中间容器的布局管理器设置为FlowLayout contentPane.add(b1); //将五个按钮分别按照FlowLayout布局管理器方式添加到中间容器中 contentPane.add(b2); contentPane.add(b3); contentPane.add(b4); contentPane.add(b5); jf.pack(); }}
阅读全文
1 0
- JAVA Swing常见的布局管理器
- swing的几种常见布局管理器
- Java Swing 布局管理器
- java--Swing 组件的使用 布局管理器
- Swing的布局管理器
- Swing的布局管理器
- JAVA swing BoxLayout 布局管理器
- java swing 1.6布局管理器
- java swing 边界布局管理器
- Swing的布局管理器 --转载
- java Swing布局管理器之FlowLayout布局
- java Swing布局管理器之BorderLayout布局
- java Swing布局管理器之CardLayout布局
- Java 的swing.GroupLayout布局管理器的使用方法和实例
- Java 的swing.GroupLayout布局管理器的使用方法和实例
- Java 的swing.GroupLayout布局管理器的使用方法和实例
- 谈谈Java Swing中布局管理器
- Java Swing学习笔记--GUI布局管理器
- 用宏定义来完成运算(本文来自百度文库)
- JS
- Mac xelatex
- Java 5*5数组的三种排列例子
- 创新实训5.28 代码转交&实时评测
- JAVA Swing常见的布局管理器
- OOD对ATM分析类进行补充、调整
- 编程第四十一、四十二天
- webutil1.0版本下,t400电脑,一些性能问题
- A Discriminative Feature Learning Approach for Deep Face Recognition
- OpenCV笔记大集锦
- (148)光束
- Magento 2中文手册教程
- npm hotnode 踩坑