java 自定义对话框

按着书写的。。不过看懂了。。

package J;
public class Example9_20 {
	public static void main(String[] args) {
		MyWindow win=new MyWindow();
		win.setTitle("自定义对话框窗口");
		win.setBounds(600,200,300,400);
	}

}

package J;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyWindow extends JFrame implements ActionListener{
	JButton button;
	MyDialog dialog;
	MyWindow(){
		init();
		setLayout(new FlowLayout());
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	void init(){
		button=new JButton("打开对话框");
		add(button,BorderLayout.NORTH);
		button.addActionListener(this);
		dialog =new MyDialog(this,"我是对话框");
		dialog.setModal(true);
	}
	public void actionPerformed(ActionEvent e){
		dialog.setVisible(true);
		String str=dialog.getTitle();
		setTitle(str);
	}
}

package J;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDialog extends JDialog implements ActionListener{
	JTextField input;
	JButton button;
	String title;
	MyDialog(JFrame f,String s){
		super(f,s);
		input=new JTextField(10);
		input.addActionListener(this);
		setLayout(new FlowLayout());
		add(new JLabel("输入窗口新标题"));
		add(input);
		button=new JButton("确定");
		add(button);
		button.addActionListener(this);
		setBounds(600,260,100,150);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e){
		title=input.getText();
		setVisible(false);
	}
	public String getTitle(){
		return title;
	}

}

这是运行结果 java 自定义对话框_第1张图片


你可能感兴趣的:(【java】)