利用回调获取自拟定Dialog的返回值

        这里要讲的是如何利用回调获取自拟定DiaLog的返回值,首先看下问题的背景。我们经常会用到Dialog,系统提供了一些常用的Dialog,可以参考这篇博客 点击打开链接 。那其中一个常用Dialog做讲解,
String returnString = JOptionPane.showInputDialog("title");
当执行到这条语句时,将弹出一个对话框,程序被阻塞,直到Dialog被关闭,返回retrunString。但很多情况下并不够用,所以我们通常会自拟定自己的Dialog。可是如何从Dialog返回值到主窗口这是问题了,有些人可能会把MyDialog设置为内部类,这样容易导致代码重复和臃肿。所以这里将MyDialog设置为单独类,利用回调返回MyDialog中填入的值。关于回调的讲解,这里有篇博客讲的很通俗,值得大家看下,xiaanming的博客点击打开链接
       以下是自己码的实例代码:
回调的接口:
public interface DialogListener {
	public void getInputString(String inputString);
}
MyDialog类:
public class MyDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton comfirmBtn;
	private JButton cancelBtn;
	private JTextField inputField;
	private DialogListener dialogListener;
	
	public MyDialog(String title){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(screenSize.width/4, screenSize.height/4);
		this.setLocation(3*screenSize.width/8, 3*screenSize.height/8);
		
		JPanel mainPanel = new JPanel(new GridLayout(4,1, 10,10));

		JLabel label = new JLabel(title);
		label.setFont(new Font("宋体", Font.BOLD ,30));
		inputField = new JTextField();
		inputField.setFont(new Font("宋体", Font.BOLD ,30));
		JPanel btnPanel = new JPanel(new GridLayout(1,2,20,20));
		comfirmBtn = new JButton("确认");
		cancelBtn = new JButton("取消");
		comfirmBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//调用回调函数返回Dialog输入的内容
				dialogListener.getInputString(inputField.getText());
			}
		});
		
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialogListener.getInputString("");
			}
		});
		btnPanel.add(comfirmBtn);
		btnPanel.add(cancelBtn);
		
		mainPanel.add(label);
		mainPanel.add(inputField);
		mainPanel.add(btnPanel);
		this.getContentPane().add(mainPanel);
		this.setVisible(true);
		this.setAlwaysOnTop(true);
	}
	
	public void setOnDialogListener(DialogListener listener){
		this.dialogListener = listener;//将Main的实例传递过来
	}
}
Main类:
public class Main extends JFrame implements DialogListener{
	private JLabel label;
	private JButton btn;
	private MyDialog dialog;
	public Main(){
		JPanel panel = new JPanel();
		btn = new JButton("对话窗口");
		btn.setFont(new Font("宋体", Font.BOLD ,30));
		label = new JLabel();
		label.setFont(new Font("宋体", Font.BOLD ,30));
		panel.add(btn);
		panel.add(label);
		
		btn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
				//String str = JOptionPane.showInputDialog("title");
				//label.setText(str);
				
				dialog = new MyDialog("title");
				dialog.setOnDialogListener(Main.this);
				//此时不会阻塞,将继续执行下面语句
				System.out.println("continue");
				
			}
		});
		this.getContentPane().add(panel);
		this.setSize(600,600);
		this.setVisible(true);
	}

	@Override
	public void getInputString(String returnString){
		//重载回调函数
		label.setText(returnString);
		dialog.dispose();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main main = new Main();
	}
}

Main这里实现接口,重载回调函数可以参考设置JButton的监听。如上是一种方式,还有通过如下方式实现Main:
public class Main extends JFrame {
	private JLabel label;
	private JButton btn;
	private MyDialog dialog;
	public Main(){
		JPanel panel = new JPanel();
		btn = new JButton("对话窗口");
		btn.setFont(new Font("宋体", Font.BOLD ,30));
		label = new JLabel();
		label.setFont(new Font("宋体", Font.BOLD ,30));
		panel.add(btn);
		panel.add(label);
		
		btn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
				//String str = JOptionPane.showInputDialog("title");
				//label.setText(str);
				
				dialog = new MyDialog("title");
				dialog.setOnDialogListener(new DialogListener(){
					@Override
					public void getInputString(String returnString){
						//重载回调函数
						label.setText(returnString);
						dialog.dispose();
					}
				});
				//此时不会阻塞,将继续执行下面语句
				System.out.println("continue");
				
			}
		});
		this.getContentPane().add(panel);
		this.setSize(600,600);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main main = new Main();
	}
}





你可能感兴趣的:(Java,自拟定Dialog,返回值,回调,MyDialog)