编码下一个和上一个按钮
Posted
技术标签:
【中文标题】编码下一个和上一个按钮【英文标题】:Coding the Next and Previous button 【发布时间】:2015-02-15 08:24:30 【问题描述】:我想为下一个和上一个按钮编写代码。我已经制作了动作监听器,但不知道如何为下一个和上一个按钮编写代码。
我的代码如下:
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
);
【问题讨论】:
你想用 prev 和 next 按钮做什么?你有维护标准吗? 我只是想在代码中创建的文件 data.csv 中加载数据并加载。我希望下一个和上一个按钮在记录中移动@almasshaikh 【参考方案1】:使用 List 来存储数据,所以当你的数据看起来像这样说:
a.csv - b.csv - c.csv
因此,当您使用 listIterator 读取 b.csv,sp 时,可以使用 iterator.previous()
迭代到上一个文件,即读取 a.csv,而 iterator.next()
将引导您读取下一个文件(转发),即 c.csv。
这意味着您可以使用 dequeue 来维护列表中的上一个和下一个条目。
【讨论】:
能否请您在这里显示代码,因为我是 java 新手以上是关于编码下一个和上一个按钮的主要内容,如果未能解决你的问题,请参考以下文章