2015-10-16 112 views
0

我有問題重新打開我的代碼中的一個JInternalFrame。我在MenuBar選項「Cadastro」 - >「Cadastro deVeículos」中選擇,此操作打開一個屏幕插入車輛。但是,如果我關閉此屏幕並嘗試重新打開它,我再也不能。重新打開JDesktopPane/JFrame上的JInternalFrame

下面是我正在使用的兩個代碼。

  • 首先,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); 
    } 

} 

請幫幫我! :)

回答

0

您將需要再次進行JInternalFrame可見的,因爲它是「隱藏」,當它被關閉,例如...

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); 

    } 
} 

就我個人而言,我覺得...

telaPrincipal.jdPane.add(this); 

在你裏面JCadVeiculos class worrying。您的JCadVeiculos不應該真正在做出關於它如何被使用的決定,我個人不會以這種方式公開顯示組件,因爲這會導致其他開發人員可能被濫用。

JDesktopPane管理的責任在於JMain類,應該決定什麼被添加到JDesktopPane

隨便說。

你可能也想看看Initial Threads,並確保你從事件指派線程

+0

我真的會閱讀更多關於這個鏈接,您發佈的上下文中初始化你的用戶界面,因爲我不不明白你對我的解釋。如果我找到一個解決方案,我會在這裏發佈。 謝謝! – user1930634

+0

真的嗎?我不認爲當你關閉它時,它隱藏了(setVisible(false)),所以你需要再次顯示它(setVisible(true)) – MadProgrammer