简体   繁体   English

在第二个窗口中打开JFrame丢失的菜单栏

[英]Open JFrame in second window lost menu bar

I have a java app to open the file in separate windows, but the menu bar and frame title doesn't show on the second windows. 我有一个Java应用程序可在单独的窗口中打开文件,但是菜单栏和框架标题未显示在第二个窗口中。 As you can see when the file is open on the first windows that the title and menu bar appear. 如您所见,在第一个窗口上打开文件时,将显示标题和菜单栏。 When I open the file by clicking the menu on the first windows. 当我通过单击第一个窗口上的菜单打开文件时。 The file opens on the second windows but no frame title and menu bar. 该文件在第二个窗口中打开,但没有框架标题和菜单栏。 Would someone tell me how I can solve this problem? 有人可以告诉我如何解决这个问题吗? Thank in advance. 预先感谢。

The image is the first file opening on the windows 该图像是在Windows上打开的第一个文件 在此处输入图片说明

The image is the second file opening on the separate window 该图像是在单独窗口中打开的第二个文件 在此处输入图片说明

There is my code: 有我的代码:

package PDFAnnotationPackage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.*;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import PDFAnnotationPackage.WindowMenu.ChildMenuItem;
import com.qoppa.pdf.IEmbeddedFile;
import com.qoppa.pdf.annotations.IAnnotSelectionListener;
import java.io.File;



public class Question extends JFrame implements ActionListener{

public final static  String PDFEXTENSION= "pdf";
private ArrayList<File> fileList=new  ArrayList<File>();
private PDFInternalFrame  internalFrame=null;
private ArrayList<PDFInternalFrame>  lstInternalFrame=new ArrayList<PDFInternalFrame>();


public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length>0){
        Utility.DisplayTestMsg(args[0]);

    }

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run()
        {               
            new Question();              
        }
    });

}

public Question(File  newFile) {

    String extension=getExtension(newFile);
     if ( PDFEXTENSION.equalsIgnoreCase(extension)){
          AddJPDFNote(newFile);
     }   else{
                     Utility.DisplayWarningMsg("Only PDF File");
           }


    // TODO Auto-generated constructor stub
}
public  Question(){
    super("Question");

            //it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {                      
                }
            });


           this.setMinimumSize(new Dimension(400, 500));        
           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
           this.setExtendedState(JFrame.MAXIMIZED_BOTH);            
           this.setLayout(new BorderLayout());


            // Name the JMenu & Add Items
            JMenu menu = new JMenu("File");
            menu.add(makeMenuItem("Open"));
            menu.add(makeMenuItem("Save"));
            menu.add(makeMenuItem("Save As"));
            menu.add(makeMenuItem("Close"));
            menu.add(makeMenuItem("Print"));
            menu.add(makeMenuItem("Quit"));


            // Add JMenu bar
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);            
            setJMenuBar(menuBar);   
            setVisible(true);          

}
 public void actionPerformed(ActionEvent e) {

        // Menu item actions
        String command = e.getActionCommand().trim();  

        if (command.length()>1){
            menuAction(command);
        } 


    }
  private String getExtension(File file){
       return      file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
   }

private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(this);
    return m;
}

private void menuAction(String command){

        boolean blnGo=false;

             if (command.equals("Quit")) {

             } else if (command.equals("Open")) {
                 // Open menu item action           
                 OpenPDFFile();

             }  else if (command.equalsIgnoreCase("Save")) {


             } else if (command.equalsIgnoreCase("Save As")) {


             }else if (command.equalsIgnoreCase("Close")){

                 }

             else if (command.equalsIgnoreCase("Print")) {
             }

    }      



private void AddJPDFNote(File file){                
    try{                             

         internalFrame = new PDFInternalFrame(file, this.getWidth(), this.getHeight());
         fileList.add(file);
         lstInternalFrame.add(internalFrame);               
         internalFrame.setBounds(0, 0, 600, 100);       
         this.add(internalFrame,BorderLayout.CENTER);
         internalFrame.setVisible(true);

      try {

           internalFrame.setSelected(true);
       }
       catch (java.beans.PropertyVetoException e) {              
           Utility.DisplayExcecptionStrackTrack(e,"MainForm - AddJPDFNote line 401");

       }

       catch(Exception  err){
           Utility.DisplayExcecptionStrackTrack(err,"MainForm - AddJPDFNote line 406");
       }



    }
    catch(Exception  err){
        Utility.DisplayExcecptionStrackTrack(err,"MainForm - AddJPDFNote");


    }
}



private void  OpenPDFFile(){
      JFileChooser fileChooser = new JFileChooser();  
    FileNameExtensionFilter  pdfType=new FileNameExtensionFilter("PDF File (."+ PDFEXTENSION + ")", PDFEXTENSION);
    fileChooser.addChoosableFileFilter(pdfType);
    fileChooser.setFileFilter(pdfType);
  //clear "All files" from dropdown filter box
    fileChooser.setAcceptAllFileFilterUsed(false);

    int returnVal = fileChooser.showOpenDialog(Question.this);
    if (returnVal ==  fileChooser.APPROVE_OPTION) {

        File file = fileChooser.getSelectedFile();           
        String extension =getExtension(file);

        if ( PDFEXTENSION.equalsIgnoreCase(extension)){
          if (fileList.size()==0){

                      AddJPDFNote(file);
                  }
                  else{
                      Question a=new Question(file);
                      a.pack();
                      a.setVisible(true);
                  }
            } else{
             Utility.DisplayWarningMsg("Only PDF File");
            }

    } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
    // Do something else

    } 
  } 




}

It seems that you're calling the constructor that takes a File as an argument... 似乎您正在调用以File作为参数的构造函数...

Question a=new Question(file);

But nowhere in that constructor do you set the title of the frame or set its menu bar. 但是,在该构造函数中,您没有设置框架标题或设置其菜单栏的任何地方。

On button action listner you are calling 您正在呼叫按钮动作列表器

Question a=new Question(file);

but Jmenu items is written on constructor with no argument.so change your construtors as below and try,it worked for me 但是Jmenu项目是在构造函数上写的,没有参数。所以按如下所示更改您的构造函数并尝试,它对我有用

    public Question(File  newFile) {

    String extension=getExtension(newFile);
    if ( PDFEXTENSION.equalsIgnoreCase(extension)){
        AddJPDFNote(newFile);
    }   else{
       // Utility.DisplayWarningMsg("Only PDF File");
    }
    //it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
        }
    });


    this.setMinimumSize(new Dimension(400, 500));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setLayout(new BorderLayout());


    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Save As"));
    menu.add(makeMenuItem("Close"));
    menu.add(makeMenuItem("Print"));
    menu.add(makeMenuItem("Quit"));


    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    setJMenuBar(menuBar);
    setVisible(true);

    // TODO Auto-generated constructor stub
}
public  Question(){
    //super("Question");
    this(new File(""));
} 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM