-3

这是我删除的其他不需要的代码你现在可以运行吗???如果您能运行此程序并告诉我我错的部分,我将不胜感激。这是我的决赛项目我不知道我将如何做下一个和上一个按钮。所以我特别咨询了这个网站

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class dadas implements ActionListener {
static String summary,summary2,summary3;
JFrame frame=new JFrame("Generator X");
JPanel p = new JPanel();
JLabel studentno=new JLabel("Student No:"); 
JLabel name= new JLabel("First name:");
JLabel lastname= new JLabel("Last Name:");
JTextField number= new JTextField();
JTextField name2= new JTextField();
JTextField lastname2= new JTextField();
JButton next=new JButton("NEXT---->");
JButton prev=new JButton("<----PREV");
JButton add=new JButton("Add");
JButton save=new JButton("Save");
public static void main(String args[]) {
    new dadas();
}
public dadas(){
    frame.setSize(300,500);//frame
    frame.setLayout(null);
    frame.setVisible(true);
    frame.setResizable(false);

    studentno.setBounds(30,30,80,60);//LABEL
    number.setBounds(100,45,100,25);//TextField
    number.addActionListener(this);
    number.setDocument(new JTextFieldLimit(7));
    number.setEditable(false);
    name.setBounds(30,70,100,65);//LABEL
    name2.setBounds(100,90,100,28);//TextField
    name2.addActionListener(this);
    name2.setEditable(false);
    lastname.setBounds(30,115,100,65);//LABEL
    lastname2.setBounds(100,133,120,28);//TextField
    lastname2.addActionListener(this);
    lastname2.setEditable(false);
    next.setBounds(150,170,100,30);//Next Button
    next.addActionListener(this);
    prev.setBounds(50,170,100,30);//Previous Button
    prev.addActionListener(this);
    add.setBounds(50,210,100,30);
    add.addActionListener(this);
    save.setBounds(100,250,100,30);//Save Button
    save.addActionListener(this);
    frame.add(studentno);
    frame.add(name);
    frame.add(lastname);
    frame.add(number);
    frame.add(name2);
    frame.add(lastname2);
    frame.add(next);
    frame.add(prev);
    frame.add(add);
    frame.add(save);

}
public void actionPerformed(ActionEvent e){
if(e.getSource()==add){

number.setEditable(true);
name2.setEditable(true);
lastname2.setEditable(true); 
next.setEnabled(false);
prev.setEnabled(false);
}
else if( e.getSource()==save ){//save button

summary = (" "+number.getText()) ;
summary2 = ("" +name2.getText()) ; 
summary3 = (" "+lastname2.getText());
String I = dadas.summary;
String Love = dadas.summary2;
String You = dadas.summary3;
try {

BufferedWriter bw = new BufferedWriter(new FileWriter(newFile("C:\\lol.txt"),true));

bw.write(I);
bw.newLine();
bw.close();

JOptionPane.showMessageDialog(frame,"Your File has been Saved");
}
catch(IOException E)
{
}
}
else if(e.getSource()==next){//Next Button 
    try {
        String filePath = "C:\\lol.txt";
        BufferedReader br = new BufferedReader(new FileReader(filePath));//my file Writer
        String file;
        while((file = br.readLine()) != null){
            number.setText(file);
            lastname2.setText(file);
            name2.setText(file);
        }
        br.close();  
    }catch(IOException ex) {
        ex.printStackTrace(); 
     }
  }
}
}

在此处输入图像描述

4

1 回答 1

2

关于:

try
{
String filePath = "C:\\lol.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));
String file;
while((file = br.readLine()) != null){
file = br.readLine();
number.setText(file);
lastname2.setText(file);
name2.setText(file);
br.close();

}
}catch(IOException F)
{
}
  • 您仍然忽略异常,这是您永远不应该做的事情。同样,至少打印一个堆栈跟踪。
  • 您在while 循环中从文件中读取两次。在 while 条件下只读取一次。
  • 每次尝试读取一行时,您都会关闭 BufferedReader 。只有在阅读完整个文件后才应关闭它。
  • 您的代码格式仍然很差,因此很难阅读。请付出一些努力,以免使您的代码比需要的更难阅读。这对你要求不高,真的。

IE,

BufferedReader br = null;
try {
  br = new BufferedReader(new FileReader(filePath));
  String filePath = "C:\\lol.txt";
  // String file; // bad variable name
  String line; // better name
  while((line = br.readLine()) != null){
    // file = br.readLine(); // Don't read twice

    // You'll probably want to split the line in order to extract info from it
    // not sure what your line looks like so I can't suggest a String to split on
    String[] tokens = line.split(...); // this needs to be fixed

    number.setText(tokens[0]);  // ? first token in line? not sure
    lastname2.setText(tokens[1]); // ? 2nd item in tokens array? not sure
    name2.setText(tokens[2]); // ? 3rd item in tokens array? not sure
    // br.close(); // ** no don't close the br here.
  }
}catch(IOException ex) {
  ex.printStackTrace(); // Always catch or throw your exceptions. Don't ignore them.
} finally {
  br.close();  // better to have this in a finally block actually
}
于 2013-03-16T14:36:52.063 回答