需要制作上一条和下一个jbutton java



大家好,我需要进行预上,下一个jbutton才能读取从csv文件到jtextfield的行,我设法完成了加载按钮,但现在我被卡住了。这是我的作业。因此,如果有人能提供帮助,那就太好了:d。这是作业中的问题:下一个和上一个按钮从文件显示下一个或上一个订单。使用字符串的拆分方法。下一步需要单独的方法,上一项。不要复制任何大量代码。指南为3行或更少重复。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
 public class OrderSystem extends JFrame implements ActionListener
{
private JButton exit, cal, save, clear, load, prev, next;
private JTextField text, text1, text2,  text3;
private int counter;
//ArrayList<String> splitLine = new ArrayList<String>();
String[] textLine = new String[3];
public  OrderSystem()
{
    JFrame myFrame  = new   JFrame("{Your Name} Item Orders Calculator");
    myFrame.setLayout(new BorderLayout(2,   2));
    JPanel panel =  new JPanel();
    panel.setLayout( new    GridLayout(0, 2));
    panel.add( new  JLabel("Item name: ", SwingConstants.RIGHT));
    text = new JTextField("", 25);
    panel.add( text );
    panel.add( new  JLabel("Number of: ", SwingConstants.RIGHT));
    text1   = new   JTextField("",  20);
    panel.add( text1 );

    panel.add( new  JLabel("Cost: ", SwingConstants.RIGHT));
    text2   = new   JTextField("",  20);
    panel.add( text2 );
    panel.add( new  JLabel("Amount owed: ", SwingConstants.RIGHT));
    text3   = new   JTextField("",  20);
    text3.setEnabled(false);
    panel.add( text3 );
    add(panel,BorderLayout.CENTER);

    JPanel panelS = new JPanel( new FlowLayout(FlowLayout.CENTER, 20,3)                                    );
    //  Create some buttons to place in the south   area
    cal = new JButton("Calculate");
    save    = new   JButton("Save");
    clear = new JButton("Clear");
    exit    = new   JButton("Exit");
    load    = new   JButton("Load");
    prev    = new   JButton("<prev");
    next    = new   JButton("next>");
    exit.addActionListener(this);
    clear.addActionListener(this);
    cal.addActionListener(this);
    save.addActionListener(this);
    load.addActionListener(this);
    prev.addActionListener(this);
    next.addActionListener(this);
    panelS.add(cal);
    panelS.add(save);
    panelS.add(clear);
    panelS.add(exit);
    panelS.add(load);
    panelS.add(prev);
    panelS.add(next);
    add(panelS, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) 
   {
    if(e.getSource() == exit)
    {
        System.exit(0);
    }
    else if(e.getSource()   ==  cal)
    {
        double amount = Double.parseDouble(text1.getText()) *   Double.parseDouble(text2.getText());
        text3.setText(String.format(Locale.ENGLISH, "%.2f", amount));
    }
    else if(e.getSource()   ==  save)
    {
        boolean append  = true;
        try
        {
        BufferedWriter out = new BufferedWriter(new FileWriter("121Lab1.csv", append));
        out.write(String.format("%s,%s,%s,%s", text.getText(), text1.getText(), text2.getText(),    text3.getText()));
        out.newLine();
        out.flush();
        out.close();
    }
    catch (IOException ex)
    {
        JOptionPane.showMessageDialog(null, "Error");
    }
    counter++;
    }
    else if(e.getSource()   ==  clear)
    {
        text.setText("");
        text1.setText("");
        text2.setText("");
        text3.setText("");
    }
    else if(e.getSource()   ==  load)
    {
        try {
            String splitLine;
            BufferedReader  br  = new   BufferedReader( new FileReader("121Lab1.csv"));
            while ((splitLine = br.readLine())  !=  null)   
            {
            textLine = splitLine.split(",");
            text.setText(textLine[0]);
            text1.setText(textLine[1]);
            text2.setText(textLine[2]);
            text3.setText(textLine[3]);
            }
        } 
        catch (IOException exp) 
        {
            JOptionPane.showMessageDialog(null, "Error no file found.");
        }
    }
    else if(e.getSource()   ==  prev)
    {
        //Prev line
    }
    else if(e.getSource()   ==  next)
    {
        //Read next line
    }
}
public static   void main(String[] args)
{
    OrderSystem main = new OrderSystem();
    main.setTitle("Item Orders Calculator");
    main.setSize(450,   150);
    main.setLocationRelativeTo(null);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
}
}

在单击加载按钮时加载CSV数据。我建议使用OpenCSV一次读取整个CSV文件。CSVReader的readAll()将为您提供字符串数组的列表。

    else if (e.getSource() == load) {
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader("csv.csv"));
            myEntries = reader.readAll();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        ptr = 1;
        navigate(ptr);
    }

myEntries应该是实例级别

private List<String[]> myEntries;

定义一个实例级别int变量,当按下下一个按钮时按下prev按钮时会减小。

private int ptr;

定义一种私人方法,该方法将基于将收到的索引从Myentries列表中检索数据。

private void navigate(int index){
    String[] data = myEntries.get(index);
    text.setText(data[0]);
    text1.setText(data[1]);
    text2.setText(data[2]);
    text3.setText(data[3]);
}

在您的上一个和下一个按钮中,增量/减少PTR,然后愉快地使用导航方法传递结果值。

else if (e.getSource() == prev) {
    if(ptr > 1){
        ptr--;
        navigate(ptr);
    }
} else if (e.getSource() == next) {
    if(ptr < (myEntries.size()-1)){ //lists are 0 based
        ptr++;
        navigate(ptr);
    }
}

最新更新