GUI编程核心技术之一(Swing)

1.什么是Swing?

Swing 是一个为Java设计的GUI工具包。
Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。
它是 AWT 组件的增强组件,但是它并不能完全替代 AWT 组件,这两种组件需要同时出现在一个图形用户界面中

特点:

  • 轻量级组件
  • 可插入外观组件
  • 跨平台使用

常用Swing组件

组件名称概述
JFrame代表 Swing 的框架类
JButton代表 Swing 按钮,按钮可以带一些图片或文字
JLabel代表 Swing 中的标签组件
JTextField代表文本框
JTextArea代表 Swing 中的文本区域
JPasswordField代表密码框
JComBox代表下拉列表框,可以在下拉显示区域显示多个选项
JCheckBox代表 Swing 中的复选框组件
2.窗口 标签居中
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
    public static void main(String[] args) {
        new MyJframe2().init();
    }
}
class MyJframe2 extends JFrame{
    public void init(){
        this.setBounds(10,10,200,300);
        this.setVisible(true);
        this.setBackground(Color.BLUE);//这个背景看不到,但确实存在
        JLabel label = new JLabel("欢迎来到Java系列节目");
        this.add(label);
        //让文本标签居中  设置水平对齐
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);//设置颜色
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗口事件-关闭窗口
    }
}

注意:
在JFame中设置背景,通过JFame.setBackgrand是看不到的。

原因:JFrame当中会默认使用流式布局管理器(FlowLayout)将整个窗体进行覆盖操作,也就是说设置的颜色确实是存在的,只是被布局管理器给覆盖住了,所以无法看见。
解决: 在JFrame当中添加一个面板容器,使得面板容器对窗体形成一个覆盖后,直接设置面板的背景颜色就可以达到相当于对窗体背景颜色进行设置的效果,然后将组建都添加到面板容器当中去。

3.弹窗-通过按钮弹出窗口

//主窗口
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();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton button = new JButton("点击弹出一个对话框"); //创建
        button.setBounds(30,30,200,50);
        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() { //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });

        container.add(button);
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        Container container = this.getContentPane();
        container.add(new Label("I Love Java"));
       // container.setLayout(null);
    }
}
4.按钮

(1)单选按钮

public class Danxuan extends JFrame {
    public Danxuan() {
        Container container = this.getContentPane();
        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);     
        group.add(radioButton2);
        group.add(radioButton3);
        container.add(radioButton1,BorderLayout.CENTER); // 东西南北布局
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}

(2)多选按钮

public class Danxuan extends JFrame {
    public Danxuan() {
        Container container = this.getContentPane();
        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}
5.列表

(1)下拉框

public class Danxuan extends JFrame {
    public Danxuan() {
        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,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}

(2)列表框

public class Danxuan extends JFrame {
    public Danxuan() {
        Container container = this.getContentPane();
        Vector contents = new Vector();
        //列表中需要放入内容
        contents.add("刘恒");
        contents.add("长鸣轩");
        contents.add("加流域");
        JList jList = new JList(contents);
        container.add(jList);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}
6.文本框

(1)密码框

public class Danxuan extends JFrame {
    public Danxuan() {
        Container container = this.getContentPane();
        JPasswordField passwordField = new JPasswordField(); //****
        passwordField.setEchoChar('*');//设置隐藏
        container.add(passwordField);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}

(2)文本域

public class Danxuan extends JFrame {
    public Danxuan() {
        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.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Danxuan();
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫僧洗发爱飘柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值