哪位高手能帮我修改一下以下这个程序,使它能打开文本型的文件,现在它能打开的是图片,谢谢了
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");
}
}
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");
}
}
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方法 所以只能显示文本的一行