📄 searchtest.java
字号:
package search;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class SearchTest extends JFrame implements ActionListener
{
private JTextArea textarea;
private JTextField textfield;
int n=0;
private Container con= getContentPane();
public SearchTest()
{
//设置窗口信息
super("查找测试程序");
setSize(420,500);
setResizable(false);
//设置版面布局
con.setLayout(null);
//显示窗口显示风格
Dimension screensize =Toolkit.getDefaultToolkit().getScreenSize();//取当前屏幕大小值
setLocation(screensize.width/2-250,screensize.height/2-300);//设置窗口在屏幕中央
// setUndecorated(true);
// getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
//调用实现方法
init();
//设置窗口事件
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//程序主方法
public void init()
{
String s="wqedsfdgfhgutegfvhgjsdgfghd\n"+
"wehgkjlgymk;nbvcsdtkokll\n"+
"sdfyilkmbnvnmujitrhgfhh\n"+
"vbhnjgjfyersytusebgjhgjgfh";
textarea=new JTextArea(s);
textarea.setBounds(10,10,380,300);
con.add(textarea);
textfield=new JTextField();
textfield.setBounds(50,350,100,30);
con.add(textfield);
JButton testBut=new JButton("开始查找");
testBut.setBounds(50,400,100,30);
testBut.addActionListener(this);
con.add(testBut);
JButton exitBut=new JButton("退出");
exitBut.setBounds(250,400,100,30);
exitBut.addActionListener(this);
con.add(exitBut);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="开始查找")
{
String target=textarea.getText();
String pattern=textfield.getText();
StringSearch ss=new StringSearch(pattern);
n=ss.find(target);
textarea.setCaretColor(Color.cyan);
textarea.setSelectionColor(Color.yellow);
textarea.setCaretPosition(n);
textarea.select(n,pattern.length());
}
if(e.getActionCommand()=="退出")
{
System.exit(0);
}
}
public static void main(String args[])
{
(new SearchTest()).show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -