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();
}
}
出错那行我标记了。
我初学者,这都是书上的代码,检查好几遍了。
和书上没有差错,但就是编译不了。
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();
}
}
if (result == JFileChooser.CANCEL_OPTION);
return ;
//这行,说是不可执行的代码
File fileName = fileChooser.getSelectedFile();if后面多了个分号。就是说if条件和后面的return无关
return绝对被执行到。都return了,后面的语句当然无法继续。so 编译失败