使用布局管理器

  FlowLayout管理器

  面板的默认布局管理器是java.awt包中的FlowLayout类。使用FlowLayout时,像在页面中排列英文单词那样排组件:从左到右排列,当前行没有空间后进入下一行。

 import java.awt.*;
import javax.swing.*; public class Crisis extends JFrame {
JButton panicButton;
JButton dontPanicButton;
JButton blameButton;
JButton mediaButton;
JButton saveButton; public Crisis() {
super("Crisis");
setLookAndFeel();
setSize(348, 128);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 1.默认布局
FlowLayout flo = new FlowLayout();
setLayout(flo);
panicButton = new JButton("Panic");
dontPanicButton = new JButton("Don't Panic");
blameButton = new JButton("Blame Others");
mediaButton = new JButton("Notify the Media");
saveButton = new JButton("save yourself");
add(panicButton);
add(dontPanicButton);
add(blameButton);
add(mediaButton);
add(saveButton);
setVisible(true);
} private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
} public static void main(String[] arguments) {
Crisis frame = new Crisis();
}
}

  GridLayout管理器

  GridLayout类位于java.awt包中,它将容器中所有的组件组织为指定的行数和列数。分配给每个组件的显示区域都相同。

  当组件加入到容器中时,GridLayout将所有的组件放置到网格中的某个位置,而且组件是从左到右依次添加,当这一行满了之后,在从下一行的最左边开始添加。

 import java.awt.*;
import javax.swing.*; public class Crisis extends JFrame {
JButton panicButton;
JButton dontPanicButton;
JButton blameButton;
JButton mediaButton;
JButton saveButton; public Crisis() {
super("Crisis");
setLookAndFeel();
setSize(348, 128);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 2.GridLayout布局
GridLayout grid = new GridLayout(2, 3);
setLayout(grid);
panicButton = new JButton("Panic");
dontPanicButton = new JButton("Don't Panic");
blameButton = new JButton("Blame Others");
mediaButton = new JButton("Notify the Media");
saveButton = new JButton("save yourself");
add(panicButton);
add(dontPanicButton);
add(blameButton);
add(mediaButton);
add(saveButton);
setVisible(true);
} private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
} public static void main(String[] arguments) {
Crisis frame = new Crisis();
}
}

BorderLay管理器

  BorderLayout类也位于java.awt包中,它将容器中的组件放置在特定的位置,该位置有5个方位:东、西、南、北、中。

BorderLayout管理器将组件放置到5个位置:其中4个位置由罗盘方向指定,另外一个由中心区域指定。当在该布局下添加组件时,add()方法会包含第2个参数,用于指示组件应该放置的位置。该参数应该是BorderLayout类的5个类变量之一:NORTH、SOUTH、EAST、WEST和CENTER。

  与GridLayout类相同,BorderLayout也会将所有可用空间都分配给组件。在周围放置4个边界组件后,余下的空间都分配给中央的组件,因此它通常是最大的。

 import java.awt.*;
import javax.swing.*; public class Crisis extends JFrame {
JButton panicButton;
JButton dontPanicButton;
JButton blameButton;
JButton mediaButton;
JButton saveButton; public Crisis() {
super("Crisis");
setLookAndFeel();
setSize(348, 128);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout crisisLayout = new BorderLayout();
setLayout(crisisLayout); panicButton = new JButton("Panic");
dontPanicButton = new JButton("Don't Panic");
blameButton = new JButton("Blame Others");
mediaButton = new JButton("Notify the Media");
saveButton = new JButton("save yourself");
add(panicButton, BorderLayout.NORTH);
add(dontPanicButton, BorderLayout.SOUTH);
add(blameButton, BorderLayout.EAST);
add(mediaButton, BorderLayout.WEST);
add(saveButton, BorderLayout.CENTER);
setVisible(true);
} private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
} public static void main(String[] arguments) {
Crisis frame = new Crisis();
}
}

BoxLayout管理器

  BoxLayout类位于javax.swing包中,它可以将组件排列成一行或一列。

  使用该布局时,先创建一个放置组件的面板,然后再创建一个布局管理器,它带有2个参数:

  1.以框式布局组织的组件;

  2.BoxLayout.Y_AXIS指定垂直排列,BoxLayout.X_AXIS指定水平排列;

 import java.awt.*;
import javax.swing.*; public class Crisis extends JFrame {
JButton panicButton;
JButton dontPanicButton;
JButton blameButton;
JButton mediaButton;
JButton saveButton; public Crisis() {
super("Crisis");
setLookAndFeel();
setSize(348, 128);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
pane.setLayout(box);
panicButton = new JButton("Panic");
dontPanicButton = new JButton("Don't Panic");
blameButton = new JButton("Blame Others");
mediaButton = new JButton("Notify the Media");
saveButton = new JButton("save yourself");
pane.add(panicButton);
pane.add(dontPanicButton);
pane.add(blameButton);
pane.add(mediaButton);
pane.add(saveButton);
add(pane); setVisible(true);
} private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
} public static void main(String[] arguments) {
Crisis frame = new Crisis();
}
}

使用Insets将组件隔开

  Insets类位于java.awt包中,它有一个接受4个参数的构造函数:在容器上、下、左、右留出的空间。每个参数都以像素为单位,像素是定义框架大小时使用的度量单位。

     public Insets getInsets() {
Insets squeeze = new Insets(60, 15, 10, 15);
return squeeze;
}

 布局测试

 import java.awt.*;
import javax.swing.*; public class LottoMadness extends JFrame {
// set up row1
JPanel row1 = new JPanel();
ButtonGroup option = new ButtonGroup();
JCheckBox quickpick = new JCheckBox("Quick Pick", false);
JCheckBox personal = new JCheckBox("Personal", true);
// set up row2
JPanel row2 = new JPanel();
JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT);
JTextField[] numbers = new JTextField[6];
JLabel winnersLabel = new JLabel("Winners: ", JLabel.RIGHT);
JTextField[] winners = new JTextField[6];
// set up row3
JPanel row3 = new JPanel();
JButton stop = new JButton("Stop");
JButton play = new JButton("play");
JButton reset = new JButton("Reset");
// set up row4
JPanel row4 = new JPanel();
JLabel got3Label = new JLabel("3 of 6: ", JLabel.RIGHT);
JTextField got3 = new JTextField("0");
JLabel got4Label = new JLabel("4 of 6: ", JLabel.RIGHT);
JTextField got4 = new JTextField("0");
JLabel got5Label = new JLabel("5 of 6: ", JLabel.RIGHT);
JTextField got5 = new JTextField("0");
JLabel got6Label = new JLabel("6 of 6: ", JLabel.RIGHT);
JTextField got6 = new JTextField("0");
JLabel drawingsLabel = new JLabel("Drawings", JLabel.RIGHT);
JTextField drawings = new JTextField("0");
JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
JTextField years = new JTextField(); public LottoMadness() {
super("Lotto Madness"); setSize(550, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(5, 1, 10, 10);
setLayout(layout); FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
option.add(quickpick);
option.add(personal);
row1.setLayout(layout1);
row1.add(quickpick);
row1.add(personal);
add(row1); GridLayout layout2 = new GridLayout(2, 7, 10, 10);
row2.setLayout(layout2);
row2.add(numbersLabel);
for (int i = 0; i < 6; i++) {
numbers[i] = new JTextField();
row2.add(numbers[i]);
}
row2.add(winnersLabel);
for (int i = 0; i < 6; i++) {
winners[i] = new JTextField();
winners[i].setEditable(false);
row2.add(winners[i]);
}
add(row2); FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row3.setLayout(layout3);
stop.setEnabled(false);
row3.add(stop);
row3.add(play);
row3.add(reset);
add(row3); GridLayout layout4 = new GridLayout(2, 3, 20, 10);
row4.setLayout(layout4);
row4.add(got3Label);
got3.setEditable(false);
row4.add(got3);
row4.add(got4Label);
got4.setEditable(false);
row4.add(got4);
row4.add(got5Label);
got5.setEditable(false);
row4.add(got5);
row4.add(got6Label);
got6.setEditable(false);
row4.add(got6);
row4.add(drawingsLabel);
drawings.setEditable(false);
row4.add(drawings);
row4.add(yearsLabel);
years.setEditable(false);
row4.add(years);
add(row4); setVisible(true);
} private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
} public static void main(String[] arguments) {
LottoMadness.setLookAndFeel();
LottoMadness frame = new LottoMadness();
}
}

    

Java学习——用户界面的布局的更多相关文章

  1. java代码用户界面网格布局GridLayout.划分为格子区域

    总结:网格布局.很简单,首先要new一个   this.setlayout(new GriedLayout(3,5));里面是行数和列数 package clientFrame; //网格布局练习 i ...

  2. java图形用户界面BorderLayout布局。冲突

    总结:在使用边界布局发现,把所有的按钮组件都放入了panel.但是在中部的按钮组件找不到了.发现自己重复用了组件 1.this.add(bt4,BorderLayout.North); 2.panel ...

  3. java图形用户界面边界布局管理器

    总结:不同方向的组件,所用的板是不同的: package com.moc; //用布局写一个界面 ///运用边界布局 //2个按钮在北,2个按钮在南 //中央一个大按钮 //将同一方向的组件封装后布局 ...

  4. Java学习知识体系大纲梳理

    感悟 很奇怪,我怎么会想着写这么一篇博客——Java语言的学习体系,这不是大学就已经学过的课程嘛.博主系计算机科班毕业,大学的时候没少捧着Java教程来学习,不管是为了学习编程还是为了期末考个高分,都 ...

  5. Java学习笔记--Swing用户界面组件

    很多与AWT类似. 事件处理参考:Java学习笔记--AWT事件处理 1.设计模式: 模型:存储内容视图:显示内容控制器:处理用户输入· 2. 文本输入常用组件 2.1 文本域: JLabel lab ...

  6. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  7. 201671010140. 2016-2017-2 《Java程序设计》java学习第十五周

    java学习第十五周 Java的GUI界面设计,框架以及主要部件填充,归置,布局管理,在第十一章和第十二章进行了系统的学习,在这两章的知识奠基下,可以简单的构造一个GUI用户界面,在两周的学习后,可以 ...

  8. 201671010140. 2016-2017-2 《Java程序设计》java学习第十四周

      java学习第十四周       本周,主要精力放在了第十二章swing用户界面组件知识的学习,swing是一个用于开发Java应用程序用户界面的开发工具包.它以抽象窗口工具包(AWT)为基础使跨 ...

  9. 第十二周Java学习总结

    学习总结: 本周主要学习了其他容器和事件处理 1.窗体事件(WindowListener)常用接口方法voidwindowActivated/windowDeactivated(WindowEvent ...

随机推荐

  1. web前端好学吗?

    最近这段时间许多学生讨论关于WEB前端工程师这个职位的问题.比如:关于前端难不难?好不好找工作?有没有用?好不好学?待遇好不好?好不好转其他的职位? 针对这个问题,课工场露露老师想跟大家谈谈自己对前端 ...

  2. 【蓝牙数据采集模块】-01-Sensor Controller 功能介绍

    一. CC2650芯片内部的结构框图如图,内部包含: 一个Cortex-M3主控制器,用来做整个芯片的功能与任务实现 一个Cortex-M0射频控制器,用来驱动RF相关电路 一个Sensor Cont ...

  3. 在代码中控制UI界面

    虽然Android推荐使用XML布局文件来控制UI界面,但如果开发者愿意,Android允许开发者完全抛弃XML布局文件,完全在Java代码中控制UI界面. 实例:用编程的方式开发UI界面 packa ...

  4. Java自然语言处理NLP工具包

    1. Java自然语言处理 LingPipe LingPipe是一个自然语言处理的Java开源工具包.LingPipe目前已有很丰富的功能,包括主题分类(Top Classification).命名实 ...

  5. modprobe和insmod的区别

    linux设备驱动有两种加载方式insmod和modprobe,下面谈谈它们用法上的区别1.insmod一次只能加载特定的一个设备驱动,且需要驱动的具体地址.写法为:        insmod dr ...

  6. 【java设计模式】之 抽象工厂(Abstract Factory)模式

    1. 女娲的失误 上一节学习了工厂模式,女娲运用了该模式成功创建了三个人种,可是问题来了,她发现没有性别--这失误也忒大了点吧--竟然没有性别,那岂不是--无奈,只好抹掉重来了,于是所有人都被消灭掉了 ...

  7. 《JAVASCRIPT高级程序设计》节点层次和DOM操作技术

    DOM可以将任何HTML和XML文档描绘成一个由多层次节点构成的结构.节点分为几种不同的类型,每种类型分别表示文档中不同的信息,每种类型都继承与Node接口,因此都共同享有一些属性和方法,同时,也拥有 ...

  8. WebForm 全局对象、commend

    Repeater的增删改 内置对象:页面之间的数据交互为什么要用这些玩意? HTTP的无状态性 Response:响应请求 Request:获取请求 Cookies:保存登录状态----------- ...

  9. 编程练习------C/C++分别实现字符串与整数的转换

    C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...

  10. docker - 容器里安装ssh

    docker安装ssh 通过命令行安装 pull ubuntu镜像 docker pull ubuntu:latest 启动并进入bash docker run -it -d ubuntu:laste ...