一、概述
Swing 是一个为Java设计的GUI工具包。
Swing是JAVA基础类的一部分。
Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。
Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。
二、常用窗体
1.JFrame
JFrame 用来设计类似于 Windows 系统中窗口形式的界面。JFrame 是 Swing 组件的顶层容器,该类继承了 AWT 的 Frame 类,支持 Swing 体系结构的高级 GUI 属性。
JFrame 类的常用构造方法如下所示。
JFrame():构造一个初始时不可见的新窗体。
JFrame(String title):创建一个具有 title 指定标题的不可见新窗体
public class TestJFrame extends JFrame {
public void init(){
this.setVisible(true);
this.setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello,JFrame");
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
container.setBackground(Color.CYAN);
}
public static void main(String[] args) {
new TestJFrame().init();
}
}
结果图
2.JDialog窗体
该窗体是Swing组建中的对话框。作用为从一个窗体重弹出另一个窗体,也可以使用getContentPane()来更换为容器
public class TestJDialog extends JDialog {
public TestJDialog(){
setBounds(10,10,300,300);
//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
JLabel hello = new JLabel("Hello");
hello.setHorizontalAlignment(SwingConstants.CENTER);
container.add(hello);
container.setBackground(Color.cyan);
}
public static void main(String[] args) {
new MyJframe();
}
}
class MyJframe extends JFrame{
public MyJframe(){
setBounds(20,20,500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
Container container = getContentPane();
container.setBackground(Color.WHITE);
JButton jButton = new JButton("点击弹出对话框");
jButton.setBounds(150,50,200,50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TestJDialog().setVisible(true);
}
});
container.add(jButton);
}
}
三、组件
在Swing中显示文本或提示信息的方法是使用标签,它支持文本字符串和图标。上面我们提到的JLabel就是这里的内容。
1. 标签
标签由JLabel类定义,可以显示一行只读文本、一个图像或带图像的文本。
JLabel类提供了许多构造方法,可查看API选择需要的使用,如显示只有文本的标签、只有图标的标签或包含文本与图标的标签等。因为上面已经出现过了,这里就不再举例了。常用语法格式如下,创建的是一个不带图标和文本的JLabel对象:
JLabel jLabel = new JLabel();
//创建标签对象
2. 图标
Swing中的图标可以放置在按钮、标签等组件上,用于描述组件的用途。图标可以用Java支持的图片文件类型进行创建,也可以使用java.awt.Graphics类提供的功能方法来创建。
在Swing中通过Icon接口来创建图标,可以在创建时给定图标的大小、颜色等特性。注意,Icon是接口,在使用Icon接口的时候,必须实现Icon接口的三个方法:
public int getIconHeight()
public int getIconWidth()
public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3)
前两个方法用于获取图片的长宽,paintIcon()方法用于实现在指定坐标位置画图。
下面看一个用Icon接口创建图标的实例:
图片图标
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
JLabel jl = new JLabel("这是一个JFrame窗体,旁边是一个图片");
URL url = ImageIconDemo.class.getResource("pencil.jpg");
Icon icon = new ImageIcon(url);
// 实例化Icon对象
jl.setIcon(icon);
// 为标签设置图片
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setOpaque(true);
// 设置标签为不透明状态
Container container = getContentPane();
container.add(jl);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 350);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.面板
JPanel 是一种中间层容器,它能容纳组件并将组件组合在一起,但它本身必须添加到其他容器中使用。JPanel 类的构造方法如下。
JPanel():使用默认的布局管理器创建新面板,默认的布局管理器为 FlowLayout。
JPanel(LayoutManagerLayout layout):创建指定布局管理器的 JPanel 对象。
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10)); //后面的参数的意思,间距
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
效果图
4.按钮
按钮是图形界面上常见的元素,在前面已经多次使用过它。在 Swing 中按钮是 JButton 类的对象,JButton 类的常用构造方法如下。
- JButton():创建一个无标签文本、无图标的按钮。
- JButton(Icon icon):创建一个无标签文本、有图标的按钮。
- JButton(String text):创建一个有标签文本、无图标的按钮。
- JButton(String text,Icon icon):创建一个有标签文本、有图标的按钮。
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container container = this.getContentPane();
URL resource = JButtonDemo.class.getResource("regbtn.jpg");
Icon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
5、列表
下拉列表由 JComboBox 类实现,常用构造方法如下。
-
JComboBox():创建一个空的 JComboBox 对象。
-
JComboBox(ComboBoxModel aModel):创建一个 JComboBox,其选项取自现有的 ComboBoxModel。
-
JComboBox(Object[] items):创建包含指定数组中元素的 JComboBox。
public class TestComboboxDemo01 extends JFrame { public TestComboboxDemo01() { Container container = this.getContentPane(); JComboBox status = new JComboBox(); status.addItem(null); status.addItem("正在热映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo01(); } }
-
列表框
public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02() { Container container = this.getContentPane(); //生成列表的内容 //String[] contents = {"1","2","3"}; Vector contents = new Vector(); //列表中需要放入内容 JList jList = new JList(contents); contents.add("zhangsan"); contents.add("lisi"); contents.add("wangwu"); container.add(jList); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo02(); } }
-
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
6、文本框
Swing 中使用 JTextField 类实现一个单行文本框,它允许用户输入单行的文本信息。该类的常用构造方法如下。
-
JTextField():创建一个默认的文本框。
-
JTextField(String text):创建一个指定初始化文本信息的文本框。
-
JTextField(int columns):创建一个指定列数的文本框。
-
JTextField(String text,int columns):创建一个既指定初始化文本信息,又指定列数的文本框
public class TestTextDemo01 extends JFrame { public TestTextDemo01() { Container container = this.getContentPane(); JTextField textField = new JTextField("hello"); JTextField textField2 = new JTextField("world",20); container.add(textField,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo01(); } }
-
密码框
public class TestTextDemo03 extends JFrame { public TestTextDemo03() { Container container = this.getContentPane(); //面板 JPasswordField passwordField = new JPasswordField(); //**** passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo03(); } }
-
文本域
public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = this.getContentPane(); //文本域 JTextArea textArea = new JTextArea(20, 50); textArea.setText("欢迎学习狂神说Java"); //Scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }