Swing概述
GUI——图形用户界面。该技术是目前最为常见的应用程序模型,如手机APP、Web网站等。
Java GUI最早使用的工具包是AWT(抽象窗口工具包),这个工具包提供了一套与本地图形界面交互的接口。AWT中的图形函数与操作系统所提供的图形函数之间有一一对应的关系。也就是说,当我们用AWT来构建图形用户界面时,实际上是在利用操作系统所提供的图形库。
但是不同的操作系统,其所提供的图形库不一样,而Java为了实现可移植性,不得不把AWT所提供的图形函数定义为各种通用型操作系所提供的图形功能交集。所以AWT被称为重量级组件。
由于AWT所提供的是通用可视组件的子集,而且不能扩展,所以Java引入了Java swing技术来解决这个问题。
swing技术是在AWT的基础上构建的一套新的图形界面系统。他提供的AWT所能提供的所有技术,并且用纯Java代码对AWT的功能进行扩充,所以swing组件被称为轻量级组件 。
简单的GUI程序
下面来创建一个简单的GUI窗口
package cn.com;
import java.awt.Color;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class EmptyFrame extends JFrame
{
public EmptyFrame()
{
this.init();
}
private void init()
{
this.setSize(400, 500); //设置窗口大小
this.setTitle("基本窗口"); //设置窗口标题
this.setBackground(Color.RED); //设置窗口背景色
this.setLocationRelativeTo(null);//设置窗口位置在屏幕中间
this.setVisible(true); //设置窗口可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
测试类
package cn.com;
import javax.swing.JFrame;
public class Test
{
public static void main(String args[])
{
JFrame jframe = new EmptyFrame();
}
}
下面简单分析下上面的EmptyFrame 类
@SuppressWarnings(“serial”) | 该语句作用是取消序列化警告,读者可以不必理会 |
---|---|
setLocationRelativeTo | 该方法设置在父容器中居中显示,当其参数为null时表示窗口在屏幕居中显示 |
setDefaultCloseOperation | 该方法用于设置窗口关闭时的默认行为,下面会重点介绍 |
– | – |
setVisible | 一般窗口默认不可见,该方法设置窗口可见 |
setDefaultCloseOperation()方法参数有四个:
细心的读者可能会发现 this.setBackground(Color.RED)语句不起作用,后面我会单独讲解,因为他涉及到面板的内容。
模式窗口与非模式窗口
常见的GUI窗口分两类——模式窗口和非模式窗口。
模式窗口:模式窗口打开后成为当前活动焦点,截取用户输入,而且用户只能在和该窗口完成交互后才可以回到父窗口。常见的就是打开一个文件时弹出的对话框、系统登录界面等等。
非模式窗口:非模式窗口打开后成为当前活动焦点,但是允许在父窗口和该窗口之间进行切换,不会阻塞程序执行。
JFrame是非模式窗口,不会阻塞程序运行。Java中的模式窗口只能通过继承JDialog类来实现。
顶层容器
顶层容器是图形化界面显示的基础,其他所有组件都直接或间接显示在顶层容器中。
Java中顶层容器有四种
顶层容器面板框架
JRootPane(根面板):根面板位于最底层,是其他所有面板的载体,它覆盖整个客户区(除去边框和标题栏)。默认可见、不透明,但是由于在最底层,所以他可不可见还是取决于他上面的面板。
JLayerePane(层面板):该面板包含菜单栏和内容面板,它覆盖在根面板之上。默认透明可见。层面板有很多层,一般用数字表示(数字越大,越靠近人眼),而内容面板就位于其中的一层(默认设置是-3000)。层面板没有布局管理器,组件设置大小后可以直接添加到层面板中。
ContentPane(内容面板):内容面板默认可见、不透明。我们在向窗口添加组件时,也是先添加到该面板上,而不是直接添加到容器上。
GlassPane(玻璃面板):该面板总存在,位于最上层。默认透明不可见。
*上面的 this.setBackground(Color.RED)语句,是直接设置的根面板,但是由于内容面板不透明,所以他遮挡了根面板,所以我们才会看不到根面板的颜色,下面就来具体演示,看看各级面板怎样调用。*
package cn.com;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test
{
public static void main(String args[])
{
JFrame win1 = new JFrame();
win1.setSize(400 , 500);
//测试根面板
win1.getContentPane().setVisible(false); //设置内容面板不可见,目的是显示根面板
win1.getRootPane().setBackground(Color.BLACK);
init("根面板测试窗口" , win1);
JFrame win2 = new JFrame();
//测试层面板
win2.getContentPane().setVisible(false); //设置内容面板不可见
win2.getLayeredPane().setOpaque(true); //设置层面板不透明
win2.getLayeredPane().setBackground(Color.GREEN);
win2.getRootPane().setBackground(Color.BLACK);
init("层面板测试窗口" , win2);
JFrame win3 = new JFrame();
//测试玻璃面板
win3.getGlassPane().setVisible(true); //设置玻璃面板可见
((JPanel) win3.getGlassPane()).setOpaque(true); //设置玻璃面板不透明
win3.getGlassPane().setBackground(Color.RED);
win3.getRootPane().setBackground(Color.BLACK);
init("玻璃面板测试窗口" , win3);
}
static public void init(String str , JFrame win)
{
win.setSize(400 , 500);
win.setVisible(true);
win.setTitle(str);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
下面是测试效果
中间容器
中间容器不能单独存在,必须依附于某一个顶层容器。严格来说,除顶层容器外,Java Swing其他组件都是中间容器。
下面来看看常用的中间容器:
下面看看具体的例子:
package cn.com;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ScrollDemo extends JFrame
{
public ScrollDemo()
{
init();
}
private void init()
{
JScrollPane pan = new JScrollPane();
//设置水平和垂直滚动条总是出现
pan.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
pan.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JTextArea textshow = new JTextArea(); //文本区
pan.setViewportView(textshow); //将文本区添加进pan容器
this.add(pan); //将中间容器加入顶层容器
//this.setContentPane(pan); 该语句也可以
this.setTitle("滚动面板展示");
this.setSize(400 , 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
测试
package cn.com;
public class Test
{
public static void main(String args[])
{
ScrollDemo win = new ScrollDemo();
win.setVisible(true);
}
}
下面来看看拆分面板
package cn.com;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class SplitDemo extends JFrame
{
public SplitDemo()
{
init();
}
private void init()
{
JScrollPane pan = new JScrollPane(); //带滚动条的面板
JTextArea textshow1 = new JTextArea();
JTextArea textshow2 = new JTextArea();
pan.setViewportView(textshow1); //设置滚动条
JSplitPane pan2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT , pan , textshow2);//拆分面板
pan2.setDividerLocation(100); //设置分割条的位置
pan2.setOneTouchExpandable(true); //设置可折叠箭头
this.add(pan2);
this.setVisible(true);
this.setTitle("拆分面板");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package cn.com;
public class Test
{
public static void main(String args[])
{
SplitDemo win = new SplitDemo();
win.setSize(400 , 500);
}
}