GUI编程(二)

Swing

Swing是GUI(图形用户界面)开发工具包。

早期的AWT(抽象窗口工具包)组件开发的图形用户界面,要依赖本地系统,当把AWT组件开发的应用程序移植到其他平台的系统上运行时,不能保证其外观风格,因此AWT是依赖于本地系统平台的。

而使用Swing开发的Java应用程序,其界面是不受本地系统平台限制的,也就是说Swing开发的Java应用程序移植到其他系统平台上时,其界面外观是不会改变的。但要注意的是,虽然Swing提供的组件可以方便开发Java应用程序,但是Swing并不能取代AWT,在开发Swing程序时通常要借助与AWT的一些对象来共同完成应用程序的设计。

一、常用窗体

Swing窗体是Swing的一个组件,同时也是创建图形化用户界面的容器,可以将其它组件放置在窗体容器中。

1. JFrame框架窗体

JFrame窗体是一个容器,在Swing开发中我们经常要用到,它是Swing程序中各个组件的载体。

这里我们要把组件添加到容器container 中,而不是像Frame一样直接添加到frame中

package com.demo4;

import javax.swing.*;
import java.awt.*;

public class TestJFrame {
    public void init(){
        //顶级窗口
        JFrame frame = new JFrame("JFrame");
        frame.setVisible(true);
        frame.setBounds(100,100,200,200);

        //设置文字
        JLabel label = new JLabel("Hello");
        //文本标签居中
        label.setHorizontalAlignment(JLabel.CENTER);

        // 获取一个容器
        Container container = frame.getContentPane();
        // 将标签添加至容器
        container.add(label);
        // 设置容器背景颜色
        container.setBackground(Color.pink);

        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new TestJFrame().init();
    }
}

常用的窗体关闭方式有四种:
“DO_NOTHING_ON_CLOSE” :什么也不做就将窗体关闭;
“DISPOSE_ON_CLOSE” :隐藏当前窗口,并释放窗体占有的其他资源。
“HIDE_ON_CLOSE” : 隐藏窗口的默认窗口关闭;
“EXIT_ON_CLOSE”:退出应用程序默认窗口关闭。

DISPOSE_ON_CLOSE在窗口被关闭的时候会dispose这个窗口。
如果你的程序没有其他线程在运行的话,当所有的窗口都被dispose了之后,JVM也会退出。
1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);一个窗口点右上角×,只有该窗口会关闭
2.而setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);一个窗口点右上角×,软件的所有窗口都会关闭

2 .JDialog窗体

JDialog窗体是Swing组件中的对话框,继承了AWT组件中的java.awt.Dialog类。功能是从一个窗体中弹
出另一个窗体。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class TestJDialog extends JFrame {
   public TestJDialog(){
       setVisible(true);
       setSize(700,500);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       Container container = this.getContentPane();
       //绝对布局
       getContentPane().setLayout(null);

       //按钮
       JButton button = new JButton("点击弹出一个对话框");
       button.setBounds(30,30,200,50);
       //监听按钮事件,点击按钮弹出弹窗
       button.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               //弹窗
               new MyDialog();
           }
       });

       container.add(button);

   }
    public static void main(String[] args) {
        new TestJDialog();
    }
}
//弹窗的窗口
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        
        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new Label("Java"));
    }
}


二、标签组件

在Swing中显示文本或提示信息的方法是使用标签,它支持文本字符串和图标。上面我们提到的JLabel就是这里的内容。

1. 标签

标签由JLabel类定义,可以显示一行只读文本、一个图像或带图像的文本。

JLabel lable = new JLabel();

2. 图标

Swing中的图标可以放置在按钮、标签等组件上,用于描述组件的用途。

图标可以用Java支持的图片文件类型进行创建,也可以使用java.awt.Graphics类提供的功能方法来创建。

在Swing中通过Icon接口来创建图标,可以在创建时给定图标的大小、颜色等特性。

import javax.swing.*;
import java.awt.*;

//图标,接口,需要实现类
public class TestIcon extends JFrame implements Icon {

    private int width;
    private int height;

    public TestIcon(){}

    public TestIcon(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init(){
        TestIcon testIcon = new TestIcon(15, 15);
        //图标放在标签,按钮上
        JLabel label = new JLabel("icon", testIcon, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);

    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }

    public static void main(String[] args) {
        new TestIcon().init();
    }
}

Swing中的图标除了可以绘制之外,还可以使用某个特定的图片创建。
利用javax.swing.ImageIcon类根据现有图片创建图标。

下面看一个实例,我们先在包下放一个图片(注意放置位置,不同位置路径不同),如下:
在这里插入图片描述

package com.demo4;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestImageIcon extends JFrame {

    public TestImageIcon(){

        //获取图片的地址  从当前类的路径下获取
        URL url = this.getClass().getResource("Java.jpg");
        ImageIcon imageIcon = new ImageIcon(url);

        //JLabel label = new JLabel("ImageIcon");
        //label.setIcon(imageIcon);
        //label.setHorizontalAlignment(SwingConstants.CENTER);
		 JLabel label = new JLabel("ImageIcon", imageIcon, SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        setBounds(100,100,200,200);

    }

    public static void main(String[] args) {
        new TestImageIcon();
    }

}

对于图片标签,我们经常将图片放置在标签上,用JLabel中的setIcon()方法即可,当然也可以在初始化JLabel对象时为标签指定图标,这需要获取一个Icon实例。
getResource()方法可以获得资源文件的URL路径,这里的路径是相对于前面的那个类的,所以可将该图片与该类放在同一个文件夹下;如果不在同一个文件夹下,需通过其它方法获取路径。

三、面板

面板也是一个容器,可作为容器容纳其他组件,但也必须被添加到其他容器中。

Swing中常用面板有JPanel面板和JScrollPane面板。

1. JPanel

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10)); // 整个容器为2行1列

        JPanel p1 = new JPanel(new GridLayout(1, 3)); // 初始化一个面板,设置1行3列的网格布局
        JPanel p2 = new JPanel(new GridLayout(1, 2)); // 初始化一个面板,设置1行2列的网格布局
        JPanel p3 = new JPanel(new GridLayout(2, 1)); // 初始化一个面板,设置2行1列的网格布局
        JPanel p4 = new JPanel(new GridLayout(3, 2)); // 初始化一个面板,设置3行2列的网格布局

        p1.add(new JButton("1"));
        p1.add(new JButton("1"));
        p1.add(new JButton("1"));

        p2.add(new JButton("2"));
        p2.add(new JButton("2"));
        
        p3.add(new JButton("3"));
        p3.add(new JButton("3"));

        p4.add(new JButton("4"));
        p4.add(new JButton("4"));
        p4.add(new JButton("4"));
        p4.add(new JButton("4"));
        p4.add(new JButton("4"));
        p4.add(new JButton("4"));

        container.add(p1);
        container.add(p2);
        container.add(p3);
        container.add(p4);

        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }


    public static void main(String[] args) {
        new JPanelDemo();
    }
}

2. JScrollPane

若遇到一个较小的容器窗体中显示一个较大部分内容的情况,可用JScrollPane面板。这是一个带滚动条的面板,就像平时浏览网页,经常遇到的滚动条一样。

如果需要在JScrollPane面板中放置多个组件,需将这多个组件放置在JPanel面板上,然后将JPanel面板作为一个整体组件添加在JScrollPane面板上。

import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo extends JFrame {
    public JScrollPaneDemo(){
        Container container = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("Java");

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);

        container.add(jScrollPane);


        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JScrollPaneDemo();
    }
}

四、按钮组件

1. 图片按钮

JButton在之前的例子中已经出现多次,是较为常用的组件,用于触发特定动作。可以在按钮上显示文本标签,还可以显示图标。

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo1 extends JFrame {
    public JButtonDemo(){
        Container container = getContentPane();
        URL resource = this.getClass().getResource("Java.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

        //将图标放在按钮上
        JButton button = new JButton();
        button.setIcon(imageIcon);
        button.setToolTipText("图片按钮");

        container.add(button);

        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo();
    }
}

2. 单选按钮

默认情况下,单选按钮显示一个圆形图标,通常在其旁放置一些说明性文字。当用户选中某个单选按钮后,按钮组中其它按钮将被自动取消,这时就需要按钮组(ButtonGroup)来将同组按钮放在一起,该按钮组中的按钮只能选择一个,而不在此按钮中的按钮不受影响。

import javax.swing.*;
import java.awt.*;

public class JButtonDemo2 extends JFrame{

    public JButtonDemo2(){
        Container container = getContentPane();
        //单选框
        JRadioButton radioButton1 = new JRadioButton("1");
        JRadioButton radioButton2 = new JRadioButton("2");
        JRadioButton radioButton3 = new JRadioButton("3");

        //由于单选框只能选一个,进行分组
        //如果没有进行分组,可以同时选择多个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        container.add(radioButton1,BorderLayout.NORTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.SOUTH);


        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo2();
    }
}

3. 复选框

复选框是一个方块图标,外加一段描述性文字,与单选按钮的区别就是可以多选。每一个复选框都提供“选中”与“不选中”两种状态。

import javax.swing.*;
import java.awt.*;

public class JButtonDemo3 extends JFrame {

    public JButtonDemo3(){
        Container container = getContentPane();
        JCheckBox checkBox1 = new JCheckBox("Java");
        JCheckBox CheckBox2 = new JCheckBox("C++");
        container.add(checkBox1,BorderLayout.NORTH);
        container.add(CheckBox2,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        new JButtonDemo3();
    }
}

五、列表组件

1. 下拉列表(JComboBox)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestComboBox extends JFrame {
    public TestComboBox(){
        Container container = getContentPane();

        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("正在上映");
        comboBox.addItem("即将上映");
        comboBox.addItem("下架");

        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(comboBox.getSelectedIndex()); //返回下标
                System.out.println(comboBox.getSelectedItem());  //返回内容
            }
        });

        container.add(comboBox);


        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

    public static void main(String[] args) {
        new TestComboBox();

    }
}

2. 列表框(JList)

package com;

import javax.swing.*;
import java.awt.*;


public class TestJList extends JFrame{
    public TestJList(){
        Container container = getContentPane();

        //生成列表的内容
        String[] str={"1","2","3"};
        JList jList = new JList(str);

        container.add(jList);

        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

    public static void main(String[] args) {
        new TestJList();
    }

}

六、文本组件

1. 文本框(JTextField)

文本框用来显示或编辑一个单行文本

import javax.swing.*;
import java.awt.*;

public class TestJTestField extends JFrame {
    public TestJTestField(){
        Container container = getContentPane();
        //这里由于布局问题,文本框的长度改变
        container.setLayout(new GridLayout(2,1));

        JTextField jTextField1 = new JTextField("Java");// 创建一个文本框,值为Java
        JTextField jTextField2 = new JTextField("C++",20);// 创建一个长度为20的文本框,值为C++

        container.add(jTextField1);
        container.add(jTextField2);

        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new TestJTestField();
    }
}

2. 密码框(JPasswordField)

密码框与文本框的定义与用法类似,但会使用户输入的字符串以某种符号进行加密。

import javax.swing.*;
import java.awt.*;

public class TestJPasswordField extends JFrame {
    public TestJPasswordField(){
        Container container = getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('$');  //char 类型 设置回显符号

        container.add(jPasswordField);

        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



    }

    public static void main(String[] args) {
        new TestJPasswordField();
    }
}

3. 文本域(JTextArea)

文本域组件在上面JScrollPane面板的代码中已经出现

配合面板使用

package com.demo5;

import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo extends JFrame {
    public JScrollPaneDemo(){
        Container container = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("Java");

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);

        container.add(jScrollPane);


        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JScrollPaneDemo();
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值