Swing入门和布局

来源:互联网 发布:剑灵光头强捏脸数据 编辑:程序博客网 时间:2024/05/17 00:08

一、Swing组件分为三类:顶层容器、中间容器、基本组件

1.顶层容器:

1)JFrame:用于创建一个带有标题栏的窗体

例如:

package com.test.exercise2;import javax.swing.JFrame;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame("JFrame");jf.setSize(200, 200);jf.setLocationRelativeTo(null);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

2)JDialog:用于创建对话框窗体

例如:(在事件处理中的例子)

import javax.swing.JFrame;import javax.swing.JOptionPane;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame("JFrame");jf.setSize(200, 200);jf.setLocationRelativeTo(null);final JButton jb = new JButton("按钮");jf.add(jb);ActionListener listener = new ActionListener() {public void actionPerformed(ActionEvent arg0) {JOptionPane.showMessageDialog(jb, "你按了按钮", "消息对话框", 1);}};jb.addActionListener(listener);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

创建对话框窗口的主要类。可以使用此类创建自定义的对话框,或者调用 JOptionPane 中的多个类方法来创建各种标准对话框

3)JWindow:用于创建一个不带有标题栏和边框的窗体

package com.test.exercise2;import javax.swing.JWindow;public class Test {public Test() {init();}public void init() {JWindow jw = new JWindow();jw.setSize(200, 200);jw.setLocationRelativeTo(null);jw.setVisible(true);}public static void main(String[] args) {new Test();}}

4)JApplet(不建议用):用于创建一个applet小应用程序

2.中间容器:

1)JPanel:中间层非常重要的容器,可以无限叠加放置,支持创建一个面板对象

import java.awt.Color;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);JPanel jp = new JPanel();jp.setBackground(Color.red);// setSize()方法只对顶层容器有用,中间容器即基本组件设置大小如下Dimension mension = new Dimension(100, 100);jp.setPreferredSize(mension);jf.add(jp, BorderLayout.WEST);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

2)JScrollPane:滚动面板,创建一个带有滚动条的面板,最主要的是结合一个基本组件使用(JTextArea、JTextField)

package com.test.exercise2;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);JTextArea jta = new JTextArea();JScrollPane jsp = new JScrollPane(jta);Dimension mension = new Dimension(100, 100);jsp.setPreferredSize(mension);jf.add(jsp);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

3)JTabbedPane:选项卡面板,支持创建带有若干标签的选项卡窗格,它允许用户通过单击具有给定标题和/或图标的选项卡,在一组组件之间进行切换。

3.基本组件:功能性组件,不属于容器,在组件上不能继续添加组件

1)标签:JLabel,用于短文本字符串或图像或二者的显示区。标签不对输入事件作出反应。因此,它无法获得键盘焦点。

2)按钮:JButton(按钮)、JRadioButton(单选按钮,通常与ButtonGroup使用,就是一个组内只有一个可以选)、JCheckBox(复选按钮,可以多选)

3)列表:JList(显示对象列表并且允许用户选择一个或多个项的组件)、JComboBox(下拉列表)

4)输入框:JTextField(文本框,只有一行)、JPasswordField( 主要用来密码输入的)、JTextArea(文本区,多行文本)

5)工具条:JToolBar、JToolTip、JProgressBar

6)选择器:JFileChooser、JColorChooser

7)菜单:JMenuBar(菜单条)、JMenu(菜单)、JMenuItem(菜单项)、JCheckBoxMenuItem(复选菜单项)、JRadioButtonMenuItem(单选菜单项)、JPopupMenu

8)树表:JTree、JTable

9)提示框:JOptionPane

二、布局管理器

布局概念:布局是指组件摆放到容器中的排列方式,布局一般设置到容器对象上,每个容器需要往上添加组将时,都需要先设置好布局

1.流式布局:FlowLayout

package com.test.exercise2;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);jf.setLayout(new FlowLayout(0, 0, 0));for (int i = 0; i < 10; i++) {JButton jb = new JButton();Dimension mension = new Dimension(50, 50);jb.setPreferredSize(mension);jf.add(jb);}jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

默认在第一行的居中位置,横纵向间隙为5px,JPanel默认的是FlowLayout布局

2.边框布局:BorderLayout

JFrame窗体默认布局

import javax.swing.JButton;import javax.swing.JFrame;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);JButton bt1 = new JButton(), bt2 = new JButton(), bt3 = new JButton(), bt4 = new JButton(), bt5 = new JButton();jf.add(bt1, BorderLayout.NORTH);jf.add(bt2, BorderLayout.SOUTH);jf.add(bt3, BorderLayout.WEST);jf.add(bt4, BorderLayout.EAST);jf.add(bt5);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

3.网格布局:GridLayout

package com.test.exercise2;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);jf.setLayout(new GridLayout(3, 3));for (int i = 0; i < 9; i++) {JButton jb = new JButton();jf.add(jb);}jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

4.null布局(空布局)

容器不使用任何一个布局,添加组件则需要通过绝对定位方式添加(坐标定位)

空布局使用步骤:

1)创建容器对象

2)设置容器布局为空

3)组件对象设置位置和大小:调用setBounds()方法

4)组件添加到容器中

package com.test.exercise2;import javax.swing.JButton;import javax.swing.JFrame;public class Test {public Test() {init();}public void init() {JFrame jf = new JFrame();jf.setSize(500, 500);jf.setLocationRelativeTo(null);jf.setLayout(null);JButton jb = new JButton();jb.setBounds(100, 100, 50, 50);jf.add(jb);jf.setDefaultCloseOperation(3);jf.setVisible(true);}public static void main(String[] args) {new Test();}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买家确认收货超时怎么办 淘宝没收到货退款卖家不处理怎么办 微博红包都是字怎么办 500个访客没转化怎么办 店铺动态评分是0怎么办 京东店铺评分低怎么办 被淘宝主播屏蔽怎么办 在淘宝客推广后退款怎么办 生产出现异常时你应该怎么办 违规后的店铺没访客怎么办 淘宝少发货店家不承认怎么办 淘宝买东西店家不发货怎么办 淘宝店家拒绝同意退款怎么办 被淘宝店家骂了怎么办 淘宝买家骂店家骚扰店家怎么办? 不想开淘宝店了怎么办 我是客服经常有客户骂人怎么办 淘宝直播前期没人看怎么办 淘宝被投诉商标侵权怎么办 淘宝后商家页面打不开了怎么办 淘宝遇到职业打假人怎么办 发票被复写上字怎么办 淘宝直播广告图片的商品怎么办 美团商家排名低怎么办 想成为淘宝主播怎么办 用移动流量很卡怎么办 淘宝店铺被恶意刷流量怎么办 一个想要公司权利的人怎么办 淘宝商家短信推广告怎么办 在群里乱发信息怎么办 支付宝扫不了码怎么办 淘宝商家收款不发货怎么办 淘宝申请退款商家拒绝怎么办 一件代发找不到供货商怎么办 淘宝京东兼职上当怎么办 退货时快递丢件怎么办 淘宝店铺代销1688有订单怎么办 供应商已解除合作无法代销怎么办 被代运营骗了怎么办 被淘宝运营骗了怎么办 淘宝运营公司骗了怎么办