欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java awt-对话框简单实现

程序员文章站 2022-07-13 23:25:00
...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dia
{
	public static void main(String args[])
	{
		JButton btn1=new JButton("modal");
		JButton btn2=new JButton("unmodal");
		JFrame f=new JFrame("DIaloDemo");
		f.setSize(300,400);
		f.setLocation(300,200);
		f.setLayout(new FlowLayout());
		f.add(btn1);
		f.add(btn2);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);

		final JLabel label=new JLabel();
		final JDialog dialog=new JDialog(f,"Dialog");
		dialog.setSize(220,150);
		dialog.setLocation(350,250);
		dialog.setLayout(new FlowLayout());

		final JButton btn3=new JButton("sure");
		dialog.add(btn3);
		
		btn1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialog.setModal(true);
				if(dialog.getComponents().length==1){
					dialog.add(label);
				}
				label.setText("modal dialog,click to close");
				dialog.setVisible(true);
			}
		});

		
		btn2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialog.setModal(false);
				if(dialog.getComponents().length==1){
					dialog.add(label);
				}
				label.setText("unmodal dialog,click to close");
				dialog.setVisible(true);
			}
		});

		
		btn3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialog.dispose();
			}
		});

	}
}


Java awt-对话框简单实现    

Java awt-对话框简单实现              Java awt-对话框简单实现