请教各位大哥,小弟的向下查找功能已经实现了,向上查找不会做。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//执行类
public class Test
{
public static void main(String[] args)
{
myFrame f = new myFrame();
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//框架类
class myFrame extends JFrame
{
private JLabel label;
private JButton up, down;
private JTextField text;
private JTextArea area;
private String src, str;
private int n = 0;
public myFrame()
{
this.setTitle("通讯录");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
label = new JLabel("输入查找的字符串:");
text = new JTextField(10);
up = new JButton("向上查找");
down = new JButton("向下查找");
area = new JTextArea(10, 70);
JScrollPane scroll = new JScrollPane(area);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(label);
c.add(text);
c.add(scroll);
c.add(up);
c.add(down);
down.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
src = area.getText().toLowerCase();
str = text.getText().toLowerCase();
n = area.getCaretPosition(); //取得当前光标位置
area.requestFocus(); //获取光标
int i = src.indexOf(str, n); //取得找到的那个字符串的索引值
if (i != -1)
area.select(i, i + str.length()); //选中取得的字符串
else
System.out.println("找不到" + str);
}
});
up.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//???????????????????
}
});
}
}