目录
一、写在前面(不想看直接第二步)
知道会没人看,但为了自我感动,还是写点废话吧。毕竟,原创不易~
点赞
二、gitte与压缩包
三、具体代码
Keyword.java
package text01;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Keyword {
JFrame frame=new JFrame("关键字表");
JMenuBar menuBar;
JMenuItem addJMenuItem;
JMenuItem changeJMenuItem;
JMenuItem delJMenuItem;
JTable table;
DefaultTableModel dtm;
String title[]= {"ID","关键字"};
String data[][]=new String[][] {};
String no_MainString="";
void init() {
frame.setBounds((1500-500)/2+50,(800-400)/2+50,500,400);
frame.setResizable(true);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("src/data/image/关键字表图标.png").getImage());
menuBar=new JMenuBar();
addJMenuItem=new JMenuItem("添加");
menuBar.add(addJMenuItem);
addJMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_keyWord_add(frame, "添加",true, no_MainString).setVisible(true);
}
});
changeJMenuItem=new JMenuItem("修改");
menuBar.add(changeJMenuItem);
changeJMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_keyWord_change(frame, "修改", true, no_MainString).setVisible(true);
}
});
delJMenuItem=new JMenuItem("删除");
menuBar.add(delJMenuItem);
delJMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_keyWord_del(frame, "删除", true, no_MainString).setVisible(true);
}
});
see_data();
frame.setJMenuBar(menuBar);
}
private void see_data() {
dtm=new DefaultTableModel(data,title) {
@Override
public boolean isCellEditable(int hang,int lie) {
return false;
}
};
add_data();
}
private void add_data() {
table=new JTable();
no_MainString=new Way().Read_Data("src/data/关键字表.txt");
String arr2[]=no_MainString.split(",");
String arr[]=new String[2] ;
for(int i=0;i<arr2.length;i++) {
int id=i+1;
arr[0]=""+id;
arr[1]=arr2[i];
dtm.addRow(arr);
table.setModel(dtm);
}
frame.add(table);
JScrollPane jsp=new JScrollPane(table);
frame.add(jsp);
}
}
Language_Rules.java
package text01;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Language_Rules {
JFrame frame=new JFrame("语法规则");
String title[]= {"单词","巴克斯范式"};
String data[][]=new String[][] {};
DefaultTableModel dtm;
JTable table;
String not_MainString="";
void init() {
frame.setBounds((1500-1000)/2+50,(800-600)/2+50, 1000, 600);
frame.setVisible(true);
frame.setResizable(true);
frame.setIconImage(new ImageIcon("src/data/image/语法规则图标.png").getImage());
see_data();
}
void see_data() {
dtm=new DefaultTableModel(data,title) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
add_data();
}
void add_data() {
table=new JTable(dtm);
not_MainString="";
not_MainString=new Way().Read_Data("src/data/语法规则表.txt");
String arr[]=new String[2];
String arr2[]=not_MainString.split(" ");
for(int i=0;i<arr2.length/2;i++) {
arr[0]=arr2[i*2];
arr[1]=arr2[i*2+1];
dtm.addRow(arr);
table.setModel(dtm);
}
JScrollPane jsp=new JScrollPane(table);
frame.add(jsp);
}
}
MainClass.java
package text01;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame("DFA识别");
frame.setBounds((1500-800)/2+50, (800-600)/2+50, 800, 600);
frame.setVisible(true);
ImageIcon img=new ImageIcon("src/data/image/登录.gif");
JLabel label=new JLabel(img);
label.setBounds(0, 0, frame.getWidth(),frame.getHeight());
JPanel imageJPanel=(JPanel)frame.getContentPane();
imageJPanel.setOpaque(false);
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setIconImage(new ImageIcon("src/data/image/主图标.jpg").getImage());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
Box vBox=Box.createVerticalBox();
Box hBox=Box.createHorizontalBox();
TextField tf=new TextField("用户名",1);
tf.setFont(new Font("",Font.BOLD,15));
vBox.add(Box.createVerticalStrut(200));
hBox.add(Box.createHorizontalStrut(200));hBox.add(tf);hBox.add(Box.createHorizontalStrut(200));
vBox.add(hBox);
vBox.add(Box.createVerticalStrut(30));
Box h2Box=Box.createHorizontalBox();
TextField tf2=new TextField("密码",1);
tf2.setFont(new Font("",Font.BOLD,15));
h2Box.add(Box.createHorizontalStrut(200));h2Box.add(tf2);h2Box.add(Box.createHorizontalStrut(200));
vBox.add(h2Box);
vBox.add(Box.createVerticalStrut(30));
Box h3Box=Box.createHorizontalBox();
JButton button=new JButton("登录");button.setBackground(new Color(255, 228, 196));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
new MainDesk().init();
}
});
button.setFont(new Font("楷体",Font.BOLD,17));
vBox.add(Box.createHorizontalStrut(20));
h3Box.add(button);
vBox.add(h3Box);
vBox.add(Box.createVerticalStrut(230));
frame.add(vBox);
}
}
MainDesk.java
package text01;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class MainDesk {
JFrame frame;
JMenuBar menuBar;
JMenu oSMaintenanceMenu;
JMenuItem ziFu_BiaoItem;
JMenuItem word_FormatItem;
JMenuItem language_RuleItem;
JMenu cifa_analyiseJMenu;
JMenuItem keyword_JMenuItem;
JMenuItem singleword_JMenuItem;
JMenuItem DFA_JMenuItem;
JMenuItem process_exhabitJMenuItem;
JMenu helpJMenu;
JMenuItem helpItem;
void init() {
frame=new JFrame("DFA识别");
frame.setBounds((1500-900)/2+50, (800-650)/2+50, 900, 650);
frame.setVisible(true);
ImageIcon img=new ImageIcon("src/data/image/主界面背景.jpg");
JLabel label=new JLabel(img);
label.setBounds(0, 0, frame.getWidth(),frame.getHeight());
JPanel imageJPanel=(JPanel)frame.getContentPane();
imageJPanel.setOpaque(false);
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setIconImage(new ImageIcon("src/data/image/主图标.jpg").getImage());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
menuBar=new JMenuBar();
menuBar.setPreferredSize(new Dimension(frame.getWidth(),25));
oSMaintenanceMenu=new JMenu("系统维护");
oSMaintenanceMenu.setFont((new Font("楷体",Font.BOLD,20)));
oSMaintenanceMenu.setPreferredSize(new Dimension(frame.getWidth()/2,20));
ziFu_BiaoItem=new JMenuItem("字符表");
oSMaintenanceMenu.add(ziFu_BiaoItem);
oSMaintenanceMenu.addSeparator();
ziFu_BiaoItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ZifuBiao().init();
}
});
word_FormatItem=new JMenuItem("单词分类");
oSMaintenanceMenu.add(word_FormatItem);
oSMaintenanceMenu.addSeparator();
word_FormatItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Word_format().init();
}
});
language_RuleItem=new JMenuItem("语法规则");
oSMaintenanceMenu.add(language_RuleItem);
language_RuleItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Language_Rules().init();
}
});
menuBar.add(oSMaintenanceMenu);
cifa_analyiseJMenu=new JMenu("词法分析");
cifa_analyiseJMenu.setFont((new Font("楷体",Font.BOLD,20)));
cifa_analyiseJMenu.setPreferredSize(new Dimension(frame.getWidth()/2,20));
keyword_JMenuItem=new JMenuItem("关键字表");
cifa_analyiseJMenu.add(keyword_JMenuItem);
cifa_analyiseJMenu.addSeparator();
keyword_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Keyword().init();
}
});
singleword_JMenuItem=new JMenuItem("单字符分界符");
cifa_analyiseJMenu.add(singleword_JMenuItem);
cifa_analyiseJMenu.addSeparator();
singleword_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Single_Word().init();
}
});
DFA_JMenuItem=new JMenuItem("DFA");
cifa_analyiseJMenu.add(DFA_JMenuItem);
cifa_analyiseJMenu.addSeparator();
DFA_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Cifa_analyise().init();
}
});
process_exhabitJMenuItem=new JMenuItem("过程展示");
cifa_analyiseJMenu.add(process_exhabitJMenuItem);
process_exhabitJMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Process_exhabit().init();
}
});
menuBar.add(cifa_analyiseJMenu);
frame.setJMenuBar(menuBar);
}
}
Process_exhabit.java
package text01;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Process_exhabit {
JFrame frame=new JFrame("词法分析过程展示");
void init(){
frame.setBounds((1500-500)/2+50, (800-400)/2+50, 500, 400);
frame.setResizable(true);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("src/data/image/DFA图标.jpg").getImage());
}
}
Single_Word_add.java
package text01;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Single_Word_add extends JDialog{
public Single_Word_add(JFrame jf,String title,boolean isModeal,String NOT_main_string) {
super(jf,title,isModeal);
this.setBounds((1500-200)/2+50, (800-150)/2+50, 200, 150);
Box mainBox=Box.createVerticalBox();
Box nameBox=Box.createHorizontalBox();
JLabel label=new JLabel("内容:");
nameBox.add(Box.createHorizontalStrut(20));
nameBox.add(label);
JTextField tf=new JTextField(15);
mainBox.add(Box.createVerticalStrut(20));
nameBox.add(Box.createHorizontalStrut(30));
nameBox.add(tf);
nameBox.add(Box.createHorizontalStrut(65));
mainBox.add(nameBox);
mainBox.add(Box.createVerticalStrut(20));
Box btnBox=Box.createHorizontalBox();
JButton button=new JButton("添加");
button.setBackground(new Color(133, 224, 255));
button.setBorder(BorderFactory.createRaisedBevelBorder());
btnBox.add(Box.createHorizontalStrut(10));
btnBox.add(button);
mainBox.add(btnBox);
mainBox.add(Box.createVerticalStrut(20));
this.add(mainBox);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String addString=tf.getText();
String change_String;
if(addString.length()>1) {
JOptionPane.showMessageDialog(jf, "添加失败!\n只能添加单字符");
}
else if(addString.length()==0||addString==null){
JOptionPane.showMessageDialog(jf, "添加失败!\n没有数据");
}
else if(CZ(addString,NOT_main_string)==0) {
JOptionPane.showMessageDialog(jf, "添加失败!\n字符表中已存在内容");
}
else {
change_String=NOT_main_string+addString;
JOptionPane.showMessageDialog(jf, "字符表\n添加"+addString+"成功!");
new Way().Wrtie_Data(change_String, "src/data/单字符分界符表.txt");
jf.setVisible(false);
}
}
});
}
private int CZ(String addString,String NOT_main_string){
if(NOT_main_string.indexOf(addString)==-1)
return 1;
return 0;
}
}
Single_Word_change.java
package text01;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Single_Word_change extends JDialog{
public Single_Word_change(JFrame jf,String title,boolean isModeal,String not_main_String) {
super(jf,title,isModeal);
this.setBounds((1500-200)/2+50, (800-150)/2+50, 200, 150);
Box vbox=Box.createVerticalBox();
Box hBox=Box.createHorizontalBox();
JLabel label=new JLabel("ID");
TextField tf=new TextField(1);
hBox.add(Box.createHorizontalStrut(35));
hBox.add(label);
hBox.add(Box.createHorizontalStrut(30));
hBox.add(tf);
hBox.add(Box.createHorizontalStrut(70));
vbox.add(Box.createVerticalStrut(15));
vbox.add(hBox);
Box h2Box=Box.createHorizontalBox();
JLabel label2=new JLabel("改后字符");
TextField tf2=new TextField(1);
h2Box.add(Box.createHorizontalStrut(15));
h2Box.add(label2);
h2Box.add(Box.createHorizontalStrut(10));
h2Box.add(tf2);
h2Box.add(Box.createHorizontalStrut(70));
vbox.add(Box.createVerticalStrut(10));
vbox.add(h2Box);
JButton button=new JButton("修改");
Box h3Box=Box.createHorizontalBox();
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.setBackground(new Color(133, 224, 255));
h3Box.add(button);
vbox.add(Box.createVerticalStrut(10));
vbox.add(h3Box);
vbox.add(Box.createVerticalStrut(10));
this.add(vbox);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String id_String=tf.getText();
String zifu_String=tf2.getText();
if(id_String==null||id_String.length()==0) {
JOptionPane.showMessageDialog(jf, "I修改失败!\nID里面没有内容");
}
else if(!new Way().isNumeric(id_String)){
JOptionPane.showMessageDialog(jf,"修改失败!\nID不为数字类型");
}
else {
int ID=Integer.parseInt(id_String);
if(ID<1||ID>not_main_String.length())
JOptionPane.showMessageDialog(jf, "修改失败!\nID超出范围");
else if(zifu_String.length()==0){
JOptionPane.showMessageDialog(jf, "修改失败\n修改后的字符内容不能为空");
}
else if(not_main_String.indexOf(zifu_String)!=-1) {//添加字符
JOptionPane.showMessageDialog(jf, "修改失败!\n该字符在原表中已存在");
}
else if(zifu_String.length()>1){
JOptionPane.showMessageDialog(jf, "修改失败\n修改后的字符只能有1位");
}
else {
String op=""+not_main_String.charAt(ID-1);
String content=not_main_String.replace(""+not_main_String.charAt(ID-1),zifu_String);
new Way().Wrtie_Data(content,"src/data/单字符分界符表.txt");
JOptionPane.showMessageDialog(jf, "修改成功\n"+"原 ID:"+ID+" 字符:"+op+"\n"+"现 ID:"+ID+" 字符:"+zifu_String);
jf.setVisible(false);
}
}
}
});
}
}
Single_Word_del.java
package text01;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Single_Word_del extends JDialog{
public Single_Word_del(JFrame jf,String title,boolean isModeal,String NOT_main_string) {
super(jf,title,isModeal);
this.setBounds((1500-200)/2+50, (800-150)/2+50, 200, 150);
Box vbox=Box.createVerticalBox();
Box hbox=Box.createHorizontalBox();
JLabel label=new JLabel("ID");
hbox.add(Box.createHorizontalStrut(35));
hbox.add(label);
hbox.add(Box.createHorizontalStrut(30));
TextField tf=new TextField(1);
hbox.add(tf);
hbox.add(Box.createHorizontalStrut(70));
vbox.add(Box.createVerticalStrut(25));
vbox.add(hbox);
vbox.add(Box.createHorizontalStrut(10));
vbox.add(Box.createVerticalStrut(20));
Box pbox=Box.createHorizontalBox();
JButton button=new JButton("删除ID");
button.setBackground(new Color(133, 224, 255));
button.setBorder(BorderFactory.createRaisedBevelBorder());
pbox.add(Box.createHorizontalStrut(5));
pbox.add(button);
vbox.add(pbox);
vbox.add(Box.createVerticalStrut(20));
this.add(vbox);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String del_String=tf.getText();
if(del_String.length()==0||del_String==null) {
JOptionPane.showMessageDialog(jf,"删除失败!\n没有数据");
}
else if(!new Way().isNumeric(del_String)) {
JOptionPane.showMessageDialog(jf,"删除失败!\n删除内容不为数字类型");
}
else {
int ID=Integer.parseInt(del_String);
if(ID<1||ID>NOT_main_string.length()) {
JOptionPane.showMessageDialog(jf, "删除失败!\nID超出范围");
}
else{
JOptionPane.showMessageDialog(jf, "删除ID:"+ID+"成功");
String del_afterString=del(NOT_main_string,ID-1);
new Way().Wrtie_Data(del_afterString,"src/data/单字符分界符表.txt");
jf.setVisible(false);
}
}
}
});
}
//删除指定位置字符,并返回删除后的新数据
private String del (String Yuan,int index) {
String DEL_after="";
for(int i=0;i<=index-1;i++) {
DEL_after+=Yuan.charAt(i);
}
for(int i=index+1;i<Yuan.length();i++) {
DEL_after+=Yuan.charAt(i);
}
return DEL_after;
}
}
Single_Word.java
package text01;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Single_Word {
JFrame frame=new JFrame("单字符分界符");
JMenuBar menuBar;
JMenuItem add_JMenuItem;
JMenuItem change_JMenuItem;
JMenuItem del_JMenuItem;
String[] title= {"ID","单分界符"};
String[][]data=new String[][] {};
DefaultTableModel dtm;
JTable table;
String NOT_main_string="";
void init(){
frame.setBounds((1500-500)/2+50, (800-400)/2+50, 500, 400);
frame.setResizable(true);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("src/data/image/分界符图标.jpg").getImage());
menuBar=new JMenuBar();
//---------------菜单添加------------------------------
add_JMenuItem=new JMenuItem("添加");
menuBar.add(add_JMenuItem);
add_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Single_Word_add(frame, "添加", true, NOT_main_string).setVisible(true);
}
});
//---------------菜单修改------------------------------
change_JMenuItem=new JMenuItem("修改");
menuBar.add(change_JMenuItem);
change_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Single_Word_change(frame, "修改", true, NOT_main_string).setVisible(true);
}
});
//---------------菜单删除------------------------------
del_JMenuItem=new JMenuItem("删除");
menuBar.add(del_JMenuItem);
del_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Single_Word_del(frame, "删除", true, NOT_main_string).setVisible(true);
}
});
frame.setJMenuBar(menuBar);
see_data();
}
private void see_data() {
dtm=new DefaultTableModel(data,title) {
@Override
public boolean isCellEditable(int hang,int lie) {
return false;
}
};
add_data();
}
private void add_data() {
NOT_main_string="";
table=new JTable(dtm);
NOT_main_string=new Way().Read_Data("src/data/单字符分界符表.txt");
String arr[]=new String[2];
int ID;
for(int i=0;i<NOT_main_string.length();i++) {
ID=i+1;
arr[0]=""+ID;
arr[1]=""+NOT_main_string.charAt(i);
dtm.addRow(arr);
table.setModel(dtm);
}
JScrollPane jsp=new JScrollPane(table);
frame.add(jsp);
}
}
Way.java
package text01;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Way {
//判断字符串是否为数字类型
public boolean isNumeric(String del_String) {
// TODO Auto-generated method stub
if(del_String.length()==0)
return false;
for (int i = del_String.length()-1;i>=0;i--){
if (!Character.isDigit(del_String.charAt(i))){
return false;
}
}
return true;
}
//向File中重新写入新数据
public boolean Wrtie_Data(String afterString,String file) {
// TODO Auto-generated method stub
String content=afterString;
try {
BufferedWriter writer=new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
}catch (IOException el) {
el.printStackTrace();
// TODO: handle exception
}
return true;
}
//从file读取数据返回子符串
public String Read_Data(String file) {
String not_MainString="";
try {
String line="";
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
//UTF-8中文不显示
while((line=reader.readLine())!=null) {
not_MainString+=line;
}
reader.close();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return not_MainString;
}
public String Read_Data(File file) {
String not_MainString="";
try {
String line="";
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
//UTF-8中文不显示
while((line=reader.readLine())!=null) {
not_MainString=not_MainString+line+"\n";
}
reader.close();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return not_MainString;
}
//查找串数组中是否有字串
public boolean ZiChuan_find(String Yuan,String string) {
String arr[]=Yuan.split(",");
for(int i=0;i<arr.length;i++) {
if(string.equals(arr[i]))
return true;
}
return false;
}
//将字符串进行分类
public String cifa_analysie_format(String op) {
String KeyWord=Read_Data("src/data/关键字表.txt");
String Zifu=Read_Data("src/data/标识符表.txt");
String Format_Word=Read_Data("src/data/单字符分界符表.txt");
String ShuangZi=":=";
String ZhuShiTou="{";
String ZhuShiWei="}";
String zifuChuan="'";
String Shuzu_Xiabiao="..";
String end=".";
if(KeyWord.indexOf(op)!=-1&&op.length()>1) {
return "关键字";
}
else if(isNumeric(op)) {
return "无符号整数";
}
else if(Zifu.indexOf(op)!=-1) {
return "标识符";
}
else if(Format_Word.indexOf(op)!=-1) {
return "单字符分界符";
}
else if(op.equals(ShuangZi)) {
return "双字符分界符";
}
else if(op.equals(ZhuShiTou)) {
return "注释头符";
}
else if(op.equals(ZhuShiWei)) {
return "注释尾符";
}
else if(op.equals(zifuChuan)) {
return "字符串标识符";
}
else if(op.equals(Shuzu_Xiabiao)) {
return "数组下标界限符";
}
else if(op.equals(end)) {
return "程序结束";
}
else {
return "未识别";
}
}
//取消注释
public String cifa_analyise_del(String Yuan) {
String nowString="";
if(Yuan.indexOf("{")!=-1&&Yuan.indexOf("}")!=-1&&Yuan.indexOf("{")<Yuan.indexOf("}")) {
for(int i=0;i<Yuan.indexOf("{");i++) {
nowString+=Yuan.charAt(i);
}
for(int i=Yuan.indexOf("}")+1;i<Yuan.length();i++) {
nowString+=Yuan.charAt(i);
}
return nowString;
}
else {
return Yuan;
}
}
}
Word_format.java
package text01;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Word_format {
JFrame frame;
String word_format_title[]= {"序号","分类","内容"};
String data[][]=new String[][] {};
DefaultTableModel dtm;
JTable table;
String not_MainString="";
public void init() {
frame=new JFrame("单词分类表");
frame.setBounds((1500-500)/2+50,(800-400)/2+50, 500, 400);
frame.setResizable(true);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("src/data/image/单词分类表图标.jpg").getImage());
see_data();
}
private void see_data() {
dtm = new DefaultTableModel(data, word_format_title) {
//设置单元格不可编辑
@Override
public boolean isCellEditable(int hang , int lie) {
return false;
}
};
add_data();
}
private void add_data() {
table=new JTable(dtm);
not_MainString="";
not_MainString=new Way().Read_Data("src/data/单词分类表.txt");
String []arr=new String[3];
String arr2[]=not_MainString.split(" ");
for(int i=0;i<arr2.length/2;i++) {
int id=i+1;
arr[0]=""+id;
arr[1]=""+arr2[i*2];
arr[2]=""+arr2[i*2+1];
dtm.addRow(arr);
table.setModel(dtm);
}
JScrollPane jsp = new JScrollPane(table);
frame.add(jsp);
}
}
ZifuBiao.java
package text01;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ZifuBiao {
JFrame frame;
JMenuBar menuBar;
JMenuItem add_JMenuItem;
JMenuItem change_JMenuItem;
JMenuItem del_JMenuItem;
String[] zifu_title= {"ID","字符"};
String[][]data=new String[][] {};
DefaultTableModel dtm;
JTable table;
String NOT_main_string="";
void init() {
frame=new JFrame("字符表");
frame.setBounds((1500-500)/2+50, (800-400)/2+50, 500, 400);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("src/data/image/字符表图标.jpg").getImage());
menuBar=new JMenuBar();
//---------------菜单添加------------------------------
add_JMenuItem=new JMenuItem("添加");
menuBar.add(add_JMenuItem);
add_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_zifuBiao_add(frame,"添加", true,NOT_main_string).setVisible(true);
}
});
//---------------菜单修改------------------------------
change_JMenuItem=new JMenuItem("修改");
menuBar.add(change_JMenuItem);
change_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_zifuBiao_change(frame, "修改", true, NOT_main_string).setVisible(true);
}
});
//---------------菜单删除------------------------------
del_JMenuItem=new JMenuItem("删除");
menuBar.add(del_JMenuItem);
del_JMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Dialog_zifubiao_del(frame, "删除", true, NOT_main_string).setVisible(true);
}
});
frame.setJMenuBar(menuBar);
see_data();
}
private void see_data() {
dtm = new DefaultTableModel(data, zifu_title) {
//设置单元格不可编辑
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
add_data();
}
private void add_data() {
//bug 问题,see_data()从字符表读取的数据有多次循环,已解决
NOT_main_string="";
//bug 解决,每次读取前都将初始数据默认为空,如果不为空,进行NOT_main_string+=line;会将初始的seedata加入
table=new JTable(dtm);
NOT_main_string=new Way().Read_Data("src/data/字符表.txt");
String []arr=new String[2];
for(int i=0;i<NOT_main_string.length();i++) {
int id=i+1;
arr[0]=""+id;
arr[1]=""+NOT_main_string.charAt(i);
dtm.addRow(arr);
table.setModel(dtm);
}
JScrollPane jsp = new JScrollPane(table);
frame.add(jsp);
}
}