public void fn(String s2)
{
try
{
BufferedReader br=new BufferedReader(new FileReader("Student.txt"));
String s=br.readLine();
Book[] b=Book.StringToObj(s);
if(s2.equals("Read Data"))
{
l5.setText(String.valueOf(b[0].id));
l6.setText(String.valueOf(b[0].title));
l7.setText(String.valueOf(b[0].price));
l8.setText(String.valueOf(b[0].auth));
}
if(s2.equals("<<"))
{
if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
{
JOptionPane.showMessageDialog(this,"You are already at First Record");
}
else
{
l5.setText(String.valueOf(b[0].id));
l6.setText(String.valueOf(b[0].title));
l7.setText(String.valueOf(b[0].price));
l8.setText(String.valueOf(b[0].auth));
}
}
if(s2.equals(">>"))
{
if(l5.getText().equals(String.valueOf(b[b.length-1].id))&&l6.getText().equals(String.valueOf(b[b.length-1].title))&&l7.getText().equals(String.valueOf(b[b.length-1].price))&&l8.getText().equals(String.valueOf(b[b.length-1].auth)))
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
l5.setText(String.valueOf(b[b.length-1].id));
l6.setText(String.valueOf(b[b.length-1].title));
l7.setText(String.valueOf(b[b.length-1].price));
l8.setText(String.valueOf(b[b.length-1].auth));
}
}
if(s2.equals("<"))
{
if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
{
JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
}
else
{
}
}
if(s2.equals(">"))
{
}
}
catch(Exception e)
{
}
}
当我点击“&gt;”按钮我想从Array中获取Next记录。 我该怎么办?
答案 0 :(得分:3)
为什么不将当前页面存储在int
之类的变量中,然后用它进行操作?
这样的事情:
int page = 0;
Book[] books;
public void init()
{
BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
String s = br.readLine();
books = Book.StringToObj(s);
}
public void show()
{
l5.setText(String.valueOf(books[page].id));
l6.setText(String.valueOf(books[page].title));
l7.setText(String.valueOf(books[page].price));
l8.setText(String.valueOf(books[page].auth));
}
public void fn(String s2)
{
try
{
if(s2.equals("Read Data"))
{
show();
}
if(s2.equals("<<"))
{
if (page == 0)
{
JOptionPane.showMessageDialog(this,"You are already at First Record");
}
else
{
page = 0;
show();
}
}
if(s2.equals(">>"))
{
if (page == books.length - 1)
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
page = books.length-1;
show();
}
}
if(s2.equals("<"))
{
if (page == 0)
{
JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
}
else
{
page--;
show();
}
}
if(s2.equals(">"))
{
if (page == books.length - 1)
{
JOptionPane.showMessageDialog(this,"You are already at Last Record");
}
else
{
page++;
show();
}
}
}
catch(Exception e)
{
}
}
请注意,我已将books
移至全局范围,因为每次都无法再次读取文件。
答案 1 :(得分:1)
您需要设置一个全局变量。将此变量用作索引以从数组中获取值。最初这将是一个,显示数组中的第一个元素。当您单击最后一次更改变量值时。单击next或previous时,只需增加或减少变量的值,并在该位置显示数组元素。