P4:java之GUI编程

Swing

Swing 是一个为Java设计的GUI工具包。

Swing是JAVA基础类的一部分。

Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

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

【代码案例】利用Swing工具包创建一个JFrame窗口,并在其窗口内定义一系列属性。

package com.gui;
import javax.swing.*;
import java.awt.*;

public class Demo08 {
        /**{
         * 创建并显示GUI。出于线程安全的考虑,
         * 这个方法在事件调用线程中调用。
         */
        private static void MyJfreame() {

            // 创建及设置窗口
            JFrame frame = new JFrame("HelloWorldSwing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口事件

            // 添加 "Hello World" 标签
            JLabel label = new JLabel("Hello World");
            label.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中
            frame.getContentPane().add(label);

            // 显示窗口
            frame.setBounds(100,100,400,400);
            frame.setVisible(true);

            //获得一个容器,在容器内定义颜色属性!
            Container container = frame.getContentPane();
            container.setBackground(Color.cyan);

        }

        public static void main(String[] args) {
            // 显示应用 GUI
            new Demo08().MyJfreame();
        }
}

运行结果:
在这里插入图片描述

Dialog弹窗

/**
*与awt组件不同,Swing组件不能直接的添加到顶层容器中,他必须添加到一个与Swing顶层容器相关联的内容模板(contentpane)上;内容面板是顶层容器包含的一个普通容器,它是一个轻量级组件。基本规则如下: (1)把Swing组件放入一个顶层Swing容器的内容面板上 (2)避免使用非Swing的重量级组件。 * * */

package com.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//JDialog弹窗
public class Demo09 extends JFrame {
    public static void main(String[] args) {
            new Demo09().DialogDemo();
    }
    public void DialogDemo (){
        //设置窗口
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //获得一个容器
        Container container = this.getContentPane();
        container.setLayout(null);  //绝对布局
        //添加及设置按钮
        JButton jButton = new JButton("弹窗按钮");
        jButton.setBounds(30,30,200,50);
        //给按钮添加一个监听事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    new MyDialog();
            }
        });
        container.add(jButton);
    }
}
//创建及设置弹窗
class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,300,300);

        Container container = this.getContentPane();
        container.setBackground(Color.cyan);
        container.setLayout(null);

        JLabel jLabel = new JLabel("我是一个弹窗");
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);  //设置标签居中
        jLabel.setBounds(30,30,200,50);

        container.add(jLabel);
    }
}

运行结果:
在这里插入图片描述

标签

Java中swing使用ImageIcon类添加图片

package com.gui;

import javax.swing.*;
//swing使用ImageIcon类添加图片
public class ImageIconDemo extends JFrame {
    public void GUI(){
        setTitle("测试窗口");
        JPanel jPanel = new JPanel();

        JLabel jLabel = new JLabel("标签");
        ImageIcon icon = new ImageIcon("src/com/gui/tp.jpg");
        jLabel.setIcon(icon);
        jLabel.add(jPanel);
        add(jLabel);

        setExtendedState(JFrame.MAXIMIZED_BOTH);  //窗口最大化
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口
        setVisible(true);

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

}

运行结果:

在这里插入图片描述
其中ImageIcon图片相对路径问题

  1. 假如你的工程根目录为:Project

你的图片文件放在:Project/src/com/gui/1.jpg

所以正确的调用是(不要加Project):

ImageIcon icon = new ImageIcon(“src/com/gui/1.jpg”);

  1. 假如你的工程根目录为:Project

你的图片文件在:Project1/src/com/gui/1.jpg

所以正确的调用是(com是包名)

ImageIcon icon = new ImageIcon(“src/com/lgui/1.jpg”);

这行代码执行时在project/test目录下查找到了文件

总结起来就是一句话:所谓相对路径就是相对于工程根目录的位置_

JSCrollpane(滚动窗口)

当一个容器内放置了许多组件,而容器的显示区域不足以同时显示所有组件时,如果让容器带滚动条,通过移动滚动条的滑块,容器中位置上的组件就能看到。滚动面板JScrollPane能实现这样的要求,JScrollPane是带有滚动条的面板。JScrollPane是Container类的子类,也是一种容器,但是只能添加一个组件。JScrollPane的一般用法是先将一些组件添加到一个JPanel中,然后再把这个JPanel添加到JScrollPane中。这样,从界面上看,在滚动面板上,好像也有多个组件。在Swing中,像JTextArea、JList、JTable等组件都没有自带滚动条,都需要将它们放置于滚动面板,利用滚动面板的滚动条,浏览组件中的内容。

【代码实例】

package com.gui;

import javax.swing.*;
import java.awt.*;
//创建JSCroll滚动面板
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea("20,50");
        textArea.setText("欢迎学习滚动面板");

        //SCroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);

        this.setVisible(true);
        this.setBounds(100,100,400,450);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JScrollDemo();
    }
}

运行结果:
在这里插入图片描述

Button按钮

图片按钮

【代码实例】

package com.gui;

import javax.swing.*;
import java.awt.*;
//图片按钮
public class JButtonDem01 extends JFrame {
    public JButtonDem01()  {
        Container container = this.getContentPane();
        ImageIcon imageIcon = new ImageIcon("src/com/gui/tp.jpg");

        JButton button = new JButton();
        button.setIcon(imageIcon);
        button.setToolTipText("图片按钮");

        container.add(button);

        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:
在这里插入图片描述

单选按钮

单选按钮就是在给定的多个选择项中选择一个,并且只能选择一个。在Swing中可以使用JRadioButton完成一组单选按钮的操作

【代码实例】

package com.gui;
//单选按钮
import javax.swing.*;
import java.awt.*;

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02()  {
        Container container = this.getContentPane();//获得一个容器

        //创建按钮
        JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");

        //由于按钮只能选择一个,所以将按钮分组,一个组中只能选择一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        //将按钮添加在容器上
        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

运行结果:
在这里插入图片描述

复选框按钮

程序可以通过JRadioButton实现单选按钮的功能,那么如果要实现复选框的功能,则可以使用JCheckBox完成。

【代码实例】

package com.gui;
//复选框按钮
import javax.swing.*;
import java.awt.*;

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container contentPane = this.getContentPane(); //容器
        //复选框按钮
        Checkbox checkbox1 = new Checkbox("chockbox1");
        Checkbox checkbox2 = new Checkbox("chockbox2");

        contentPane.add(checkbox1,BorderLayout.NORTH);
        contentPane.add(checkbox2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值