[英]how to call jFrame on jDesktopPane without using JInternalFrame
[英]Reopen a JInternalFrame on JDesktopPane/JFrame
我在我的代碼中重新打開一個JInternalFrame時遇到問題。 我在MenuBar選項中選擇“ Cadastro”->“ Cadastro deVeí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);
}
}
請幫我! :)
您需要再次使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
類中令人擔憂。 您的JCadVeiculos
不應真正決定如何使用它,我個人不要以這種方式使組件公開可見,因為這可能會導致其他開發人員的濫用。
JDesktopPane
的管理職責在於JMain
類,它應確定將什么添加到JDesktopPane
以及何時添加。
只是說。
您可能還想看一看“ 初始線程” ,並確保從事件調度線程的上下文中初始化UI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.