gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:19:53 25 4
gpt4 key购买 nike

我有一个 Java 应用程序可以在单独的窗口中打开文件,但菜单栏和框架标题不会显示在第二个窗口上。正如您所看到的,当文件在第一个窗口中打开时,会出现标题和菜单栏。当我通过单击第一个窗口上的菜单打开文件时。该文件在第二个窗口上打开,但没有框架标题和菜单栏。有人会告诉我如何解决这个问题吗?提前致谢。

该图像是在Windows上打开的第一个文件enter image description here

该图像是在单独窗口中打开的第二个文件 enter image description here

这是我的代码:

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

}
}




}

最佳答案

您似乎正在调用以 File 作为参数的构造函数...

Question a=new Question(file);

但是在该构造函数中没有任何地方可以设置框架的标题或设置其菜单栏。

关于java - 在第二个窗口中打开 JFrame 丢失菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537411/

25 4 0