现在的问题是JDialog弹出的时候,总是大小不够,用户需要手动去拉伸对话框来改变大小
能否给对话框加上一个最大化的按钮的呢?我现有的知识认为是不能的??
那么就只能用JWindow或JFrame,但是用他们我又不知如何实现模态对话框的效果,即始终居于程序最顶部
哪位大虾能指点一下呢?
要么是让对话框又最大化按钮
要么是让JWindow或JFrame能够实现模态对话框的效果

解决方案 »

  1.   

    楼主说的好象都不可以
    不知道要在什么时候最大化JDialog
    如果只要求大小是全屏的话
    可以设置size
      

  2.   

    JFrame模态两种方法:
    1) JNI
    2) 
    frame.addWindowFocusListener(new WindowFocusListener() {
      public  void  windowGainedFocus(WindowEvent e) {}
      public  void  windowLostFocus(WindowEvent e)  {
          frame.toFront();
      } 
    } );第二种方法也有些问题,从其他程序切换回来的时候,弹出的JFrame不在最前面。
     
      

  3.   

    frame.toFront();
    这个方法我试过了
    不是想要的效果
    不知道为什么...
      

  4.   

    如果自己设置的对话框大小不合适,可以在生成了对话框后,使用如下的方法,pack(),自动根据控件的大小来设置对话框的大小。
      

  5.   

    手头正好有实现JFrame的模态化的代码:
     
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;public class ModalFrameUtil {
        public ModalFrameUtil() {
            //
        }    static class EventPump implements InvocationHandler {
            Frame frame;        public EventPump(Frame frame) {
                this.frame = frame;
            }        public Object invoke(Object proxy, Method method, Object[] args) throws
                    Throwable {
                return frame.isShowing() ? Boolean.TRUE : Boolean.FALSE;
            }        // when the reflection calls in this method has to be
            // replaced once Sun provides a public API to pump events.
            public void start() throws Exception {
                Class clazz = Class.forName("java.awt.Conditional");
                Object conditional = Proxy.newProxyInstance(
                        clazz.getClassLoader(),
                        new Class[] {clazz},
                        this);
                Method pumpMethod = Class.forName("java.awt.EventDispatchThread")
                        .getDeclaredMethod("pumpEvents", new Class[] {clazz});
                pumpMethod.setAccessible(true);
                pumpMethod.invoke(Thread.currentThread(), new Object[] {conditional});
            }
        }    // 调用方法
        public static void showAsModal(final Frame frame, final Frame owner) {
            frame.addWindowListener(new WindowAdapter() {
                public void windowOpened(WindowEvent e) {
                    owner.setEnabled(false);
                }            public void windowClosed(WindowEvent e) {
                    owner.setEnabled(true);
                    owner.removeWindowListener(this);
                }
            });        owner.addWindowListener(new WindowAdapter() {
                public void windowActivated(WindowEvent e) {
                    if (frame.isShowing()) {
                        frame.setExtendedState(JFrame.NORMAL);
                        frame.toFront();
                    }
                    else {
                        owner.removeWindowListener(this);
                    }
                }
            });        frame.setVisible(true);
            try {
                new EventPump(frame).start();
            }
            catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }}