Java 在第二个窗口的菜单栏中打开JFrame
我有一个java应用程序可以在单独的窗口中打开文件,但是菜单栏和框架标题不会显示在第二个窗口中。正如您在标题和菜单栏出现的第一个窗口中打开文件时所看到的那样。当我通过单击第一个窗口上的菜单打开文件时。文件在第二个窗口打开,但没有框架标题和菜单栏。有人能告诉我如何解决这个问题吗?先谢谢你 图像是在windows上打开的第一个文件 该图像是在单独窗口上打开的第二个文件 这是我的代码:Java 在第二个窗口的菜单栏中打开JFrame,java,swing,Java,Swing,我有一个java应用程序可以在单独的窗口中打开文件,但是菜单栏和框架标题不会显示在第二个窗口中。正如您在标题和菜单栏出现的第一个窗口中打开文件时所看到的那样。当我通过单击第一个窗口上的菜单打开文件时。文件在第二个窗口打开,但没有框架标题和菜单栏。有人能告诉我如何解决这个问题吗?先谢谢你 图像是在windows上打开的第一个文件 该图像是在单独窗口上打开的第二个文件 这是我的代码: package PDFAnnotationPackage; import java.awt.event.Actio
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
}
}
}
package-pdfantotationpackage;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFileChooser;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
导入java.awt.BorderLayout;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.filechooser.*;
导入java.util.*;
导入javax.swing.JButton;
导入java.awt.Color;
导入javax.swing.JTextField;
导入PDFAnnotationPackage.WindowMenu.ChildMenuItem;
导入com.qoppa.pdf.IEmbeddedFile;
导入com.qoppa.pdf.annotations.IAnnotSelectionListener;
导入java.io.File;
公共类问题扩展JFrame实现ActionListener{
公共最终静态字符串PDFEXTENSION=“pdf”;
private ArrayList fileList=new ArrayList();
私有PDFInternalFrame internalFrame=null;
private ArrayList lsternalframe=new ArrayList();
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
如果(参数长度>0){
DisplayTestMsg(args[0]);
}
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐
{
新问题();
}
});
}
公共问题(文件newFile){
字符串扩展名=getExtension(新文件);
if(PDFEXTENSION.equalsIgnoreCase(扩展)){
AddJPDFNote(新文件);
}否则{
DisplayWarningMsg(“仅PDF文件”);
}
//TODO自动生成的构造函数存根
}
公众问题({
超级(“问题”);
//它等于此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(窗口事件e){
}
});
此设置最小尺寸(新尺寸(400500));
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_两者);
此.setLayout(新的BorderLayout());
//命名JMenu&添加项
JMenu菜单=新JMenu(“文件”);
添加(makeMenuItem(“打开”);
添加(makeMenuItem(“保存”);
添加(makeMenuItem(“另存为”);
添加(makeMenuItem(“关闭”);
添加(makeMenuItem(“打印”);
添加(makeMenuItem(“退出”);
//添加JMenu栏
JMenuBar menuBar=新的JMenuBar();
菜单栏。添加(菜单);
setJMenuBar(菜单栏);
setVisible(真);
}
已执行的公共无效操作(操作事件e){
//菜单项操作
String command=e.getActionCommand().trim();
if(command.length()>1){
菜单操作(命令);
}
}
私有字符串getExtension(文件){
返回file.getName().substring(file.getName().lastIndexOf(“.”+1,file.getName().length());
}
私有JMenuItem makeMenuItem(字符串名称){
JMenuItem m=新的JMenuItem(名称);
m、 addActionListener(此);
返回m;
}
私有void菜单操作(字符串命令){
布尔blnGo=false;
if(command.equals(“Quit”)){
}else if(command.equals(“Open”)){
//打开菜单项操作
OpenPDFFile();
}else if(command.equalsIgnoreCase(“Save”)){
}else if(command.equalsIgnoreCase(“另存为”)){
}else if(command.equalsIgnoreCase(“Close”)){
}
else if(command.equalsIgnoreCase(“打印”)){
}
}
私有void AddJPDFNote(文件){
试试{
internalFrame=new PDFInternalFrame(文件,this.getWidth(),this.getHeight());
fileList.add(文件);
lsternalframe.add(internalFrame);
内部框架立根(0,0,600,100);
添加(internalFrame,BorderLayout.CENTER);
internalFrame.setVisible(true);
试一试{
internalFrame.setSelected(true);
}
catch(java.beans.PropertyVetoException){
Utility.displayExceptionStrackTrack(e,“MainForm-AddJPDFNote行401”);
}
捕获(异常错误){
Utility.displayExceptionStrackTrack(err,“MainForm-AddJPDFNote第406行”);
}
}
捕获(异常错误){
displayExceptionStrackTrack(err,“MainForm-AddJPDFNote”);
}
}
私有void OpenPDFFile(){
JFileChooser fileChooser=新的JFileChooser();
FileNameExtensionFilter pdfType=新的FileNameExtensionFilter(“PDF文件(.“+PDFEXTENSION+”),PDFEXTENSION);
fileChooser.addChoosableFileFilter(pdfType);
setFileFilter(pdfType);
//从下拉筛选框中清除“所有文件”
fileChooser.SetAcceptableFileFilterUsed(false);
int returnVal=fileChooser.showOpenDialog(问题.this);
if(returnVal==fileChooser.APPROVE_选项){
File File=fileChooser.getSelectedFile();
字符串扩展名=getExtension(文件);
if(PDFEXTENSION.equalsIgnoreCase(扩展)){
if(fileList.size()==0){
AddJPDFNote(文件);
}
否则{
问题a=新问题(文件);
a、 包装();
Question a=new Question(file);
Question a=new Question(file);
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(""));
}