Java学习基础Swing

本文深入讲解Swing组件的使用,包括JFrame窗口的创建与配置,JDialog弹窗的实现,JLabel标签的定制,以及JPanel面板的布局管理。通过实例演示了如何设置窗口属性,响应用户操作,以及布局组件。

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

Swing

1、窗口,面板

Swing中用的是JFrame,是Frame的子类

package zhang.com.demo24;

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

public class JFrameDemo {

    //初始化
    public void init(){
        //顶级窗口
        JFrame jframe = new JFrame("这是第一个JFrame窗口");
        jframe.setVisible(true);
        jframe.setBounds(100,100,200,200);
        jframe.setBackground(Color.blue);
        //设置文字 并且居中
        JLabel jLabel = new JLabel("Hello World!",SwingConstants.CENTER);
        jframe.add(jLabel);
        //关闭窗口
		jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        //主方法,建立窗口
        new JFrameDemo().init();
    }
}

这个里面的方法与Frame大多类似,但是还有一些他自带的方法,比如说关闭窗口。就可以直接调用方法来关,但是Frame中就要用事件来关闭。调用Window适配器来进行关闭

package zhang.com.demo1;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Colse {
    public Colse(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
             @Override public void windowClosing(WindowEvent e) {System.exit(0);}});
    }
}

//Jframe
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

2、弹窗
JDialog,用来被弹出,默认就有关闭事件

package zhang.com.demo24;

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

public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setBounds(200,200,400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局,不会被拖动
        container.setLayout(null);
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(50,50,100,50);
        container.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo2();
            }
        });
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
class MyDialogDemo2 extends JDialog{
    public MyDialogDemo2(){

        this.setVisible(true);
        this.setBounds(100,100,500,500);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);

        contentPane.add(new Label("Hello,World!"));
    }
}

3、标签

package zhang.com.demo24;

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

public class MyTestLabel  extends JFrame implements Icon{
   private int width;
   private int heigh;

   public MyTestLabel(){
   }

   public MyTestLabel(int width,int heigh){
       this.width=width;
       this.heigh=heigh;
   }

   public  void init(){
       MyTestLabel myTestLabel = new MyTestLabel(15,15);
       //图标放在标签,也可以放在按钮上!
       JLabel icontest = new JLabel("Icontest", myTestLabel, SwingConstants.CENTER);

       Container contentPane = getContentPane();
       contentPane.add(icontest);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,heigh);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.heigh;

   }

    public static void main(String[] args) {
        new MyTestLabel().init();
    }
}

运行出来结果是:
在这里插入图片描述
4、面板

面板和Frema一样在前面加一个J即可。即为JPanel。

package zhang.com.demo24;

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

public class MyjPanelDemo extends JFrame {
    public static void main(String[] args) {
        new MyjPanelDemo();
    }

    public MyjPanelDemo() {
        Container container = this.getContentPane();
        //后面参数的意思,间距
        container.setLayout(new GridLayout(2, 1, 10, 10));
        JPanel panel1 = new JPanel(new GridLayout(1, 3));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(3, 2));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));


        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值