我写了一个程序,是单击按钮时弹出一个窗口,但是无法弹出
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class fileinputstream2 implements ActionListener
{
JFrame frame=new JFrame("读取文件");
JTextField text=new JTextField("fileinputstream.java");
JButton button=new JButton("读取");
JTextArea area=new JTextArea(80,70);
JFrame frame2=new JFrame("文件内容");
public fileinputstream2()
{
Container cp=frame.getContentPane();
Container cp2=frame2.getContentPane();
cp2.add(area,BorderLayout.CENTER);
cp.setLayout(new FlowLayout());
cp.add(button);
cp.add(text);
button.addActionListener(this);
frame.setBounds(300,300,200,200);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
fileinputstream2 diao=new fileinputstream2();
}
public void actionPerformed(ActionEvent e)
{
try{
File file=new File(text.getText());
FileInputStream input=new FileInputStream(file);
byte by[]=new byte[1000];
int b=input.read(by);
String s=new String(by,0,b,"Default");
area.setText(s);
frame2.setBounds(350,350,500,600);
frame2.setVisible(true);
frame2.pack();
}catch(IOException e2){area.setText("找不到文件");}
}
}
请问怎么修发这个程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class fileinputstream2 implements ActionListener
{
JFrame frame=new JFrame("读取文件");
JTextField text=new JTextField("fileinputstream.java");
JButton button=new JButton("读取");
JTextArea area=new JTextArea(80,70);
JFrame frame2=new JFrame("文件内容");
public fileinputstream2()
{
Container cp=frame.getContentPane();
Container cp2=frame2.getContentPane();
cp2.add(area,BorderLayout.CENTER);
cp.setLayout(new FlowLayout());
cp.add(button);
cp.add(text);
button.addActionListener(this);
frame.setBounds(300,300,200,200);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
fileinputstream2 diao=new fileinputstream2();
}
public void actionPerformed(ActionEvent e)
{
try{
File file=new File(text.getText());
FileInputStream input=new FileInputStream(file);
byte by[]=new byte[1000];
int b=input.read(by);
String s=new String(by,0,b,"Default");
area.setText(s);
frame2.setBounds(350,350,500,600);
frame2.setVisible(true);
frame2.pack();
}catch(IOException e2){area.setText("找不到文件");}
}
}
请问怎么修发这个程序
IO异常找不到文件,被捕获后,下面的代码没有执行了。
public void actionPerformed(ActionEvent e)
{
try{
File file=new File(text.getText());
FileInputStream input=new FileInputStream(file);
byte by[]=new byte[1000];
int b=input.read(by);
}catch(IOException e2){
area.setText("找不到文件");
}
String s=new String(by,0,b,"Default");
area.setText(s);
frame2.setBounds(350,350,500,600);
frame2.setVisible(true);
frame2.pack(); }