JAVA——45.flowlayout和gridlayout布局

发布时间:2023-05-25 09:17:29

练习一、flowlayout布局(JPanel默认布局为流式布局) FlowLayout:流式布局(顺着放下),比如按钮一个接一个地放在面板上,当然是在中间显示的,一行满了,放在下一行

package org.zhaiyujia.test1;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Guitest1 extends JFrame {          JButton b1,b2,b3,b4,b5;          JPanel p;          public Guitest1() {          //FlowLayout:例如,按钮一个接一个地放在面板上,当然是在中间显示的,放满一行,放在下一行。          b1=new JButton(Button1);          b2=new JButton(Button2);          b3=new JButton(Button3);          b4=new JButton(Button4);          b5=new JButton(Button5);          p=new JPanel();          this.getContentPane().add(p);///获取窗口对象的内容面板对象,再添加p          FlowLayout layout=new FlowLayout();          p.setLayout(layout);          p.add(b1);          p.add(b2);          p.add(b3);          p.add(b4);          p.add(b5);          this.setSize(300, 300);          this.setVisible(true);          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          }          public static void main(String[] args) {              new Guitest1();          }}

JAVA——45.flowlayout和gridlayout布局_流式布局

 

练习二、GridLayout:网格布局,几行几列模式

构造方法:GridLayout(int rows, int cols) :用指定的行数和列数创建网格布局。

package org.zhaiyujia.test1;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Guitest1 extends JFrame {          JButton b1,b2,b3,b4,b5;          JPanel p;          public Guitest1() {          //FlowLayout:例如,按钮一个接一个地放在面板上,当然是在中间显示的,放满一行,放在下一行。          b1=new JButton(Button1);          b2=new JButton(Button2);          b3=new JButton(Button3);          b4=new JButton(Button4);          b5=new JButton(Button5);          p=new JPanel();          this.getContentPane().add(p);///获取窗口对象的内容面板对象,再添加p          //FlowLayout layout=new FlowLayout();          //p.setLayout(layout);          //GridLayout:网格布局          GridLayout layout=new GridLayout(2,3);//两行三列          p.setLayout(layout);          p.add(b1);          p.add(b2);          p.add(b3);          p.add(b4);          p.add(b5);          this.setSize(300, 300);          this.setVisible(true);          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          }          public static void main(String[] args) {              new Guitest1();          }}

JAVA——45.flowlayout和gridlayout布局_流式布局_02

练习3,如何使按钮不充满整个网格空间

setHgap(int hgap) :将组件之间的水平间距设置为指定值。

setVgap(int vgap) :将组件之间的垂直间距设置为指定值。

package org.zhaiyujia.test1;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class Guitest1 extends JFrame {          JButton b1,b2,b3,b4,b5;          JPanel p;          public Guitest1() {          //FlowLayout:例如,按钮一个接一个地放在面板上,当然是在中间显示的,放满一行,放在下一行。          b1=new JButton(Button1);          b2=new JButton(Button2);          b3=new JButton(Button3);          b4=new JButton(Button4);          b5=new JButton(Button5);          p=new JPanel();          this.getContentPane().add(p);///获取窗口对象的内容面板对象,再添加p          //FlowLayout layout=new FlowLayout();          //p.setLayout(layout);          //GridLayout:网格布局          GridLayout layout=new GridLayout(2,3);//两行三列          p.setLayout(layout);          layout.setHgap(10);          layout.setVgap(10);          p.add(b1);          p.add(b2);          p.add(b3);          p.add(b4);          p.add(b5);          this.setSize(300, 300);          this.setVisible(true);          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          }          public static void main(String[] args) {              new Guitest1();          }}

上一篇 JAVA——56.输入输出流四-printwriter
下一篇 JAVA——53.输入输出流

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题