大家好,我需要创建上一个和下一个Jbutton才能将一行从csv文件读取到JTextField的行中,我设法创建了Load按钮,但是现在我被卡住了。这是我的功课。因此,如果有人可以提供帮助,那将是不错的:D。这是作业中的问题:下一个和上一个按钮显示文件中的下一个或上一个顺序。使用字符串的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("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变量,该变量在按下上一个按钮时将减少,在按下下一个按钮时将增加。
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);
}
}