Swing之Jframe窗体、 JDialog弹窗、标签、面板、按钮(图片按钮,单选框,多选框)、列表(下拉框,列表框)、文本框、密码框

这篇博客详细介绍了Java Swing库中的核心组件,包括JFrame窗体、JDialog弹窗的使用,以及标签、面板、各种按钮(如图片按钮、单选框、多选框)的操作。同时,还涵盖了列表组件,如下拉框和列表框,以及文本输入组件——文本框和密码框的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swing

Jframe窗体

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

public class JFrameDemo {
   
    //init() 初始化
    public void init(){
   
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100, 100, 200, 200);
        //frame.setBackground(Color.pink);
        //设置一个容器
        frame.getContentPane().setBackground(Color.pink);
        //设置文字
        JLabel jLabel = new JLabel("阿涛来了");
        frame.add(jLabel);

        //让文本标签水平居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

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

    public static void main(String[] args) {
   
        //建立一个窗口
        new JFrameDemo().init();
    }
}

JDialog弹窗

弹窗默认有关闭事件!

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

//主窗口
public class DialogDemo extends JFrame {
   
    public static void main(String[] args) {
   
        new DialogDemo();
    }
    public DialogDemo() {
   
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(600, 600);

        //放东西,容器
        Container container = this.getContentPane();

        //绝对布局
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(30, 30, 200, 60);

        //监听器
        button.addActionListener(new ActionListener() {
   
            @Override
            public void actionPerformed(ActionEvent e) {
   
                //弹窗
                new MyDialog();
            }
        });
        container.add(button);
    }
}

//弹窗的窗口
class MyDialog extends JDialog{
   
    public MyDialog() {
   
        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("阿涛学JAVA"));
    }
}

标签

Icon标签:

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

public class IconDemo extends JFrame implements Icon 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值