如何制作一个简单的能打开文本的程序
哪位高手能帮我修改一下以下这个程序,使它能打开文本型的文件,现在它能打开的是图片,谢谢了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame{
JMenuBar bar = new JMenuBar();
JMenu fileMenu = new JMenu();
JMenuItem openItem = new JMenuItem();
JLabel imageLabel = new JLabel();
public MyFrame(){
super();
init();
}
protected void init(){
this.setJMenuBar(bar);
fileMenu.setText("File");
bar.add(fileMenu);
openItem.setText("Open");
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile(e);
}
});
fileMenu.add(openItem);
this.getContentPane().add(imageLabel,BorderLayout.CENTER);
}
protected void openFile(ActionEvent e){
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
ImageIcon icon = new ImageIcon(chooser.getSelectedFile()
.getAbsolutePath());
imageLabel.setIcon(icon);
}
}
}
class Test{
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(300,300);
frame.setTitle("My Frame");
}
}
问题点数:20、回复次数:5Top
1 楼haisenmai(我应该做得到)回复于 2006-12-01 14:37:21 得分 0
package LastMonth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class MyFrame extends JFrame{
JMenuBar bar = new JMenuBar();
JMenu fileMenu = new JMenu();
JMenuItem openItem = new JMenuItem();
JLabel imageLabel = new JLabel();
public MyFrame(){
super();
init();
}
protected void init(){
this.setJMenuBar(bar);
fileMenu.setText("File");
bar.add(fileMenu);
fileMenu.add(openItem);
openItem.setText("Open");
this.validate();
this.repaint();
this.pack();
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile(e);
}
});
this.getContentPane().add(imageLabel,BorderLayout.CENTER);
}
protected void openFile(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
String local = chooser.getSelectedFile().toString().replace("\\","/");
System.out.println("dasdsa"+local.substring(local.length()-3,local.length()));
if(local.substring(local.length()-3,local.length()).equals("txt")){
File f = new File(local);
FileReader fr;
try {
fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String record =new String();
while((record = br.readLine())!=null){
System.out.println("record "+record+local);
imageLabel.setText(record);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{
ImageIcon icon = new ImageIcon(chooser.getSelectedFile()
.getAbsolutePath());
imageLabel.setIcon(icon);
}
}
}
}
class OpenFile{
public static void main(String[] args){
MyFrame frame = new MyFrame();
frame.validate();frame.repaint();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(300,300);
frame.setTitle("My Frame");
}
}
//JLABLE不象textarea 支持APPEND方法 所以只能显示文本的一行Top
2 楼yslf8517()回复于 2006-12-01 19:40:40 得分 0
JDK文档里面细节都有的
Top
3 楼teminator147511682()回复于 2006-12-01 20:21:56 得分 0
我的意思是要显示在刚打开的那个窗体里面,不是命令提示符那个窗口,而且要能显示全部的文本。Top
4 楼armylau(冯.城.褐.兰)回复于 2006-12-02 00:53:35 得分 0
参见JDK里的Demo: 如 C:\jdk1.5.0_09\demo\jfc\Notepad 就是一个很好的例子. 比楼上稍好一点之处在于读取和保存文件时使用了线程.Top
5 楼ly342540479(心不在跳了, 脸也不红了)回复于 2006-12-02 08:25:26 得分 0
直接调用打开系统的记事本的函数不就可以了
Top




