JAVA中JDialog的举例

本文展示了如何使用Java创建模态对话框,并通过按钮触发对话框的显示。代码中涉及窗口布局、按钮事件监听及对话框的基本操作。

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

This program demonstrates the creation of a JDialog from a super-window. The created dialog is on the mode "Modal".

package com.han; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * This program demonstrates the creation of a JDialog from a super-window. * The created dialog is on the mode "Modal". * @author han * */ public class SwingJDialog { public SwingJDialog(){ final JFrame jf=new JFrame("弹出窗体实验"); // Some methods defined by Toolkit query the native operating system directly. Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize(); int Swing1x=500; int Swing1y=300; jf.setBounds(screensize.width/2-Swing1x/2,screensize.height/2-Swing1y/2,Swing1x,Swing1y); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c=jf.getContentPane(); c.setBackground(Color.pink); c.setLayout(null); Dimension Swing1size=jf.getSize(); JButton jb=new JButton("弹出对话窗"); int jbx=100; int jby=30; jb.setBounds(Swing1size.width/2-jbx/2,Swing1size.height/2-jby/2,jbx,jby); //jb.setBounds(Swing1x/2-jbx/2,Swing1y/2-jby/2,jbx,jby); c.add(jb); class Dialog1 { JDialog jd=new JDialog(jf,"JDialog窗体",true); Dialog1(){ jd.setSize(300,200); Container c2=jd.getContentPane(); c2.setLayout(null); JLabel jl=new JLabel("只是一个对话框"); jl.setBounds(0,-20,100,100); JButton jbb=new JButton("确定"); jbb.setBounds(100,100,60,30); c2.add(jl); c2.add(jbb); jbb.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { jd.dispose(); //System.exit(0); } }); System.out.println("OK"); jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } } jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ new Dialog1().jd.setVisible(true);//弹出对话框 System.out.println("OK2"); } }); System.out.println("OK3"); } public static void main(String[] args){ new SwingJDialog(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值