Java Swing Dialog 弹窗使用教程

一、新建一个带按钮的JFrame 参考教程[跳转]参考代码如下(https://blog.csdn.net/jarvan5/article/details/105621342)
package GUI.Swing.Dialog弹窗;

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

public class Demo extends JFrame {
    private void init() {
        //初始化JFrame
        this.setVisible(true);
        this.setLocation(100, 100);
        this.setSize(700, 500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //容器,放东西
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");
        //水平居中
        jButton.setHorizontalAlignment(SwingConstants.CENTER);
        jButton.setSize(200, 50);
        //按钮添加到容器
        contentPane.add(jButton);
        //添加事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //new 弹窗
                new DemoDialog弹窗();

            }
        });

    }

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

二、新建一个 DemoDialog弹窗.java 用来写弹窗的内容。

1.extends JDialog 继承JDialog
2.其他操作和JFrame(Frame)类似
3.问题:设置居中对按钮无效?待解决?
package GUI.Swing.Dialog弹窗;

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

public class DemoDialog弹窗 extends JDialog {
    //construct method 构造方法初始化弹窗样式
    DemoDialog弹窗(){
        this.setTitle("Dialog弹窗");
        this.setVisible(true);
        this.setLocation(200,200);
        this.setSize(200,250);
        //add one label
        Container contentPane = this.getContentPane();
        JLabel jLabel = new JLabel("再容器中添加标签");
        contentPane.add(jLabel);
        //center 居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);


    }
}

三、拓展 自带的几个 弹窗样式.更多

public class Task2 {
	Task2()
	{
		
		String firstName=JOptionPane.showInputDialog("Please input your first name","Jiawen");
		String lastName=JOptionPane.showInputDialog("please input your last name","Deng");
		String fullName=firstName+" "+lastName;
		JOptionPane.showMessageDialog(null, fullName,"YOUR FULL NMAE",JOptionPane.PLAIN_MESSAGE);
			
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JarvanStack

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

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

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

打赏作者

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

抵扣说明:

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

余额充值