DAY8 GUI编程_02

这篇博客详细介绍了Java Swing库在GUI编程中的应用,包括窗口和面板的创建、各种类型的对话框、标签和图标使用、ImageIcon操作、JPanel组件、页面滚轮JScroll的实现、不同类型的按钮如JButton、单选按钮和复选按钮,以及列表组件如下拉框和列表框的使用,最后还涵盖了各种文本输入框,如文本框、密码框和文本域。

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

1.Swing

1.窗口,面板

package com.GUI.Swing;
​
import javax.swing.*;
import java.awt.*;
​
public class JFrameDemo extends JFrame{
    //init();初始化
    public void init(){
        //顶级面板
        JFrame jFrame = new JFrame("JFrame窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(200,200,400,300);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //创建一个容器,让配置生效
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.BLUE);
        Label label = new Label();
        label.setText("JFrame窗口");
        jFrame.add(label);
​
    }
    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}

2.弹窗dialog

package com.GUI.Swing;
​
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class JDialogDemo extends JFrame {
    public JDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = new Container();
        container.setLayout(null);
        JButton jButton = new JButton("点击按钮");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new JDialog();
            }
        });
        this.add(jButton);
​
    }
    public static void main(String[] args) {
    new JDialogDemo();
    }
}
​
class JDialog extends javax.swing.JDialog {
    public JDialog() {
        this.setVisible(true);
        this.setBounds(50,50,300,300);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = new Container();
        //container.setLayout(null);
        Label label = new Label();
        label.setText("你点击了按钮");
        container.add(label);
    }
}

3.标签 label,icon

package 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值