我把一个show 放到后面的主程序里时却什么都没有,真是奇怪

解决方案 »

  1.   

    to super_zzw:
    这个语句不行呀,编译都通不过
      

  2.   

    to super_zzw:
    这个语句不行呀,编译都通不过你的SET CLASSPATH=什么啊
      

  3.   

    这不是死机,是你的程序有问题。
    你不给每个按钮加Action,它们怎么会有反应呢?在textedit()中加这句看看:
    exitItem.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
                             System.exit(0);
                      }
                  });然后运行,再点击File中的Exit按钮,程序就会退出。其他按钮同样要加不同的Action。
      

  4.   

    我改成下面这样的还是一样:
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import java.awt.datatransfer.*;
    import java.io.*;public class textedit extends Frame implements ActionListener{
        private TextArea textArea =new TextArea();
        private MenuBar menuBar =new MenuBar();
        private Menu  fileMenu =new Menu("file");
        private MenuItem newItem=new MenuItem("New");
        private MenuItem openItem=new MenuItem("Open");
        private MenuItem saveItem=new MenuItem("Save");
        private MenuItem saveAsItem=new MenuItem("Save As");
        private MenuItem exitItem=new MenuItem("Exit");
        private Menu editMenu=new Menu("Edit");
        private MenuItem selectItem=new MenuItem("Select All");
        private MenuItem copyItem=new MenuItem("Copy");
        private MenuItem cutItem=new MenuItem("Cut");
        private MenuItem pastItem=new MenuItem("Paste");
        String fileName=null;
        Clipboard sysClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
    private FileDialog openFileDialog=new FileDialog(this,"open file",FileDialog.LOAD);
    private FileDialog saveAsFileDialog=new FileDialog(this,"save as",FileDialog.SAVE);
    public textedit(){
    setTitle("Text Editor");
    setBackground(Color.white);
    setSize(400,300);
    //show();
    fileMenu.add(newItem);
    fileMenu.add(openItem);
    fileMenu.add(saveItem);
    fileMenu.add(saveAsItem);
    fileMenu.addSeparator();
    fileMenu.add(exitItem);
    editMenu.add(selectItem);
    editMenu.addSeparator();
    editMenu.add(copyItem);
    editMenu.add(cutItem);
    editMenu.add(pastItem);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setMenuBar(menuBar);
    add(textArea);

    addWindowListener(new WindowAdapter(){
    public void windowClosing (WindowEvent e){
    System.exit(0);
    }
    });
    newItem.addActionListener(this);
    openItem.addActionListener(this);
    saveItem.addActionListener(this);
    saveAsItem.addActionListener(this);
    exitItem.addActionListener(this);
    selectItem.addActionListener(this);
    copyItem.addActionListener(this);
    cutItem.addActionListener(this);
    pastItem.addActionListener(this);


    }
     public void actionPerformed (ActionEvent e){
      Object eventSource =e.getSource();
      if(eventSource==newItem){
      textArea.setText("");
      }else if(eventSource==openItem){
      openFileDialog.show();
      fileName=openFileDialog.getDirectory()+openFileDialog.getFile();
      if(fileName!=null)
      readFile(fileName);
         }
         else if(eventSource==saveItem){
             writeFile(fileName);}
             else if(eventSource==saveAsItem)
             { saveAsFileDialog.show();
               fileName=saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
              if (fileName!=null)
              writeFile(fileName);
              }else if(eventSource==exitItem){
               System.exit(0);}
             if (eventSource==selectItem){
              textArea.selectAll();
             }
             if(eventSource==copyItem){
              String text=textArea.getSelectedText();
              StringSelection selection=new StringSelection(text);
              sysClipboard.setContents(selection,null);
             }
             if (eventSource==cutItem){
              String text=textArea.getSelectedText();
              StringSelection  selection=new StringSelection(text);
              sysClipboard.setContents(selection,null);
              textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
             }
              
            }
         
        public void readFile(String fileName){
        try{
          File file = new File(fileName);
          FileReader readIn = new FileReader(file);
          int size = (int)file.length();
          int charsRead = 0;
          char[] content = new char[size];
          while(readIn.ready())
           charsRead += readIn.read(content, charsRead, size - charsRead);
         readIn.close();
          textArea.setText(new String(content, 0, charsRead));
       }
        catch(IOException e){
          errorWarning("Error opening file");
        }
      }  public void writeFile(String fileName){
       try{
          File file = new File (fileName);
          FileWriter writeOut = new FileWriter(file);
          writeOut.write(textArea.getText());
          writeOut.close();
        }
        catch(IOException e){
          errorWarning("Error writing file");
        }
      }
      public void errorWarning(String err){
        ErrorWarning warn = new ErrorWarning(this,err);
        warn.show();
      }
      public void paint (Graphics g){
       g.drawRect(0,0,size().width-1,size().height-1);
       //g.drawLine(0,0,0,size().height);
      }
     public static void main(String[] args)
     {
       Frame edit =new textedit();
       edit.show();
       }
       }
      

  5.   

    ErrorWarning类你定义了吗?
    去掉和ErrorWarning有关的方法后,我这里完全可以正常运行。
    我装的是j2sdk1.4.1_02。顺便说一句,paint方法中的size()现在已经不建议使用,最好改为getSize()。
      

  6.   

    好像第一个程序是正确的。  但是,有些建议:
    1.类名textedit改为TextEdit;(规范,当然你的可以)
    2.import java.awt.Graphics可以不要;(你已经import java.awt.*把他给导入了)
    3.show()写到构造器的最后一行或如第2个一样,创建窗体后再调用;(当然,你那样并没错);
    4.setTitle("Text Editor")为何不直接在构造器中写super("Text Editor")呢?(当然,那样也行);最后,个人再说一次,认为你的程序并没有错,请确认你是编译后运行吗?
      

  7.   

    不好意思,你是说没有相应消息啊!  看错了,以为你说没看到界面出现。第一个应该像第二个一样,申明自己处理菜单消息,才能自己响应菜单的操作。
    newItem.addActionListener(this);
    openItem.addActionListener(this);
    saveItem.addActionListener(this);
    saveAsItem.addActionListener(this);
    exitItem.addActionListener(this);
    selectItem.addActionListener(this);
    copyItem.addActionListener(this);
    cutItem.addActionListener(this);
    pastItem.addActionListener(this);你的菜单不能出来就是因为你是show了后才去设置menuBar和添加组件到窗体,他们并没有被布局管理器布局,所以,你没有得到相应的效果。
    参考一下建议,就可以响应了。  至于消息事件的处理我想你好像明白,我就不多说了。
      

  8.   

    这就奇怪了,我这里还是看不到任何结果, 另外顺便问一下: 可不可以直接在jcreator
    里运行呀, 我每次compile时都出现无效路径
      

  9.   

    to Passants(路过) :
      我现在的问题是那个菜单根本就没出来,连窗体的影子都没见到
    to Iforgot(清风雨):
      谢谢你的建议,只是问题总是解决不了,你帮我运行了代码吗 ,
    还会有其它方面的原因吗
      

  10.   

    不行的话在dos下看看,如果通过会有显示的。
      

  11.   

    请问super_zzw(之支吾): 为什么要这样写,这是什么意思,而且我编译器说不能识别它呀
      

  12.   

    getContentPane() 是JFrame中添加组件时,必须加到内容面板中,而不是直接add。你用的是Frame,所以不需要getContentPane()。你的JDK没有问题的,肯定。我计算机上没有开发环境JDK,所以不能帮你调试。你是只菜单没响应吧? 那么你可以先只处理一个菜单项的消息。
    你还是说窗体的关闭按钮没用?在构造器里加入addWindowListener(new WindowAdapter() {
    void windowClosing(WindowEvent e) {
    System.exit(0);
    }})
      

  13.   

    我现在把其它的都去掉,发现可能是setMenuBar(menuBar)的问题.
    因为我加上这句就死了, 去掉它至少还可以看到一个白色的窗体, 为什么
    这里会有错呢
      

  14.   

    以下使帮你简单盖了一下的代码:
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.io.*;
    import javax.swing.*;public class TextEdit extends Frame implements ActionListener{
        private TextArea textArea =new TextArea();
        private MenuBar menuBar =new MenuBar();
        private Menu  fileMenu =new Menu("file");
        private MenuItem newItem=new MenuItem("New");
        private MenuItem openItem=new MenuItem("Open");
        private MenuItem saveItem=new MenuItem("Save");
        private MenuItem saveAsItem=new MenuItem("Save As");
        private MenuItem exitItem=new MenuItem("Exit");
        private Menu editMenu=new Menu("Edit");
        private MenuItem selectItem=new MenuItem("Select All");
        private MenuItem copyItem=new MenuItem("Copy");
        private MenuItem cutItem=new MenuItem("Cut");
        private MenuItem pastItem=new MenuItem("Paste");
        String fileName=null;
        Clipboard sysClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
    private FileDialog openFileDialog=new FileDialog(this,"open file",FileDialog.LOAD);
    private FileDialog saveAsFileDialog=new FileDialog(this,"save as",FileDialog.SAVE);
    public TextEdit(){
    setTitle("Text Editor");
    setBackground(Color.white);
    setSize(400,300);
    //show();
    fileMenu.add(newItem);
    fileMenu.add(openItem);
    fileMenu.add(saveItem);
    fileMenu.add(saveAsItem);
    fileMenu.addSeparator();
    fileMenu.add(exitItem);
    editMenu.add(selectItem);
    editMenu.addSeparator();
    editMenu.add(copyItem);
    editMenu.add(cutItem);
    editMenu.add(pastItem);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setMenuBar(menuBar);
    add(textArea);

    addWindowListener(new WindowAdapter(){
    public void windowClosing (WindowEvent e){
    System.exit(0);
    }
    });
    newItem.addActionListener(this);
    openItem.addActionListener(this);
    saveItem.addActionListener(this);
    saveAsItem.addActionListener(this);
    exitItem.addActionListener(this);
    selectItem.addActionListener(this);
    copyItem.addActionListener(this);
    cutItem.addActionListener(this);
    pastItem.addActionListener(this);


    }
     public void actionPerformed (ActionEvent e){
      Object eventSource =e.getSource();
      if(eventSource==newItem){
      textArea.setText("");
      }else if(eventSource==openItem){
      openFileDialog.show();
      fileName=openFileDialog.getDirectory()+openFileDialog.getFile();
      if(fileName!=null)
      readFile(fileName);
         }
         else if(eventSource==saveItem){
             writeFile(fileName);}
             else if(eventSource==saveAsItem)
             { saveAsFileDialog.show();
               fileName=saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
              if (fileName!=null)
              writeFile(fileName);
              }else if(eventSource==exitItem){
               System.exit(0);}
             if (eventSource==selectItem){
              textArea.selectAll();
             }
             if(eventSource==copyItem){
              String text=textArea.getSelectedText();
              StringSelection selection=new StringSelection(text);
              sysClipboard.setContents(selection,null);
             }
             if (eventSource==cutItem){
              String text=textArea.getSelectedText();
              StringSelection  selection=new StringSelection(text);
              sysClipboard.setContents(selection,null);
              textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
             }
              
            }
         
        public void readFile(String fileName){
        try{
          File file = new File(fileName);
          FileReader readIn = new FileReader(file);
          int size = (int)file.length();
          int charsRead = 0;
          char[] content = new char[size];
          while(readIn.ready())
           charsRead += readIn.read(content, charsRead, size - charsRead);
         readIn.close();
          textArea.setText(new String(content, 0, charsRead));
       }
        catch(IOException e){
          errorWarning("Error opening file");
        }
      }  public void writeFile(String fileName){
       try{
          File file = new File (fileName);
          FileWriter writeOut = new FileWriter(file);
          writeOut.write(textArea.getText());
          writeOut.close();
        }
        catch(IOException e){
          errorWarning("Error writing file");
        }
      }
      public void errorWarning(String err){
        //ErrorWarning warn = new ErrorWarning(this,err);
        //warn.show();
        JOptionPane.showConfirmDialog(null,err);
      }
      public void paint (Graphics g){
       g.drawRect(0,0,getSize().width-1,getSize().height-1);
       //g.drawLine(0,0,0,size().height);
      }
     public static void main(String[] args)
     {
       Frame edit =new TextEdit();
       edit.show();
     }
    }保存为TextEdit.java
    然后到该文件目录,运行:javac TextEdit.java
    编译好以后,再运行:java TextEdit在我的机器上是完全可行的。
    WinXP+j2sdk1.4.1_02
      

  15.   

    还是一样的不行呀,我的是Win2k advanced server+j2sdk1.4.1_02