Java语言(GUI编程2(Swing(组件、容器)))

Java语言

GUI(Graphical User Interface)

Swing

javax.swing.*;
  1. 概述:Swing是在AWT基础上拓展而来,是提供用户的界面的类库;主要包括的组件分为:顶级容器和轻量级组件;
  2. 顶级容器:JFrame、JApplet、JDialog、JWindow等;
    轻量级组件:继承了AWT的Container类的JComponent类及其子类,包含:JLabel、JTextArea,JFextField、JButton、Jpanal等;
  3. 组件、容器
  • JFrame、JLabel
import javax.swing.*;
import java.awt.*;

public class Demo {
    public static void main(String[] args) {
        new Demo();
        //顶级窗口
        JFrame jf = new JFrame("JFrame窗口");
        jf.setBounds(100,100,300,200);
        jf.setVisible(true);
        //设置标签
        JLabel label = new JLabel("大家好!!");
        jf.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);////设置水平对齐,让文本标签居中
        //获得一个容器
        Container container = jf.getContentPane();
        container.setBackground(Color.GREEN);//用容器设置背景颜色
        //关闭窗口
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第1张图片

窗口背景颜色问题:
在使用JFrame创建窗体时,直接调用setBackground(Color.)这个方法后,你看到的却不是直接的JFrame,而是JFrame.getContentPane();而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame怎么设置背景颜色,你看到的都只是contentPane,我们就需要通过容器来设置背景颜色。

  • JDialog、JButton
    利用监听事件完成,点击一个按钮弹出一个窗口:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Demo  {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(400,300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = jFrame.getContentPane();       
        container.setLayout(null);//绝对布局        
        JButton button = new JButton("点击弹出一个弹窗"); //创建按钮
        button.setBounds(100,30,200,50);
        //监听器
        button.addActionListener(new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });
        container.add(button);
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {        
        this.setBounds(150,150,300,200);
		this.setVisible(true);
        Container container = this.getContentPane();
        container.setLayout(null);
    }
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第2张图片

  • Icon(图标): Swing包下的接口
//将图标放在标签(JLabel)上;实现这个接口需要重写它的三个方法;
import javax.swing.*;
import java.awt.*;
public class Demo implements Icon {
    private int width;
    private int height;

    public Demo(int width,int height){
        this.width = width;
        this.height = height;
    }
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        Demo demo = new Demo(15,15);
        JLabel jLabel = new JLabel("icon",demo,SwingConstants.CENTER);
        Container container = jFrame.getContentPane();
        container.add(jLabel);
        /*将图标放在按钮上
        JButton button = new JButton("button", demo);
        Container container = jFrame.getContentPane();
        container.add(button);*/
        jFrame.setVisible(true);
    }
    @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;
    }
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第3张图片

  • ImageIcon(图像图标):
    将 picture.jpg 这张图片设置为图标
//利用URL获取图片地址
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("picture.jpg");
        ImageIcon imageIcon = new ImageIcon(url); 
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER)
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(10,10,300,200);
    }
    public static void main(String[] args) {
        new ImageIconDemo();
    }
}
  • JPanel
//用JPanel布局一些按钮
import javax.swing.*;
import java.awt.*;

public class Demo extends JFrame {
       public static void main(String[] args) {
             new Demo();
       }
    public Demo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,2,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(300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }   
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第4张图片

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

public class Demo extends JFrame {
    public Demo(){
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        //textArea.setText("");可以设定文本域的内容
        //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 Demo();
    }
}

结果:可以自由输入文字
Java语言(GUI编程2(Swing(组件、容器)))_第5张图片

  • JRadioButton:单选按钮
import javax.swing.*;
import java.awt.*;

public class Demo extends JFrame {
    public Demo() {
        Container container = this.getContentPane();
        //单选框
        JRadioButton radioButton1 = new JRadioButton("Button01");
        JRadioButton radioButton2 = new JRadioButton("Button02");
        JRadioButton radioButton3 = new JRadioButton("Button03");
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        container.add(radioButton1,BorderLayout.NORTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(400,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Demo();
    }
}

结果:每次只能选择一个按钮
Java语言(GUI编程2(Swing(组件、容器)))_第6张图片

  • JCheckBox:复选框
import javax.swing.*;
import java.awt.*;

/**
 * @Description:
 * @Author: Mr.Chen
 * @CreateTime: 2019-11-30  21:59
 */
public class Demo extends JFrame {

    public Demo() {
        Container container = this.getContentPane();
        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        JCheckBox checkBox03 = new JCheckBox("checkBox03");
        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.CENTER);
        container.add(checkBox03,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(400,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Demo();
    }
}

结果:随意几个都可以选择
Java语言(GUI编程2(Swing(组件、容器)))_第7张图片

  • JComboBox:组合框(下拉框)
    用于:选择性别,地区…或者一些单个选项
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
    public Demo() {
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);//空选项
        status.addItem("111111");
        status.addItem("222222");
        status.addItem("333333");
        container.add(status);
        this.setVisible(true);
        this.setSize(300,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Demo();
    }
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第8张图片

  • JList:列表
    用于展示信息
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo extends JFrame {
    public Demo() {
        Container container = this.getContentPane();
        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);
        contents.add("111111");
        contents.add("222222");
        contents.add("333333");
        container.add(jList);
        this.setVisible(true);
        this.setSize(300,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Demo();
    }
}

结果:
Java语言(GUI编程2(Swing(组件、容器)))_第9张图片
//Swing包就介绍这么多了,它的包下还有很多的应用,就不一一介绍了
//下篇再见…谢谢

你可能感兴趣的:(JavaSE)