-2

我有一个问题如何在下一个和上一个中执行操作。我有一台扫描仪,可以读取我在 wew.txt 中的文件。为什么它不能读取我输入的第一个?我怎样才能转到我保存的下一个文件?

我的 wew.txt 在底部。我的代码如下:

static int index = 5;
public void openFile(){
try   {
x = new Scanner(new File("C:\\wew.txt"));
}
catch(Exception d){
}

}
public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();

number.setText(a);
name2.setText(b);
lastname2.setText(c);
    }
}

public void closeFile(){
x.close();
}
public void update(){
openFile();
readFile();
closeFile();
   }


public void actionPerformed(ActionEvent e){
if(e.getSource()==cancel){
number.setEditable(false);
name2.setEditable(false);
lastname2.setEditable(false);
delete.setEnabled(true);
next.setEnabled(true);
prev.setEnabled(true);
edit.setEnabled(true);
  }
if(e.getSource()==add){

number.setEditable(true);
name2.setEditable(true);
lastname2.setEditable(true); 
next.setEnabled(true);
prev.setEnabled(false);
edit.setEnabled(false);
delete.setEnabled(false);
number.setText("");
name2.setText("");
lastname2.setText("");

}
else if( e.getSource()==save ){//save button
summary = (" "+number.getText()) ;
summary2 = ("" +name2.getText()) ; 
summary3 = (" "+lastname2.getText());
String I = wew.summary;
String Love = wew.summary2;
String You = wew.summary3;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\wew.txt"),true));
bw.write(I);
bw.newLine();
bw.write(Love);
bw.newLine();
bw.write(You);
bw.newLine(); 
bw.close();
JOptionPane.showMessageDialog(frame,"Your File has been Saved");
}
catch(IOException E)
{
}
}
else if(e.getSource()==next){
if(index>=1)//Next Button 
index ++;
update();
System.out.println(index);

}

else if(e.getSource()==prev){
if(index<= 5)
{
index --;
update();
System.out.println(index);
}

}
else if(e.getSource()==exit)
{
JOptionPane.showConfirmDialog(frame,"Want To Exit?");
frame.dispose();
}
} 
public static void main(String args[]) {
new wew();
}
}

实际上我的 wew.txt 看起来像这样:

1111111

克里斯托弗

潘吉利南

1455122

克里斯

庞吉尔

1210176

克里斯

潘吉利南

12111

CS

光盘

1210176

克里斯

1111111

克里斯

4

1 回答 1

1

当我浏览你的代码时,我发现它有一些严重的问题!

当您按next或时prev。你是openning文件,然后是read三行,然后closing是文件。因为一直在做同样的事情而没有记录你想阅读的位置,所以基本上你看到的是相同的数据(第一个位置的数据)。

解决方案:

假设您需要读取第 i^th 个数据,因此您需要跳过(i-1)3行,然后为三个文本字段读取三行。这里给出了示例代码!

 public void readFile(int offset){
   //Scanner x = new Scanner(new File("C://wew.txt"));
    int i = 0;
    while (x.hasNext() && i <= offset ) {
        String a = x.next();
        String b = x.next();
        String c = x.next();
        System.out.println("offset: "+offset);
       if (i == offset) {
            number.setText(a);
            name2.setText(b);
            lastname2.setText(c);
         break;
        }
     i++;
    }
   // x.close();
}

只需将您的 readFile() 方法替换为我的方法即可。readFile()在您的 update() 方法readFile(index)中替换int index = 5int index = 0. 注释掉if (index >= 1)并替换if(index<= 5)if (index >= 0).

编辑:

这是整个代码!

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.*;

public class GuiMain implements ActionListener {
static String summary, summary2, summary3;
private Scanner x;
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");
JButton delete = new JButton("Delete");
JButton edit = new JButton("Edit");
JButton cancel = new JButton("Cancel");
JButton exit = new JButton("Exit");
JButton clear = new JButton("Clear");
static int index = -1;

public void openFile() {
    try {

      x = new Scanner(new File("C://wew.txt"));

    } catch (Exception d) {
    }

}

public void readFile(int offset) {

    int i = 0;
    while (x.hasNext() && i <= offset ) {
        String a = x.next();
        String b = x.next();
        String c = x.next();
        System.out.println("offset: "+offset);
       if (i == offset) {
            number.setText(a);
            name2.setText(b);
            lastname2.setText(c);
         break;
        }
     i++;
    }
}

public void closeFile() {
    x.close();
}

public void update() {
    openFile();
    readFile(index);
    closeFile();
}

public GuiMain() {

    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();
    //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);

    cancel.setBounds(50, 330, 200, 35);
    cancel.addActionListener(this);

    delete.setBounds(50, 290, 200, 35);
    delete.addActionListener(this);

    edit.setBounds(150, 210, 100, 30);
    edit.addActionListener(this);

    exit.setBounds(50, 370, 200, 35);
    exit.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);
    frame.add(cancel);
    frame.add(exit);
    frame.add(delete);
    frame.add(clear);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == cancel) {
        number.setEditable(false);
        name2.setEditable(false);
        lastname2.setEditable(false);
        delete.setEnabled(true);
        next.setEnabled(true);
        prev.setEnabled(true);
        edit.setEnabled(true);
    }
    if (e.getSource() == add) {

        number.setEditable(true);
        name2.setEditable(true);
        lastname2.setEditable(true);
        next.setEnabled(true);
        prev.setEnabled(false);
        edit.setEnabled(false);
        delete.setEnabled(false);
        number.setText("");
        name2.setText("");
        lastname2.setText("");

    } else if (e.getSource() == save) {//save button
        summary = (" " + number.getText());
        summary2 = ("" + name2.getText());
        summary3 = (" " + lastname2.getText());
        String I = GuiMain.summary;
        String Love = GuiMain.summary2;
        String You = GuiMain.summary3;
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C://wew.txt"), true));
            bw.write(I);
            bw.newLine();
            bw.write(Love);
            bw.newLine();
            bw.write(You);
            bw.newLine();
            bw.close();
            JOptionPane.showMessageDialog(frame, "Your File has been Saved");
        } catch (IOException E) {
        }
    } else if (e.getSource() == next) {
        //if (index >= 1)//Next Button
        index++;
        update();

       // System.out.println(index);

    } else if (e.getSource() == prev) {
        if (index >= 0) {
            index--;
            update();

           // System.out.println(index);
        }

    } else if (e.getSource() == exit) {
        JOptionPane.showConfirmDialog(frame, "Want To Exit?");
        frame.dispose();
    }
}

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

最后但是同样重要的。首先学习基本的Java概念,相信我,有很多东西要知道!

于 2013-03-19T15:36:03.073 回答