Java modal window with maximize button

后端 未结 3 2033
灰色年华
灰色年华 2021-01-02 06:26

How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame or create a JDialog with maxim

3条回答
  •  佛祖请我去吃肉
    2021-01-02 06:36

    Solution 1: Tested on Windows

    I used a JFrame for the modal window

    JFrame mainWindow = new JFrame;
    mainWindow.setVisible(true);
    JFrame modalWindow = new JFrame();
    // The next two sentences gives modalWindow modal beahaviour
    mainWindow.setEnabled(false);
    mainWindow.setFocusable(false);
    modalWindow.setVisible(true);
    

    Solution 2: Tested on Ubuntu

    I added a WindowFocusListener

    addWindowFocusListener(new java.awt.event.WindowFocusListener() {
        public void windowGainedFocus(java.awt.event.WindowEvent evt) {}
        public void windowLostFocus(java.awt.event.WindowEvent evt) {
            formWindowLostFocus(evt);}
    
    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {
        this.requestFocus();
        this.toFront();}
    

提交回复
热议问题