开发记事本一开始怎么这样了?
(https://img-ask.csdn.net/upload/201507/11/1436630076_786879.png)
问题的关键是你把选择文件对话框加到了你的JFrame中,这是不对的,注释掉就行了。改成下面的:
// 默认方式
jfc1.showOpenDialog(null);
// this.add(jfc1); //注释掉这一行即可。
JFileChooser本身的bug,把你的程序拿到windows 7上运行看看。
另外看看你的计算机是不是感染了360等病毒,它们会破坏和干扰系统的运行。
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();
}
}
}
}
}
这是代码,帮帮忙。
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();
}
}
}
}
}
这是代码,帮帮忙。