我有以下代码。问题是它在列中背靠背显示。
这是我的excel文件中的图片:
一旦从列到列填充单次执行的记录,我希望它从下一行重新开始。
这是我的代码:
public class panel extends OrderSystem {
final JTextField items;
final JTextField number;
final JTextField cost;
final JTextField amount;
public panel() {
JFrame myFrame = new JFrame();
myFrame.setLayout( new FlowLayout() );
myFrame.setTitle("GUI Demo - ISTE-121");
myFrame.pack();
myFrame.setLocation(600,300);
myFrame.setSize(400,200);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JPanel order = new JPanel();
order.setLayout(new GridLayout(5,5,2,2));
myFrame.add(order, BorderLayout.CENTER);
order.add(new JLabel("Item no:", SwingConstants.RIGHT));
order.add(items = new JTextField(3));
order.add(new JLabel("Number of:", SwingConstants.RIGHT));
order.add(number = new JTextField(3));
order.add(new JLabel("Cost", SwingConstants.RIGHT));
order.add(cost = new JTextField(3));
order.add(new JLabel("Amount owed:", SwingConstants.RIGHT));
order.add(amount = new JTextField(10));
amount.setEditable(false);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1,1,2,2));
myFrame.add(buttons ,BorderLayout.CENTER);
JButton calculate;
buttons.add(calculate=new JButton("Calculate"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
double cost1 = new Double(cost.getText().trim()).doubleValue();
double number1 = new Double(number.getText().trim()).doubleValue();
double result = cost1*number1;
amount.setText(String.valueOf(result));
}
});
JButton save;
buttons.add(save = new JButton("Save"));
save.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
try{
// Assume default encoding.
FileWriter fw = new FileWriter("data.csv",true);
BufferedWriter bufferedWriter = new BufferedWriter(fw);
bufferedWriter.newLine();
bufferedWriter.write(',');
bufferedWriter.write("\"ItemCode:\""+"\""+ items.getText() +"\""+",");
bufferedWriter.write("\"ItemNumber:\""+"\""+number.getText()+"\""+",");
bufferedWriter.write("\"ItemCost:\""+"\""+cost.getText()+"\""+",");
bufferedWriter.write("\"AmountOwned:\""+"\""+amount.getText()+"\""+",");
bufferedWriter.newLine();
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ "'");
// Or we could just do this:
// ex.printStackTrace();
}
}}
);
JButton clear;
buttons.add(clear=new JButton("Clear"));
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
items.setText(null);
number.setText(null);
cost.setText(null);
amount.setText(null);
}});
JButton exit;
buttons.add(exit= new JButton("Exit"));
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
JPanel buttons2 = new JPanel();
buttons2.setLayout(new GridLayout(1,1,2,2));
myFrame.add(buttons2 ,BorderLayout.SOUTH);
JButton load;
buttons2.add(calculate=new JButton("Load"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
JButton next;
buttons2.add(calculate=new JButton("Next>"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
JButton prev;
buttons2.add(calculate=new JButton("<Prev"));
calculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
}
}
答案 0 :(得分:0)
Android: How to write newline in CSV file?
这里写的是你可以使用write.print(“\ r \ n”)所以使用“\ r \ n”来创建一个新行
而不是写
bufferedWriter.newLine();
你写了
bufferedWriter.write("\r\n");
或
bufferedWriter.write("\r");
bufferedWriter.newLine();
因为bufferedWriter.newLine();据我所知,只会在文件中写入“\ n”。 希望它会帮助你