antiitna 2015-07-11 16:02 采纳率: 100%
浏览 2176
已采纳

JFileChooser打开文件怎么变这样?

开发记事本一开始怎么这样了?
(https://img-ask.csdn.net/upload/201507/11/1436630076_786879.png)

  • 写回答

4条回答 默认 最新

  • JonsonJiao 2015-07-12 01:43
    关注

    问题的关键是你把选择文件对话框加到了你的JFrame中,这是不对的,注释掉就行了。改成下面的:

    // 默认方式
                jfc1.showOpenDialog(null);
        //          this.add(jfc1); //注释掉这一行即可。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • threenewbee 2015-07-11 16:06
    关注

    JFileChooser本身的bug,把你的程序拿到windows 7上运行看看。
    另外看看你的计算机是不是感染了360等病毒,它们会破坏和干扰系统的运行。

    评论
  • antiitna 2015-07-11 17:23
    关注

    package com.notepad;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class notepad extends JFrame implements ActionListener{
    /**
    * @记事本开发界面和功能
    * 组件:JMenubar--->JMenu---->JMenuItem
    * 组件:JFileChooser
    */
    //定义组件
    JTextArea jta=null;
    //菜单栏组件
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenu jm2=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    notepad nt=new notepad();
    }
    //构造函数中添加组件
    public notepad(){
    //初始化组件
    jta=new JTextArea();
    jmb=new JMenuBar();
    jm1=new JMenu("文件");
    jmi1=new JMenuItem("打开");
    jmi2=new JMenuItem("保存");

        //监听
         jmi1.addActionListener(this);
        jmi1.setActionCommand("open");
        //助记符
        jm1.setMnemonic('F');
        //添加组件
            jmb.add(jm1);
            jm1.add(jmi1);
            jm1.add(jmi2);
            this.add(jta);
    
       //注意添加按钮组件的方式
            this.setJMenuBar(jmb);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(4000,3000);
            this.setVisible(true);
    }
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getActionCommand().equals("open")){
            System.out.println("aaa");
            //文件选择组件
            JFileChooser jfc1=new JFileChooser();
            //选择对话
            jfc1.setDialogTitle("请选择文件...");
            jfc1.setVisible(true);
            //默认方式
            jfc1.showOpenDialog(null);
            this.add(jfc1); 
            //获取文件路径
            String filename=jfc1.getSelectedFile().getAbsolutePath();
            System.out.println(filename);
            //打开文件显示到文本中
            FileReader fr=null;
            BufferedReader br=null;
            try {
                fr=new FileReader(filename);
                br=new BufferedReader(fr);
                String s="";
                String allCon="";
                while((s=br.readLine())!=null){
                    allCon+=s+"\r\n";
                }
                jta.setText(allCon);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    br.close();
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
            }
    }
    

    }

    
    

    这是代码,帮帮忙。

    评论
  • antiitna 2015-07-11 17:23
    关注

    package com.notepad;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class notepad extends JFrame implements ActionListener{
    /**
    * @记事本开发界面和功能
    * 组件:JMenubar--->JMenu---->JMenuItem
    * 组件:JFileChooser
    */
    //定义组件
    JTextArea jta=null;
    //菜单栏组件
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenu jm2=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    notepad nt=new notepad();
    }
    //构造函数中添加组件
    public notepad(){
    //初始化组件
    jta=new JTextArea();
    jmb=new JMenuBar();
    jm1=new JMenu("文件");
    jmi1=new JMenuItem("打开");
    jmi2=new JMenuItem("保存");

        //监听
         jmi1.addActionListener(this);
        jmi1.setActionCommand("open");
        //助记符
        jm1.setMnemonic('F');
        //添加组件
            jmb.add(jm1);
            jm1.add(jmi1);
            jm1.add(jmi2);
            this.add(jta);
    
       //注意添加按钮组件的方式
            this.setJMenuBar(jmb);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(4000,3000);
            this.setVisible(true);
    }
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getActionCommand().equals("open")){
            System.out.println("aaa");
            //文件选择组件
            JFileChooser jfc1=new JFileChooser();
            //选择对话
            jfc1.setDialogTitle("请选择文件...");
            jfc1.setVisible(true);
            //默认方式
            jfc1.showOpenDialog(null);
            this.add(jfc1); 
            //获取文件路径
            String filename=jfc1.getSelectedFile().getAbsolutePath();
            System.out.println(filename);
            //打开文件显示到文本中
            FileReader fr=null;
            BufferedReader br=null;
            try {
                fr=new FileReader(filename);
                br=new BufferedReader(fr);
                String s="";
                String allCon="";
                while((s=br.readLine())!=null){
                    allCon+=s+"\r\n";
                }
                jta.setText(allCon);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    br.close();
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
            }
    }
    

    }

    
    

    这是代码,帮帮忙。

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
  • ¥20 h3c静态路要求有详细过程