缓冲读取器加载GUI

时间:2017-02-06 12:45:51

标签: java user-interface jframe actionlistener bufferedreader

我已经使用GUI来获取输入并保存它。我加载输入时遇到麻烦,我试图用缓冲的读卡器制作它,我被困在这里。

不要介意注释的代码区域:)

我需要有关load方法的帮助,因为我需要从数组中输入它以添加输入并使用prev<和下一个>。

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;


//begining of a class that will implement Action listener
public class Orders extends JFrame implements ActionListener {
 //instance variables, text field and buttons
 private int index;
 JFrame jf;

 JTextField jtfItemName;
 JTextField jtfNumberOf;
 JTextField jtfCost;
 JTextField jtfAmountOwned;

 JButton jbCalculate;
 JButton jbSave;
 JButton jbClear;
 JButton jbExit;

 double calculate;

 JButton jbLoad;
 JButton jbPrev;
 JButton jbNext;

 String itemName;
 String numberOf;
 String cost;
 String amountOwned;

 ArrayList < String > lines;
 String split = ", ";

 //gui implementation

 public Orders() {
  index = 0;
  lines = new ArrayList < > ();
  jf = new JFrame(" Orders Calculator");
  //pannels
  JPanel pnl = new JPanel();
  pnl.setLayout(new GridLayout(0, 2));
  JLabel jlFirst = new JLabel("Item name :");
  jlFirst.setHorizontalAlignment(JLabel.RIGHT);
  jtfItemName = new JTextField(10);
  pnl.add(jlFirst);
  pnl.add(jtfItemName);

  JLabel jlSecond = new JLabel("Number of :");
  jlSecond.setHorizontalAlignment(JLabel.RIGHT);
  jtfNumberOf = new JTextField(10);
  pnl.add(jlSecond);
  pnl.add(jtfNumberOf);

  JLabel jlThird = new JLabel("Cost");
  jlThird.setHorizontalAlignment(JLabel.RIGHT);
  jtfCost = new JTextField(10);
  pnl.add(jlThird);
  pnl.add(jtfCost);

  JLabel jlFourth = new JLabel("Amount owned :");
  jlFourth.setHorizontalAlignment(JLabel.RIGHT);
  jtfAmountOwned = new JTextField(10);
  jtfAmountOwned.setEditable(false);
  pnl.add(jlFourth);
  pnl.add(jtfAmountOwned);



  JPanel jpSouth = new JPanel(new BorderLayout());

  JPanel jpSouthUp = new JPanel();
  JPanel jpSouthDown = new JPanel();

  jpSouth.add(jpSouthUp, BorderLayout.NORTH);
  jpSouth.add(jpSouthDown, BorderLayout.SOUTH);

  //buttons top
  jbCalculate = new JButton("Calculate");
  jbSave = new JButton("Save");
  jbClear = new JButton("Clear");
  jbExit = new JButton("Exit");
  jpSouthUp.add(jbCalculate);
  jpSouthUp.add(jbSave);
  jpSouthUp.add(jbClear);
  jpSouthUp.add(jbExit);

  //buttons bottom
  jbLoad = new JButton("Load");
  jbPrev = new JButton("<Prev");
  jbNext = new JButton("Next>");

  jpSouthDown.add(jbLoad);
  jpSouthDown.add(jbPrev);
  jpSouthDown.add(jbNext);


  jf.add(pnl);
  jf.add(jpSouth, BorderLayout.SOUTH);
  jf.pack();
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //action listener
  jbCalculate.addActionListener(this);
  jbSave.addActionListener(this);
  jbClear.addActionListener(this);
  jbExit.addActionListener(this);
  jbLoad.addActionListener(this);
  jbPrev.addActionListener(this);
  jbNext.addActionListener(this);


 }
 public double calc() {
   double number = Double.parseDouble(jtfNumberOf.getText());
   double cost = Double.parseDouble(jtfCost.getText());
   //     double sum = number * cost;
   //    calculate = "" + sum;
   //    jtfAmountOwned.setText(calculate);
   calculate = number * cost;
   jtfAmountOwned.setText(calculate + "");
   return calculate;
  }
  //parametar constructor
 public Orders(String itemName, String numberOf, String cost, String amountOwned) {

  this.itemName = itemName;
  this.numberOf = numberOf;
  this.cost = cost;
  this.amountOwned = amountOwned;


 }

 public void setItemName(String itemName) {
  this.itemName = itemName;
 }

 public String getItemName() {
  return itemName;
 }

 public void setNumberOf(String numberOf) {
  this.numberOf = numberOf;
 }

 public String getNumberOf() {
  return numberOf;
 }

 public void setCost(String cost) {
  this.cost = cost;
 }

 public String getCost() {
  return cost;
 }

 public void setAmountOwned(String amountOwned) {
  this.amountOwned = amountOwned;
 }

 public String getAmountOwned() {
  return amountOwned;
 }




 public void actionPerformed(ActionEvent events) {
  if (events.getSource() == jbExit) {
   System.exit(0);
  } else if (events.getSource() == jbClear) {
   jtfItemName.setText("");
   jtfNumberOf.setText("");
   jtfCost.setText("");
   jtfAmountOwned.setText("");
  } else if (events.getSource() == jbCalculate) {
   calc();
  } else if (events.getSource() == jbSave) {

   try {
    PrintWriter pV = new PrintWriter(new BufferedWriter(new FileWriter("121lab1.csv", true)));
    pV.println("' " + jtfItemName.getText() + "', " + jtfNumberOf.getText() + ", " + jtfCost.getText() + ", " + calc());
    pV.close();
   } catch (FileNotFoundException ex) {
    System.out.println("File not found");
   } catch (IOException ioe) {
    System.out.println("IOExcetion");

   } catch (Exception e) {
    System.out.println("General exception");
   }


  } else if (events.getSource() == jbLoad) {
   BufferedReader br = null;
   String line = null;

   try {
    br = new BufferedReader(new FileReader("121Lab1.csv"));
    while ((line = br.readLine()) != null) {
     lines.addAll(Arrays.asList(line.split(",[ ]*")));

    }





   } catch (FileNotFoundException ex) {
    System.out.println("File not found");
   } catch (IOException ioe) {
    System.out.println("IOExcetion");

   } catch (Exception e) {
    System.out.println("General exception");
   }

   System.out.println(lines.size());
  } else if (events.getActionCommand().equals("Next>") || events.getActionCommand().equals("<Prev")) {
   move(events.getActionCommand());
  }
 }
 public void move(String where) {
  if (where.equals("Next>")) {
   if ((index + 4) >= lines.size())
    JOptionPane.showMessageDialog(jf, "No more entries");
   else {
    //String[] name = line.split(split);
    //name nameNext = new name();

    /*index = index + 4;
    jtfItemName.setText(lines.get(index));
    jtfCost.setText(lines.get(index+2));
    jtfNumberOf.setText(lines.get(index + 1));
    jtfAmountOwned.setText(lines.get(index + 3));*/
   }
  } else if (where.equals("<Prev")) {
   if ((index - 4) < 0)
    JOptionPane.showMessageDialog(jf, "No more entries");
   else {
    /*index = index - 4;
     jtfItemName.setText(lines.get(index));
     jtfCost.setText(lines.get(index+2));
     jtfNumberOf.setText(lines.get(index + 1));
     jtfAmountOwned.setText(lines.get(index + 3));*/
   }
  } else {
   jtfItemName.setText(lines.get(0));
   jtfCost.setText(lines.get(2));
   jtfNumberOf.setText(lines.get(1));
   jtfAmountOwned.setText(lines.get(3));
  }


 }

 public static void main(String[] args) {

  Orders test = new Orders();

 }

}

1 个答案:

答案 0 :(得分:0)

在bufferedreader中,文件名为121Lab1.csv。在你的bufferedwriter中,文件名是121lab1.csv。您正在读取121lab1.csv,您正在写信给121Lab1.csv。你怎么期望它的工作? (从不存在的文件中读取)