zoukankan      html  css  js  c++  java
  • JFrame、JDialog close

    package common;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    /*2015-5-26*/
    public class SwingConsole {
    
        public static void run(final JFrame frame,final int width,final int height){
            SwingUtilities.invokeLater(new Runnable() {
                
                @Override
                public void run() {
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(width, height);
                    frame.setTitle(frame.getClass().getSimpleName());
                    frame.pack();// //该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                }
            });
        }
    }
    package dialogclose;
    
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    /*2015-6-3*/
    public class JFrameDemo extends JFrame {
        private static final long serialVersionUID = 1L;
        private JButton button;
        private JTextField result;
    
        public JFrameDemo() {
            // this.setLayout(new GridLayout(4, 4));
            this.setLayout(new FlowLayout());
            button = new JButton("click");
            getContentPane().add(button);
            result = new JTextField("...");
            result.setPreferredSize(new Dimension(200, 20));
            getContentPane().add(result);
        }
    
        public static void main(String[] args) {
            final JFrameDemo frame = new JFrameDemo();
            frame.button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("before open dialog");
                    JDialogController dlg = new JDialogController(frame);
                    frame.result.setText("Result:" + dlg.isCancel());
                    System.out.println("after close dialog");
                    if (dlg.isCancel()) {
                        System.out.println("is canceled");
                        return;
                    }
                    System.out.println(dlg.isCancel());
                }
            });
    
            common.SwingConsole.run(frame, 800, 600);
    
        }
    
    }
    package dialogclose;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    /*2015-6-3*/
    public class JDialogController {
    
        private boolean cancel = true;
    
        public JDialogController(JFrame parent) {
            final JDialogDemo dlg = new JDialogDemo(parent);
            dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            /*dlg.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    int result = JOptionPane.showConfirmDialog(dlg, "是否退出本系统!", "退出消息框", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (result == JOptionPane.YES_OPTION)
                    {
                        System.out.println("Choose Yes");
                        System.exit(0);
                    }
                    else if (result == JOptionPane.NO_OPTION)
                    {
                        System.out.println("Choose No");
                        dlg.setVisible(true);
                        dlg.validate();
                    }
                }
    
            });*/
    
            dlg.setVisible(true);
        }
    
        public boolean isCancel() {
            return cancel;
        }
    }
    package dialogclose;
    
    import java.awt.FlowLayout;
    
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    /*2015-6-3*/
    public class JDialogDemo extends JDialog {
        private static final long serialVersionUID = 1L;
    
        private boolean cancel=true;
        public JDialogDemo(JFrame parent) {
            super(parent,true);
            this.setLayout(new FlowLayout());
            this.setSize(400, 200);
    
            this.add(new JTextField("test"));
            this.setLocationRelativeTo(null);
        }
        public boolean isCancel() {
            return cancel;
        }
    }

    关闭JDialog的几个方法:
    1、如果你只是想让该对象隐藏起来,就在按扭的事件处理方法中让使用dialog.setVisible(false);方法;
    2、如果你想关闭的时候让该对话框释放资源而又不退出程序,可以使用dialog.dispose();
    方法或dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    3、如果你想关闭的时候退出程序可以使用System.exit(0); 
    用dispose()关闭
    this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);   
    this.addWindowListener(new WindowAdapter(){     
      public void windowClosing(WindowEvent e) {       
      dispose();     
      }   
    });
    http://www.cnblogs.com/jaward/articles/2826924.html

    JTextField设置长度
    JTextField jtf = new JTextField(130); 

    public void setPreferredSize(Dimension preferredSize)
    http://www.iteye.com/problems/89359

    JFrame类的其他常用方法:
     1.setBounds(int x,int y,int width,int height):参数x,y指定窗口出现在屏幕的位置;参数width,height指定窗口的宽度和高度。单位是像素。
     2.setSize(int width,int height):设置窗口的大小,参数width和height指定窗口的宽度和高度,单位是像素。
     3.setBackground(Color c):以参数 c设置窗口的背景颜色。
     4.setVisible(boolean b):参数b设置窗口是可见或不可见。JFrame默认是不可见的。
     5.pack():用紧凑方式显示窗口。如果不使用该方法,窗口初始出现时可能看不到窗口中的组件,当用户调整窗口的大小时,可能才能看到这些组件。
    http://www.cnblogs.com/Coda/p/4550219.html




  • 相关阅读:
    容器技术|Docker三剑客之docker-compose
    容器技术|Docker三剑客之docker-machine
    ProxySQL+Mysql实现数据库读写分离实战
    这20个Docker Command,有几个是你会的?
    水木桶的博客-序
    MVC实现 模型绑定传值
    Asp.net MVC模式实现登录功能
    项目开发过程中如何使用免费开发手机验证码验证功能(详细教程)
    项目开发过程中如何使用免费开发手机验证码验证功能(详细教程)
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/softidea/p/4550527.html
Copyright © 2011-2022 走看看