📄 testfind.java
字号:
/*
*Code by Lee
*/
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFind extends JFrame implements ActionListener
{
private JLabel l=new JLabel("查找");
private JLabel tishi1=new JLabel("友情提示:查找括号是否匹配");
private JLabel tishi2=new JLabel("点击打开按钮选择");
private JLabel tishi3=new JLabel("要查找的文件....");
private JTextField tResult=new JTextField(15);//定义显示结果的文本框
private JButton bbs = new JButton("打开");
private JButton bcl = new JButton("查找");
private boolean isOpened = false;
private String s = new String();
public TestFind(){//构造方法
super("判断括号");//设置标题
Container contentPane=getContentPane();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
contentPane.setLayout(gbl);
l.setForeground(Color.blue);
gbc.gridx=1;gbc.gridy=1;
gbc.gridwidth=5;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(l,gbc);
contentPane.add(l);
tResult.setEditable(false);//添加文本框
tResult.setBackground(Color.cyan);
tResult.setForeground(Color.red);
tResult.setHorizontalAlignment(JTextField.RIGHT);
gbc.gridx=1;gbc.gridy=2;
gbc.gridwidth=5;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(tResult,gbc);
contentPane.add(tResult);
bbs.addActionListener(this);//添加打开按钮
bbs.setForeground(Color.red);
gbc.gridx=1;gbc.gridy=3;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(bbs,gbc);
contentPane.add(bbs);
bcl.addActionListener(this);//添加查找按钮
bcl.setForeground(Color.red);
gbc.gridx=3;gbc.gridy=3;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(bcl,gbc);
contentPane.add(bcl);
tishi1.setForeground(Color.blue);
gbc.gridx=1;gbc.gridy=9;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(tishi1,gbc);
contentPane.add(tishi1);
tishi2.setForeground(Color.blue);
gbc.gridx=1;gbc.gridy=10;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(tishi2,gbc);
contentPane.add(tishi2);
tishi3.setForeground(Color.blue);
gbc.gridx=1;gbc.gridy=11;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets=new Insets(2,1,1,1);
gbl.setConstraints(tishi3,gbc);
contentPane.add(tishi3);
//布局结束
}//构造方法结束
public void actionPerformed(ActionEvent e){
if(e.getSource() == bbs )
{
JFileChooser c = new JFileChooser();
int rVal = c.showOpenDialog(TestFind.this);
if(rVal == JFileChooser.APPROVE_OPTION)
{
isOpened = true;
tResult.setText(s = c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName());
}
if(rVal==JFileChooser.CANCEL_OPTION)
{
tResult.setText("You pressed cancel");
}
}
if(e.getSource() == bcl)
{
if(!isOpened)
{
JFrame f = new JFrame();
f.setVisible(false);
JOptionPane.showMessageDialog(f,"你还没有选择文件!",
"Error",JOptionPane.ERROR_MESSAGE);
}
else
{
new StartFind(s);
}
}
}//结束actionPerformed方法
public static void main(String args[])//main方法
{
JFrame frame = new TestFind();
frame.setSize(200,330);
frame.setLocation(300,200);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class StartFind
{
int i=0;
String temp = new String();
String sou = new String();
private LinkedList sd = new LinkedList();//通过LindedList来实现堆栈
public StartFind(String s)
{
File f = new File(s);
FileReader fin = null;
BufferedReader br ;
try
{
fin=new FileReader(f);
}
catch(FileNotFoundException e)
{
JFrame frame = new JFrame();
frame.setVisible(false);
JOptionPane.showMessageDialog(frame,"文件未找到,请重试!",
"Error",JOptionPane.ERROR_MESSAGE);
}
br = new BufferedReader(fin);
try
{
while((temp=br.readLine())!=null)
{
sou+=temp;
}
}
catch(IOException e)
{
JFrame frame = new JFrame();
frame.setVisible(false);
JOptionPane.showMessageDialog(frame,"文件读取错误,请重试!",
"Error",JOptionPane.ERROR_MESSAGE);
}
temp = new String();
for(i=0;i<=sou.length()-1;i++)//将包含括号的所有字符全部顺序保存到一个字符串里面
{
if(sou.charAt(i)=='{' || sou.charAt(i)=='[' || sou.charAt(i)=='(' || sou.charAt(i)=='}'
|| sou.charAt(i)==']' || sou.charAt(i)==')')
temp+=sou.charAt(i);
}
if(analyiz(0))//analyiz(0)方法为判断方法.返回值是boolean类型的
{
JFrame frame = new JFrame();
frame.setVisible(false);
JOptionPane.showMessageDialog(frame,"文件中的括号都匹配!",
"information",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JFrame frame = new JFrame();
frame.setVisible(false);
JOptionPane.showMessageDialog(frame,"文件中包含不匹配的括号!",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
//利用堆栈递归方法.来实现对括号匹配的判断
public boolean analyiz(int ip)
{
char tempChar = temp.charAt(ip);
char a;
if (tempChar == '(' || tempChar == '[' || tempChar == '{')
{
sd.add(tempChar);
}
else if (tempChar == ')')
{
a = (Character)sd.getLast();
if (a == '(')
{
sd.removeLast();
}
}
else if (tempChar == ']')
{
a = (Character)sd.getLast();
if (a == '[')
{
sd.removeLast();
}
}
else if (tempChar == '}')
{
a = (Character)sd.getLast();
if (a == '{')
{
sd.removeLast();
}
}
if (sd.size() == 0 && ip == temp.length() - 1)
{
return true;
}
else if (sd.size() != 0 && ip == temp.length() - 1)
{
return false;
}
else
{
return analyiz(ip + 1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -