如何在下一个和上一个按钮中实施操作?

时间:2013-03-19 14:39:42

标签: java user-interface button

我有一个问题如何在next和previous中实现动作。我有一个可以在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

克里斯托弗

Pangilinan

1455122

克里斯

Pangil

1210176

克里斯

Pangilinan

12111

CS

CD

1210176

克里斯

1111111

克里斯

1 个答案:

答案 0 :(得分:1)

当我查看代码时,我发现了一些严重的问题!

当您按nextprev时。您是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()方法替换为我的方法即可。将update()方法中的readFile()替换为readFile(index),并将int index = 5替换为int 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概念并相信我,有很多事情要知道!