我有一个java应用程序在单独的窗口中打开文件,但菜单栏和框架标题不显示在第二个窗口中。正如您所看到的那样,文件在标题和菜单栏出现的第一个窗口上打开。当我通过单击第一个窗口上的菜单打开文件。该文件在第二个窗口打开,但没有框架标题和菜单栏。有人会告诉我如何解决这个问题吗?提前谢谢。
图像是在窗口
上打开的第一个文件图像是在单独窗口
上打开的第二个文件有我的代码:
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
}
}
}
答案 0 :(得分:1)
您似乎正在调用以File
作为参数的构造函数...
Question a=new Question(file);
但在该构造函数中没有任何地方设置框架的标题或设置其菜单栏。
答案 1 :(得分:0)
您正在呼叫的按钮操作列表器
Question a=new Question(file);
但是Jmenu项目是在没有参数的构造函数上编写的。所以改变你的construtors如下并尝试,它对我有用
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(""));
}