cifaFrame.java
package com.zhida;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
class cifaFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JButton jButton = null;
private JLabel jLabel1 = null;
private String st1;
private String st;
private JPanel jPanel21 = null;
private JLabel jLabel2 = null;
private JButton jButton1 = null;
//定义的一个比较运算符优先级大小的数组: + - * / ( ) #
String sign[]= {"+","-","*","/","(",")","#"};
char op[][]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
private JEditorPane jEditorPane = null;
private JScrollPane jScrollPane = null;
private JEditorPane jEditorPane1 = null;
public cifaFrame() {
super();
initialize();
}
private void initialize() {
this.setSize(435, 336);
this.setContentPane(getJContentPane());
this.setTitle("词法分析 作者:陈奉斌 QQ:402027721");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel2.setText(" 语法分析过程 ");
jLabel2.setFont(new Font("Dialog", Font.BOLD, 14));
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
jLabel = new JLabel();
jLabel.setText(" 请输入表达式");
jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel.setPreferredSize(new Dimension(110, 20));
jContentPane = new JPanel();
jContentPane.setBackground(new Color(238, 238, 238));
jContentPane.setLayout(flowLayout);
jContentPane.setPreferredSize(new Dimension(350, 275));
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJPanel(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setPreferredSize(new Dimension(210, 22));
jTextField.setText("以像\"V=i....#\"以'#'结束");
}
return jTextField;
}
private JPanel getJPanel() {
if (jPanel == null) {
FlowLayout flowLayout1 = new FlowLayout();
flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
flowLayout1.setHgap(8);
jPanel = new JPanel();
jPanel.setRequestFocusEnabled(true);
jPanel.setBackground(new Color(237, 235, 241));
jPanel.setLayout(flowLayout1);
jPanel.setPreferredSize(new Dimension(415, 85));
jPanel.add(getJPanel1(), null);
jPanel.add(getJPanel21(), null);
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("词法分析");
jButton.setPreferredSize(new Dimension(86, 22));
jButton.setFont(new Font("Dialog", Font.BOLD, 12));
jButton.setVerticalAlignment(SwingConstants.CENTER);
jButton.setVerticalTextPosition(SwingConstants.CENTER);
jButton.setActionCommand("");
jButton.setHorizontalAlignment(SwingConstants.LEFT);
jButton.setHorizontalTextPosition(SwingConstants.LEFT);
jButton.setMinimumSize(new Dimension(50, 25));
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try{
st1=jTextField.getText(0,jTextField.getText().length()).trim();
String st2=jTextField.getText(0,jTextField.getText().length()-1).trim();
char[] str=st1.toCharArray();
int tr=0;
if(str[0]=='V'&&str[1]=='='&&tr==0){
tr=1;
for(int i=2,n=0;i<st1.length();i++){
int RT=0;
if(str=='i'&&RT==0){
RT=1;
int rt=0;
if(str[i+1]==')'&&n>0&&rt==0){
rt=1;
if(str[i+2]=='#'){
st="合法 "+st2;
i=st1.length();
}
else if(str[i+2]=='+'||str[i+2]=='-'||str[i+2]=='*'||str[i+2]=='/'){
i=i+2;
--n;
}
else {
st="不合法";
i=st1.length();
}
}
else if(str[i+1]=='+'||str[i+1]=='-'||str[i+1]=='*'||str[i+1]=='/'&&rt==0){
rt=1;
i+=1;
}
else if (str[i+1]=='#'&&rt==0&&n==0){
rt=1;
st="合法 "+st2;
i=st1.length();
}
else if(rt==0){
rt=1;
st="不合法";
i=st1.length();
}
}
else if(str=='('&&RT==0){
RT=1;
++n;
}
else if(RT==0){
RT=1;
st="不合法";
i=st1.length();
}
}
}
else if(tr==0){
st="不合法";
}
jEditorPane.setText("");
jEditorPane.setText(st);
}
catch(Exception e){}
}
});
}
return jButton;
}
private JPanel getJPanel1() {
if (jPanel1 == null) {
FlowLayout flowLayout2 = new FlowLayout();
flowLayout2.setAlignment(java.awt.FlowLayout.LEFT);
jLabel1 = new JLabel();
jLabel1.setText("显示判断结果");
jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel1.setBackground(Color.white);
jLabel1.setPreferredSize(new Dimension(95, 20));
jPanel1 = new JPanel();
jPanel1.setBackground(new Color(237, 238, 239));
jPanel1.setLayout(flowLayout2);
jPanel1.setPreferredSize(new Dimension(105, 75));
jPanel1.add(jLabel1, null);
}
return jPanel1;
}
private JPanel getJPanel21() {
if (jPanel21 == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
jPanel21 = new JPanel();
jPanel21.setLayout(gridLayout);
jPanel21.setPreferredSize(new Dimension(285, 75));
jPanel21.add(getJEditorPane(), null);
}
return jPanel21;
}
private JEditorPane getJEditorPane() {
if (jEditorPane == null) {
jEditorPane = new JEditorPane();
jEditorPane.setPreferredSize(new Dimension(260, 72));
jEditorPane.setText("");
}
return jEditorPane;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setPreferredSize(new Dimension(410, 145));
jScrollPane.setViewportView(getJEditorPane1());
}
return jScrollPane;
}
private JEditorPane getJEditorPane1() {
if (jEditorPane1 == null) {
jEditorPane1 = new JEditorPane();
}
return jEditorPane1;
}
[ 本帖最后由 天下第一疯 于 2008-5-6 17:02 编辑 ]