需要制作Prev和Next JButton Java

时间:2014-02-04 23:26:55

标签: java swing jbutton

大家好我需要让Prev和下一个Jbutton读取从csv文件到JTextField的行我设法生成Load按钮但现在我被卡住了。这是我的作业。所以,如果有人能帮忙,那就太好了:D。这是家庭作业中的问题: “下一个”和“上一个”按钮显示文件中的下一个或上一个订单。 使用String的split方法。下一个,之前需要单独的方法。 不要复制任何大量的代码。指南是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("Armando Vucic's Item Orders Calculator");
    main.setSize(450,   150);
    main.setLocationRelativeTo(null);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:1)

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

    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);
    }
}