在JDesktopPane / JFrame上重新打开JInternalFrame

时间:2015-10-16 02:07:41

标签: java swing jframe jinternalframe

我在代码中重新打开一个JInternalFrame时遇到问题。我在MenuBar选项中选择“Cadastro” - >“CadastrodeVeículos”,此动作打开一个屏幕以插入车辆。但是,如果我关闭此屏幕并尝试重新打开它,我就不能了。

下面是我正在使用的两个代码。

  • 首先,JMain(这是我的JFrame):

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyVetoException;
    
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.WindowConstants;
    import javax.swing.event.InternalFrameListener;
    
    public class JMain extends JFrame implements ActionListener{
    
        /**
         * Create the panel.
         */
        private JMenuBar menuBar = new JMenuBar();
    
        private JMenu firstMenu = new JMenu("Cadastro");
        private JMenu secndMenu = new JMenu("Indicadores");
        private JMenu thirdMenu = new JMenu("Agendamentos");
        private JMenu fourthMenu = new JMenu("Relatórios");
    
        private JMenuItem cadVeiculos = new JMenuItem("Cadastro de Veículos");
        private JMenuItem cadMotoristas = new JMenuItem("Cadastro de Motoristas");
        private JMenuItem cadCargas = new JMenuItem("Cadastro de Cargas");
        private JMenuItem newExit = new JMenuItem("Sair");
    
        public  JDesktopPane jdPane = new JDesktopPane();
    
        JCadVeiculos telaCadVeiculos;
    
    
    
        public static void main (String args []){
            JMain jmain = new JMain();
        }
    
        public JMain() {
            jdPane.setBackground(Color.LIGHT_GRAY);
            setTitle("Gtrix - Version 0.1");
            setSize(600, 500);
    
            getContentPane().add(jdPane);
    
            setJMenuBar(menuBar);
    
            menuBar.add(firstMenu);
            menuBar.add(secndMenu);
            menuBar.add(thirdMenu);
            menuBar.add(fourthMenu);
    
            firstMenu.add(cadVeiculos);
            firstMenu.add(cadMotoristas);
            firstMenu.add(cadCargas);
            firstMenu.addSeparator();
            firstMenu.add(newExit);
    
            cadVeiculos.addActionListener(this);
    
            setVisible(true);   
        }
    
        public void actionPerformed(ActionEvent evt) {
            if(evt.getSource() == cadVeiculos){
                if(telaCadVeiculos == null){
                    telaCadVeiculos = new JCadVeiculos(this);
                }
    
                //telaCadVeiculos.show();
                //telaCadVeiculos.setDefaultCloseOperation(JCadVeiculos.DO_NOTHING_ON_CLOSE);
                jdPane.moveToFront(telaCadVeiculos);
    
            }
        }
    }
    

和JCadVeiculos(我的JInternalFrame):

    package ui;

import java.awt.EventQueue;
import java.awt.Menu;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

public class JCadVeiculos extends JInternalFrame {

    private JMain telaPrincipal;

    /**
     * Create the frame.
     */
    public JCadVeiculos(JMain telaPrincipal) {
        super("", true, true, false);
        setSize(600,500);
        setTitle("Cadastro de Veículos");
        setVisible(true);

        this.telaPrincipal = telaPrincipal;
        telaPrincipal.jdPane.add(this);
    }

}

请帮帮我! :)

1 个答案:

答案 0 :(得分:0)

You will need to make the JInternalFrame visible again, as it was "hidden" when it was closed, for example...

public void actionPerformed(ActionEvent evt) {
    if(evt.getSource() == cadVeiculos){
        if(telaCadVeiculos == null){
            telaCadVeiculos = new JCadVeiculos(this);
        }

        if (!telaCadVeiculos.getParent().equals(jdPane)) {
            jdPane.add(telaCadVeiculos);
        }
        telaCadVeiculos.setVisible(true);

        //telaCadVeiculos.show();
        //telaCadVeiculos.setDefaultCloseOperation(JCadVeiculos.DO_NOTHING_ON_CLOSE);
        jdPane.moveToFront(telaCadVeiculos);

    }
}

On a personal note, I find...

 telaPrincipal.jdPane.add(this);

in you JCadVeiculos class worrying. Your JCadVeiculos shouldn't really be making decisions about how it might be getting used and I'd personally refrain from making components publicly visible in this way, as it leads to possible misuse on the part of other developers.

The responsibility for the management of the JDesktopPane lies with the JMain class, it should be deciding what gets added to the JDesktopPane and when

Just saying.

You might also like to take a look at Initial Threads并确保您在事件调度线程的上下文中初始化您的用户界面