栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

GUI编程(二)Swing专题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUI编程(二)Swing专题

目录

一、简介

二、基础控件

2.1 JFrame

2.2 JDialog弹窗

2.3 Icon、ImageIcon标签

2.4 JScroll面板

2.5 JPanel面板

2.6 图片按钮、单选框、多选框

2.7 下拉框、列表框

2.8 文本框、密码框、文本域

一、简介

        Swing提供许多比 AWT 更好的屏幕显示元素。它们用纯 Java 写成,所以同 Java 本身一样可以跨平台运行,这 一点不像 AWT。 它们是 JFC 的一部分。 它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着可以在任意平台 上使用 JAVA 支持的任意面板。

        轻量级元件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

二、基础控件

2.1 JFrame

Demo01:

public class JFrameDemo {
    //init():初始化
    public void init(){
        JFrame jf = new JFrame("这是一个jframe窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.pink);

        //设置文字
        JLabel label = new JLabel("biubiubbiu");
        jf.add(label);

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

运行结果:

Demo02:

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

class MyJFrame02 extends JFrame{
    public void init(){
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.pink);

        //设置文字
        JLabel label = new JLabel("biubiubbiu");
        this.add(label);
        //文字居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

运行结果:

2.2 JDialog弹窗
//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo(){
        //可见性
        this.setVisible(true);
        //大小
        this.setSize(700,500);
        //关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame存放东西需要一个容器
        Container container = this.getContentPane();
        //绝对布局
        this.setLayout(null);
        //按钮
        JButton button = new JButton("click on it");
        button.setBounds(30,30,200,50);
        container.add(button);
        //点击按钮 弹出弹窗---->监听事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });

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

//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //默认可以直接关闭 多写会报错
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);

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

运行结果:

2.3 Icon、ImageIcon标签

Icon:普通图标

//图标是一个接口 需要实现类
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    public IconDemo(){
    }

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

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

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

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

    @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;
    }
}

运行结果:

ImageIcon:将自定义图片设为图标

直接将需要使用的图片放至同级代码目录结构中即可

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片地址    获得类下的同级资源
        URL url = ImageIconDemo.class.getResource("pic.PNG");
        JLabel label = new JLabel("ImageIcon");

        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);

        //居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //容器
        Container container = this.getContentPane();
        container.add(label);

        //可见性
        setVisible(true);

        //大小
        setBounds(10,10,500,500);

        //关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

2.4 JScroll面板
public class JscrollDemo extends JFrame {
    public JscrollDemo(){
        //容器
        Container container = this.getContentPane();

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

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

        container.add(scrollPane);

        setBounds(10,10,300,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

运行结果:

2.5 JPanel面板
public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        //容器
        Container container = this.getContentPane();
        //设置布局
        //hgap:水平间距    vgap:垂直间距
        container.setLayout(new GridLayout(2,1,10,10));

        //面板
        JPanel panel = 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));

        panel.add(new JButton("1-1"));
        panel.add(new JButton("1-2"));
        panel.add(new JButton("1-3"));

        panel2.add(new JButton("2-1"));
        panel2.add(new JButton("2-2"));

        panel3.add(new JButton("3-1"));
        panel3.add(new JButton("3-2"));

        panel4.add(new JButton("4-1"));
        panel4.add(new JButton("4-2"));
        panel4.add(new JButton("4-3"));
        panel4.add(new JButton("4-3"));
        panel4.add(new JButton("4-5"));
        panel4.add(new JButton("4-6"));

        container.add(panel);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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

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

 运行结果:

2.6 图片按钮、单选框、多选框

图片按钮:

public class JButtonDemo extends JFrame {
    public JButtonDemo(){
        //容器
        Container container = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonDemo.class.getResource("pic.PNG");
        ImageIcon icon = new ImageIcon(resource);
        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        //设置提示信息
        button.setToolTipText("图片按钮");
        container.add(button);

        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo();
    }
}

 运行结果:

单选框:

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        //容器
        Container container = this.getContentPane();
        //把图片变成一个图标
        URL resource = JButtonDemo02.class.getResource("pic.PNG");
        ImageIcon icon = new ImageIcon(resource);
        //单选框
        JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
        //不能被同时选择----->分组  一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton01);
        group.add(jRadioButton02);
        group.add(jRadioButton03);

        container.add(jRadioButton01,BorderLayout.NORTH);
        container.add(jRadioButton02,BorderLayout.CENTER);
        container.add(jRadioButton03,BorderLayout.SOUTH);

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

    }

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

 运行结果:

多选框:

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03(){
        //容器
        Container container = this.getContentPane();
        //把图片变为图标
        URL resource = JButtonDemo03.class.getResource("pic.PNG");
        ImageIcon icon = new ImageIcon(resource);
        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");

        container.add(checkBox1,BorderLayout.EAST);
        container.add(checkBox2,BorderLayout.WEST);

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

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

 运行结果:

2.7 下拉框、列表框

下拉框:

//下拉框 适用于选择地区,或者一些单个选项
public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container = this.getContentPane();

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

        container.add(comboBox);

        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 vector = new Vector();
        vector.add("aa");
        vector.add("bb");
        vector.add("cc");
        JList list = new JList(vector);

        container.add(list);

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

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

 运行结果:

2.8 文本框、密码框、文本域

文本框:

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container container = this.getContentPane();

        //文本框
        JTextField textField01 = new JTextField("hello");
        JTextField textField02 = new JTextField("world");

        container.add(textField01,BorderLayout.NORTH);
        container.add(textField02,BorderLayout.SOUTH);

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

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

运行结果:

 密码框:

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        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 TestTextDemo02();
    }
}

运行结果:

 文本域:

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("biubiu");
        //scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);

        container.add(scrollPane);

        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestTextDemo03();
    }
}

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020418.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号