我做了一个小的记事本演示,我试图在主框架中的相同框架中打开文件但是在打开文件时它在不同的框架中打开,我希望它在我运行时打开的第一帧中打开程序
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class DemoMenu extends Frame
{
Frame frm=new Frame();
MenuBar bar=new MenuBar();
//frm.setMenuBar(bar);
Menu mf=new Menu("File");
Menu me=new Menu("Edit");
Menu mfo=new Menu("Format");
Menu mv=new Menu("View");
Menu mh=new Menu("Help");
MenuItem mf1=new MenuItem("New"+" "+"Ctrl+N");
MenuItem mf2=new MenuItem("Open..."+" "+"Ctrl+O");
MenuItem mf3=new MenuItem("Save"+" "+"Ctrl+S");
MenuItem mf4=new MenuItem("Save As...");
MenuItem mf5=new MenuItem("Page Setup...");
MenuItem mf6=new MenuItem("Print..."+" "+"Ctrl+P");
MenuItem mf7=new MenuItem("Exit");
MenuItem me1=new MenuItem("Undo"+" "+"Ctrl+Z");
MenuItem me2=new MenuItem("Cut"+" "+"Ctrl+X");
MenuItem me3=new MenuItem("Copy"+" "+"Ctrl+C");
MenuItem me4=new MenuItem("Paste"+" "+"Ctrl+V");
MenuItem me5=new MenuItem("Delete"+" "+"Del" );
MenuItem me6=new MenuItem("Find..."+" "+"Ctrl+F");
MenuItem me7=new MenuItem("Find Next"+" "+"F3");
MenuItem me8=new MenuItem("Replace..."+" "+"Ctrl+H");
MenuItem me9=new MenuItem("Go To"+" "+"Ctrl+G");
MenuItem me10=new MenuItem("Select All"+" "+"Ctrl+A");
MenuItem me11=new MenuItem("Date/Time"+" "+"F5");
MenuItem mfo1=new MenuItem("Word Wrap");
MenuItem mfo2=new MenuItem("Font...");
MenuItem mv1=new MenuItem("Status Bar");
MenuItem mh1=new MenuItem("Help Topics");
MenuItem mh2=new MenuItem("About Notepad");
public static void main(String args[])
{
DemoMenu dm=new DemoMenu();
}
public DemoMenu()
{
super("United- Notepad");
setMenuBar(bar);
bar.add(mf);
bar.add(me);
bar.add(mfo);
bar.add(mv);
bar.add(mh);
mf.add(mf1);
mf.add(mf2);
mf.add(mf3);
mf.add(mf4);
mf.addSeparator();
mf.add(mf5);
mf.add(mf6);
mf.addSeparator();
mf.add(mf7);
mf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane smd=new JOptionPane();
smd.showMessageDialog(null,"Press Ok to open new page");
}
});
final JTextArea jta=new JTextArea(10,20);
JScrollPane scrl=new JScrollPane(jta);
frm.add(scrl);
mf2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
int selection=chooser.showOpenDialog(frm);
if(selection==JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null,"so u want to open the file...");
frm.setSize(520,540);
frm.setVisible(true);
File file = chooser.getSelectedFile();
FileInputStream fis=null;
try
{
fis=new FileInputStream(file);
byte b[]=new byte[fis.available()];
fis.read(b);
String str=new String(b);
jta.setText(str);
}
catch(Exception f)
{
f.printStackTrace();
}
}
}
});
mf3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
chooser.showSaveDialog(null);
}
});
mf4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser=new JFileChooser();
chooser.showDialog(null,"Save As");
}
});
mf7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane scd=new JOptionPane();
scd.showConfirmDialog(null,"Are you want to exit");
}
});
me.add(me1);
me.addSeparator();
me.add(me2);
me.add(me3);
me.add(me4);
me.add(me5);
me.addSeparator();
me.add(me6);
me.add(me7);
me.add(me8);
me.add(me9);
me.addSeparator();
me.add(me10);
me.add(me11);
me11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date d1=new Date();
JOptionPane smddate=new JOptionPane();
smddate.showMessageDialog(null,d1);
}
});
mfo.add(mfo1);
mfo.add(mfo2);
mv.add(mv1);
mh.add(mh1);
mh.addSeparator();
mh.add(mh2);
setSize(520,540);
setVisible(true);
}
}
答案 0 :(得分:2)
好的,让我们从这里开始......
public class DemoMenu extends Frame
{
Frame frm=new Frame();
您扩展Frame
,但之后又创建了对另一个Frame
的另一个引用。
而不是使用DemoMenu
,您似乎正在使用此“其他”frm
代替...
你也在混合重型和轻量级组件(AWT / Swing),这永远不会结束。而不是
Frame
,使用JFrame
Menu
,使用JMenu
MenuItem
,使用JMenuItem
这让我向您介绍setDefaultCloseOperation
,它允许您定义当用户尝试通过“x”按钮关闭窗口时应该采取的操作......
如果您打开一个流,则负责关闭它(请参阅文件打开方法)。
这应该使您的代码更像......
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DemoMenu extends JFrame {
// Frame frm = new Frame();
JMenuBar bar = new JMenuBar();
//frm.setJMenuBar(bar);
JMenu mf = new JMenu("File");
JMenu me = new JMenu("Edit");
JMenu mfo = new JMenu("Format");
JMenu mv = new JMenu("View");
JMenu mh = new JMenu("Help");
JMenuItem mf1 = new JMenuItem("New" + " " + "Ctrl+N");
JMenuItem mf2 = new JMenuItem("Open..." + " " + "Ctrl+O");
JMenuItem mf3 = new JMenuItem("Save" + " " + "Ctrl+S");
JMenuItem mf4 = new JMenuItem("Save As...");
JMenuItem mf5 = new JMenuItem("Page Setup...");
JMenuItem mf6 = new JMenuItem("Print..." + " " + "Ctrl+P");
JMenuItem mf7 = new JMenuItem("Exit");
JMenuItem me1 = new JMenuItem("Undo" + " " + "Ctrl+Z");
JMenuItem me2 = new JMenuItem("Cut" + " " + "Ctrl+X");
JMenuItem me3 = new JMenuItem("Copy" + " " + "Ctrl+C");
JMenuItem me4 = new JMenuItem("Paste" + " " + "Ctrl+V");
JMenuItem me5 = new JMenuItem("Delete" + " " + "Del");
JMenuItem me6 = new JMenuItem("Find..." + " " + "Ctrl+F");
JMenuItem me7 = new JMenuItem("Find Next" + " " + "F3");
JMenuItem me8 = new JMenuItem("Replace..." + " " + "Ctrl+H");
JMenuItem me9 = new JMenuItem("Go To" + " " + "Ctrl+G");
JMenuItem me10 = new JMenuItem("Select All" + " " + "Ctrl+A");
JMenuItem me11 = new JMenuItem("Date/Time" + " " + "F5");
JMenuItem mfo1 = new JMenuItem("Word Wrap");
JMenuItem mfo2 = new JMenuItem("Font...");
JMenuItem mv1 = new JMenuItem("Status Bar");
JMenuItem mh1 = new JMenuItem("Help Topics");
JMenuItem mh2 = new JMenuItem("About Notepad");
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
DemoMenu dm = new DemoMenu();
}
});
}
public DemoMenu() {
super("United- Notepad");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(bar);
bar.add(mf);
bar.add(me);
bar.add(mfo);
bar.add(mv);
bar.add(mh);
mf.add(mf1);
mf.add(mf2);
mf.add(mf3);
mf.add(mf4);
mf.addSeparator();
mf.add(mf5);
mf.add(mf6);
mf.addSeparator();
mf.add(mf7);
mf1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane smd = new JOptionPane();
smd.showMessageDialog(null, "Press Ok to open new page");
}
});
final JTextArea jta = new JTextArea(10, 20);
JScrollPane scrl = new JScrollPane(jta);
add(scrl);
mf2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int selection = chooser.showOpenDialog(DemoMenu.this);
if (selection == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "so u want to open the file...");
File file = chooser.getSelectedFile();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte b[] = new byte[fis.available()];
fis.read(b);
String str = new String(b);
jta.setText(str);
} catch (Exception f) {
f.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception exp) {
}
}
}
}
});
mf3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
}
});
mf4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showDialog(null, "Save As");
}
});
mf7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane scd = new JOptionPane();
scd.showConfirmDialog(null, "Are you want to exit");
}
});
me.add(me1);
me.addSeparator();
me.add(me2);
me.add(me3);
me.add(me4);
me.add(me5);
me.addSeparator();
me.add(me6);
me.add(me7);
me.add(me8);
me.add(me9);
me.addSeparator();
me.add(me10);
me.add(me11);
me11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date d1 = new Date();
JOptionPane smddate = new JOptionPane();
smddate.showMessageDialog(null, d1);
}
});
mfo.add(mfo1);
mfo.add(mfo2);
mv.add(mv1);
mh.add(mh1);
mh.addSeparator();
mh.add(mh2);
setSize(520, 540);
setVisible(true);
}
}
答案 1 :(得分:1)
这是因为您的类扩展了JFrame,并且从该JFrame创建了一个新的JFrame。
删除第frm=new Frame();
行
并使用this
代替frm