Java学习?用户界面的布局
作者:网络转载 发布时间:[ 2017/3/2 11:15:49 ] 推荐标签:测试开发技术 Java
BoxLayout管理器
BoxLayout类位于javax.swing包中,它可以将组件排列成一行或一列。
使用该布局时,先创建一个放置组件的面板,然后再创建一个布局管理器,它带有2个参数:
1.以框式布局组织的组件;
2.BoxLayout.Y_AXIS指定垂直排列,BoxLayout.X_AXIS指定水平排列;
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class Crisis extends JFrame {
5 JButton panicButton;
6 JButton dontPanicButton;
7 JButton blameButton;
8 JButton mediaButton;
9 JButton saveButton;
10
11 public Crisis() {
12 super("Crisis");
13 setLookAndFeel();
14 setSize(348, 128);
15 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16 JPanel pane = new JPanel();
17 BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
18 pane.setLayout(box);
19 panicButton = new JButton("Panic");
20 dontPanicButton = new JButton("Don't Panic");
21 blameButton = new JButton("Blame Others");
22 mediaButton = new JButton("Notify the Media");
23 saveButton = new JButton("save yourself");
24 pane.add(panicButton);
25 pane.add(dontPanicButton);
26 pane.add(blameButton);
27 pane.add(mediaButton);
28 pane.add(saveButton);
29 add(pane);
30
31 setVisible(true);
32 }
33
34 private void setLookAndFeel() {
35 try {
36 UIManager.setLookAndFeel(
37 "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
38 } catch (Exception exc) {
39 // ignore error
40 }
41 }
42
43 public static void main(String[] arguments) {
44 Crisis frame = new Crisis();
45 }
46 }
使用Insets将组件隔开
Insets类位于java.awt包中,它有一个接受4个参数的构造函数:在容器上、下、左、右留出的空间。每个参数都以像素为单位,像素是定义框架大小时使用的度量单位。
1 public Insets getInsets() {
2 Insets squeeze = new Insets(60, 15, 10, 15);
3 return squeeze;
4 }
布局测试
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class LottoMadness extends JFrame {
5 // set up row1
6 JPanel row1 = new JPanel();
7 ButtonGroup option = new ButtonGroup();
8 JCheckBox quickpick = new JCheckBox("Quick Pick", false);
9 JCheckBox personal = new JCheckBox("Personal", true);
10 // set up row2
11 JPanel row2 = new JPanel();
12 JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT);
13 JTextField[] numbers = new JTextField[6];
14 JLabel winnersLabel = new JLabel("Winners: ", JLabel.RIGHT);
15 JTextField[] winners = new JTextField[6];
16 // set up row3
17 JPanel row3 = new JPanel();
18 JButton stop = new JButton("Stop");
19 JButton play = new JButton("play");
20 JButton reset = new JButton("Reset");
21 // set up row4
22 JPanel row4 = new JPanel();
23 JLabel got3Label = new JLabel("3 of 6: ", JLabel.RIGHT);
24 JTextField got3 = new JTextField("0");
25 JLabel got4Label = new JLabel("4 of 6: ", JLabel.RIGHT);
26 JTextField got4 = new JTextField("0");
27 JLabel got5Label = new JLabel("5 of 6: ", JLabel.RIGHT);
28 JTextField got5 = new JTextField("0");
29 JLabel got6Label = new JLabel("6 of 6: ", JLabel.RIGHT);
30 JTextField got6 = new JTextField("0");
31 JLabel drawingsLabel = new JLabel("Drawings", JLabel.RIGHT);
32 JTextField drawings = new JTextField("0");
33 JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
34 JTextField years = new JTextField();
35
36 public LottoMadness() {
37 super("Lotto Madness");
38
39 setSize(550, 400);
40 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
41 GridLayout layout = new GridLayout(5, 1, 10, 10);
42 setLayout(layout);
43
44 FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
45 option.add(quickpick);
46 option.add(personal);
47 row1.setLayout(layout1);
48 row1.add(quickpick);
49 row1.add(personal);
50 add(row1);
51
52 GridLayout layout2 = new GridLayout(2, 7, 10, 10);
53 row2.setLayout(layout2);
54 row2.add(numbersLabel);
55 for (int i = 0; i < 6; i++) {
56 numbers[i] = new JTextField();
57 row2.add(numbers[i]);
58 }
59 row2.add(winnersLabel);
60 for (int i = 0; i < 6; i++) {
61 winners[i] = new JTextField();
62 winners[i].setEditable(false);
63 row2.add(winners[i]);
64 }
65 add(row2);
66
67 FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
68 row3.setLayout(layout3);
69 stop.setEnabled(false);
70 row3.add(stop);
71 row3.add(play);
72 row3.add(reset);
73 add(row3);
74
75 GridLayout layout4 = new GridLayout(2, 3, 20, 10);
76 row4.setLayout(layout4);
77 row4.add(got3Label);
78 got3.setEditable(false);
79 row4.add(got3);
80 row4.add(got4Label);
81 got4.setEditable(false);
82 row4.add(got4);
83 row4.add(got5Label);
84 got5.setEditable(false);
85 row4.add(got5);
86 row4.add(got6Label);
87 got6.setEditable(false);
88 row4.add(got6);
89 row4.add(drawingsLabel);
90 drawings.setEditable(false);
91 row4.add(drawings);
92 row4.add(yearsLabel);
93 years.setEditable(false);
94 row4.add(years);
95 add(row4);
96
97 setVisible(true);
98 }
99
100 private static void setLookAndFeel() {
101 try {
102 UIManager.setLookAndFeel(
103 "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
104 } catch (Exception exc) {
105 // ignore error
106 }
107 }
108
109 public static void main(String[] arguments) {
110 LottoMadness.setLookAndFeel();
111 LottoMadness frame = new LottoMadness();
112 }
113 }
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Java性能测试有哪些不为众人所知的原则?Java设计模式??装饰者模式谈谈Java中遍历Map的几种方法Java Web入门必知你需要理解的Java反射机制知识总结编写更好的Java单元测试的7个技巧编程常用的几种时间戳转换(java .net 数据库)适合Java开发者学习的Python入门教程Java webdriver如何获取浏览器新窗口中的元素?Java重写与重载(区别与用途)Java变量的分类与初始化JavaScript有这几种测试分类Java有哪四个核心技术?给 Java开发者的10个大数据工具和框架Java中几个常用设计模式汇总java生态圈常用技术框架、开源中间件,系统架构及经典案例等
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南