AccountRecord类和BankUI类都没错。
出错那行我标记了。
我初学者,这都是书上的代码,检查好几遍了。
和书上没有差错,但就是编译不了。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;public class CreateSequentialFile extends JFrame{

private BankUI userInterface;
private ObjectOutputStream out;
private JButton open,enter;

public CreateSequentialFile(){

super("祁超制作");

userInterface = new BankUI(4);
getContentPane().add(userInterface,BorderLayout.CENTER);
open = userInterface.getDoTask1Button();
open.setText("打开");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile();
}
}
);

enter = userInterface.getDoTask2Button();
enter.setText("添加");
enter.setEnabled(false);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addRecord();
}
}
);


addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if (out != null)
addRecord();
closeFile();
}
}
);

setSize(300,200);
setVisible(true);

}

private void openFile(){

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

int result = fileChooser.showSaveDialog(this);

if (result == JFileChooser.CANCEL_OPTION);
return ;
//这行,说是不可执行的代码
File fileName = fileChooser.getSelectedFile();

if (fileName == null ||fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "asdasd","sdasd",JOptionPane.ERROR_MESSAGE);
else{
try{
out = new ObjectOutputStream(new FileOutputStream(fileName));
open.setEnabled(false);
enter.setEnabled(true);
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "asdasd","sdasd",JOptionPane.ERROR_MESSAGE);
}
}


}

private void addRecord(){
AccountRecord account = new AccountRecord();
int accountNumber = 0;
String[] fieldValues = userInterface.getFieldValues();

if (!fieldValues[BankUI.ACCOUNT].equals(""))
try{
accountNumber = Integer.parseInt(fieldValues[BankUI.ACCOUNT]);

if (accountNumber > 0){
account = new AccountRecord(accountNumber,
fieldValues[BankUI.FIRSTNAME],
fieldValues[BankUI.LASTNAME],
Double.parseDouble(fieldValues[BankUI.BALANCE]));
}
out.writeObject(account);
out.flush();

userInterface.clearFields();
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(this, "数字问题","数字问题",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e){
closeFile();
}
}

private void closeFile(){
try{
out.close();
System.exit(0);
}
catch (IOException e){
JOptionPane.showMessageDialog(this,"出错","出错",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}

public static void main(String[] args){
new CreateSequentialFile();
}
}

解决方案 »

  1.   


    if (result == JFileChooser.CANCEL_OPTION);
    return ;
    //这行,说是不可执行的代码
    File fileName = fileChooser.getSelectedFile();if后面多了个分号。就是说if条件和后面的return无关
    return绝对被执行到。都return了,后面的语句当然无法继续。so 编译失败
      

  2.   

    顶 1楼,if后多个分号,那么return;语句就不属于if语句范围之内的,就是说,在你的程序中,那个return总是会被执行,故return语句之后所有的语句都是不可达的,建议楼主养成好习惯,不管if语句块中有多少条语句,都加上大括号,这样代码可读性更高,也不容易出错.
      

  3.   

    楼主太大意了··建议使用eclipse··这种错误就可以减少很多了··