我想为下一个和上一个按钮编码。我已经制作了动作监听器,但不知道如何为下一个和上一个按钮编码。
我的代码如下:
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.write("\r\n");
bufferedWriter.write("\"ItemCode:\""+"\""+ items.getText() +"\""+",");
bufferedWriter.write("\"ItemNumber:\""+"\""+number.getText()+"\""+",");
bufferedWriter.write("\"ItemCost:\""+"\""+cost.getText()+"\""+",");
bufferedWriter.write("\"AmountOwned:\""+"\""+amount.getText()+"\""+",");
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 button = new JPanel();
{
button.setLayout(new GridLayout(5,5,2,2));
myFrame.add(button ,BorderLayout.SOUTH);
JButton load;
button.add(load=new JButton("Load"));
load.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
try {
String csvFile = "Documents/workspace/samp/data.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader("data.csv"));
String line = "";
StringTokenizer st = null;
int lineNumber = 0;
int tokenNumber = 0;
//read comma separated file line by line
while ((line = br.readLine()) != null) {
lineNumber++;
//use comma as token separator
st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
tokenNumber++;
//display csv values
System.out.print(st.nextToken() + " ");
}
System.out.println();
//reset token number
tokenNumber = 0;
}
} catch (Exception e1) {
System.err.println("CSV file cannot be read : " + e1);
}
}
}
);
JButton next;
button.add(next=new JButton("Next>"));
next.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
JButton prev;
button.add(prev=new JButton("<Prev"));
prev.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
}
}
}
答案 0 :(得分:0)
使用List存储数据,所以当您的数据如下所示:
a.csv - b.csv - c.csv
因此,当您使用listIterator读取b.csv,sp时,可以使用iterator.previous()
迭代到之前,即读取a.csv而iterator.next()
将导致您读取下一个文件(转发)ieccsv
这意味着您可以使用dequeue来维护列表中的上一个和下一个条目。