程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Java图形界面编程--对话框JDialog(模态对话框,非模态对话框)

发布于2021-03-13 14:10     阅读(1141)     评论(0)     点赞(8)     收藏(5)


对话框JDialog(模态对话框,非模态对话框)

package com.lddx.day0309;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

//对话框JDialog(模态对话框,非模态对话框)
public class TestDialog {
	public static void main(String[] args) {
		
		TestDialog d=new TestDialog();
		d.init();
	}
	JFrame f=new JFrame();
	public void init(){
		
		JPanel p=new JPanel();
		JButton b1=new JButton("模态按钮");
		JButton b2=new JButton("非模态按钮");
		
		//把内部类绑定到按钮
		MyListener ml=new MyListener();
		b1.addActionListener(ml);
		b2.addActionListener(ml);
		
		p.add(b1);
		p.add(b2);
		f.add(p);
		
		f.setTitle("对话框");
		f.setSize(600, 600);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	//定义内部类实现接口ActionListener接口
	public class MyListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			String str=e.getActionCommand();//获得事件源内容
			System.out.println(str);	
			if(str.equals("模态按钮"))
			{
				JDialog  d=new JDialog(f,"模态对话框");
				d.setModal(true);//设置对话框为模态对话框
				d.setSize(300, 300);
				d.setTitle("What are doing!");
				d.setVisible(true);//设置对话框可见
			}
			else
			{
				JDialog  d=new JDialog(f,"非模态对话框");
				d.setModal(false);//设置对话框为非模态对话框
				d.setSize(300, 300);
				d.setTitle("你弄啥嘞!");
				d.setVisible(true);//设置对话框可见
			}
		}
	}
}

原文链接:https://blog.csdn.net/gezongbo/article/details/114684774



所属网站分类: 技术文章 > 博客

作者:木得事

链接:http://www.javaheidong.com/blog/article/114290/d4ea167ff92c5ed46ae7/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)