qq_37967303
qq_37967303
2017-03-19 02:00

java事件监听没反应,半天没找到原因

  • 半天没找到原因
  • java

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Txt extends JFrame implements ActionListener
{

JTextArea jta =null;
JMenuBar jmb =null;
JMenu jm =null;
JMenuItem jmi1 =null;
JMenuItem jmi2 =null;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Txt txt =new Txt();
}

public Txt()
{
     jta =new JTextArea();
     jmb  =new JMenuBar();
     jm   =new JMenu("文件(F)");
     jm.setMnemonic('F');//设置快捷要用大写

     jmi1 =new JMenuItem("打开(o)");
     jmi1.setMnemonic('O');
     jmi1.addActionListener(this);
     jmi1.setActionCommand("open");



     jmi2 =new JMenuItem("保存(s)");
     jmi2.setMnemonic('S');
     jmi2.addActionListener(this);
     jmi2.setActionCommand("save");

     this.setJMenuBar(jmb);
     jmb.add(jm);
     jm.add(jmi1);
     jm.add(jmi2);

     this.add(jta);

     this.setVisible(true);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setSize(400, 300);
}
@Override
public void actionPerformed(ActionEvent e) {

    if(e.getActionCommand().equals("opne"))
    {

        JFileChooser jfc =new JFileChooser();
        jfc.showOpenDialog(null);
        jfc.setVisible(true);

        String filename =jfc.getSelectedFile().getAbsolutePath();

        System.out.println(filename);

        FileReader fr=null;
        BufferedReader br=null;
        try {
            fr=new FileReader(filename);
            br=new BufferedReader(fr);

            String s ="";
            String alls ="";
            while((s=br.readLine())!=null)
            {
                alls+=s;
            }
            jta.setText(alls);

        } catch (Exception e2) {
            // TODO: handle exception
        }finally
        {
            try {
                br.close();
                fr.close();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

        }

    }/*else if(e.getActionCommand().equals("save"))
    {

    }*/
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换