【Java GUI 编程】Swing 用户界面开发工具包

本文介绍了Java Swing用户界面开发工具包,包括窗口、弹窗、标签、面板、滚动条、按钮、列表和文本框等组件的使用,并展示了如何通过Swing构建美观的GUI。此外,还提及了利用Swing进行贪吃蛇游戏的GUI编程实践。
摘要由CSDN通过智能技术生成

Swing

用户界面开发工具包,比 AWT 更加高级一点,Swing 可以使用任何可插拔的外观风格 ,用很少的代码就可以创建优雅的用户界面,工具包中所有的包都是以swing作为名称

窗口

package JavaGUI;

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

/**
 * @Title: Test13JFrame
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 13:57
 */
public class Test13JFrame {
   
    public static void main(String[] args) {
   
        new MyJFrame().init();
    }
}

class MyJFrame extends JFrame{
   
    public void init(){
   
        // 获得一个容器
        Container contentPane = this.getContentPane();
        contentPane.setBackground(Color.red);

        JLabel label = new JLabel("欢迎来到 Java !");
        this.add(label);

        // 水平居中标签
        label.setHorizontalAlignment(SwingConstants.CENTER);

        this.setVisible(true);
        this.setBounds(1,1,200,200);
    }
}

弹窗

JDialog 用来被弹出窗口

package JavaGUI;

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

/**
 * @Title: Test15Dialog
 * @Package JavaGUI
 * @Description:
 * @author: maze
 * @date 2020/10/20下午 17:31
 */
public class Test15Dialog extends JFrame{
   
    public Test15Dialog(){
   
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 放东西,容器
        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 Test15Dialog();
    }
}

// 弹窗的窗口
class MyDialogDemo extends JDialog{
   
    public MyDialogDemo(){
   
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        Container container = this.getContentPane();
        container.setLayout(null); //绝对定位
        container.add(new Label("学 Java GUI 编程"));
    }
}

在这里插入图片描述

标签

label

画了一个圆作为标签

public class Test16IconDemo1 extends JFrame implements Icon {
   

    private int width;
    private int height;
    
    public Test16IconDemo1(){
    }
    
    public Test16IconDemo1(int width,int height){
   
        this.width = width;
        this.height = height;
    }
    public void init(){
   
        Test16IconDemo1 iconDemo1 = new Test16IconDemo1(15,15);
        // 图标放在标签上,也可以放在按钮上
        JLabel jLabel = new JLabel("icontest",iconDemo1,SwingConstants.CENTER);

        Container container = getContentPane()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿的温柔香

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

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

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

打赏作者

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

抵扣说明:

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

余额充值