📄 expframe.java
字号:
package datastructure;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.applet.*;
public class expFrame
extends AnimationFrame
implements ActionListener, Runnable {
//算法演示控制按钮
private JButton runButton = new JButton();
private JButton stepButton = new JButton();
private JButton stayButton = new JButton();
private JButton helpButton = new JButton();
private JButton newButton = new JButton();
private JButton closeButton = new JButton();
private boolean flag1 = true;
private JButton musicButton = new JButton("播放音乐");
private Thread runner;
private Thread moving;
private boolean movFlag = false;
//创建显示算法伪代码的类变量codePanel
CodeAnimationPanel codePanel;
//控制演示速度
private GlobalControls controlsPanel;
//runFlag用来控制线程runner的执行,runFlag为true时执行,为false时不执行
private boolean runFlag = false;
private int controls;
private BorderLayout borderLayout1 = new BorderLayout();
private BorderLayout borderLayout2 = new BorderLayout();
private BorderLayout borderLayout3 = new BorderLayout();
private BorderLayout borderLayout4 = new BorderLayout();
private JPanel centerPanel = new JPanel();
private JPanel jPanel1 = new JPanel();
//面板jPanel2用于添加显示算法伪代码的面板和显示变量变化的面板
private JPanel jPanel2 = new JPanel();
//面板jPanel3用于添加显示变量变化的文本框
private JPanel jPanel3 = new JPanel();
private GridLayout gridLayout = new GridLayout(5, 2);
private GridLayout gridLayout1 = new GridLayout(1, 1);
//创建堆栈对象OPTR用于存储表达式的运算符,可以方便实现栈的各种操作
private Stack OPTR = new Stack();
//创建堆栈对象OPEN用于存储表达式的操作数和运算结果,可以方便实现栈的各种操作
private Stack OPEN = new Stack();
//数组exp[]用于保存输入的表达式
private String exp[] = new String[80];
//record用于记录数组exp[]的下标,
private int record = 0;
//数组operator[]用于保存表达式中的运算符和界限符
private String operator[] = new String[40];
//oper用于记录数组operator[]的下标,
private int oper = 0;
private int flag = 0;
//result用于保存输入的表达式的运算结果
private float result;
private int oldFlag = 0;
private int newFlag = 0;
//str用于保存输入的表达式字符串表示
private String str = null;
private int tag = 0;
private Point basePoint = new Point(15, 70);
//以下各文本框用于显示变量的变化情况
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private JTextField jTextField5 = new JTextField();
private JTextField jTextField6 = new JTextField();
private JTextField jTextField7 = new JTextField();
private JTextField jTextField8 = new JTextField();
private JTextField jTextField9 = new JTextField();
private JTextField jTextField10 = new JTextField();
private boolean mode = false;
//jPanel4面板用于添加输入表达式的文本框和按钮
private JPanel jPanel4 = new JPanel();
private JLabel jLabel1 = new JLabel(" 请输入表达式 ");
private JTextField expJTextField = new JTextField();
private JButton confirmButton = new JButton();
//jPanel4面板用于添加演示栈操作的面板和显示操作结果和当前操作信息的面板
private JPanel jPanel5 = new JPanel();
private GridLayout gridLayout2 = new GridLayout(1, 3, 2, 1);
//jPanel51面板用于添加显示操作结果和当前操作信息的面板
private JPanel jPanel51 = new JPanel();
private GridLayout gridLayout3 = new GridLayout(4, 1);
private JLabel jLabel2 = new JLabel("结果:");
private JLabel jLabel3 = new JLabel("");
private JLabel jLabel4 = new JLabel("栈操作记录:");
private JLabel optrTop = new JLabel(" top");
private JLabel optrJLabel[];
private JLabel optrLabel = new JLabel(" 操作符栈");
private JLabel openJLabel[];
private JLabel openTop = new JLabel(" top");
private JLabel optrRim[] = new JLabel[3];
private JLabel openRim[] = new JLabel[3];
private JLabel openLabel = new JLabel(" 操作数栈");
//记录操作符栈中标签的个数
private int optrCount = 0;
//记录操作数栈中标签的个数
private int openCount = 0;
//变量codePart用来控制源代码的同步显示,初始化为0
private int codePart;
//变量doneFlag用于标志演示是否完成,初始化为false,演示结束为true
private boolean doneFlag = false;
//temporary用于保存表达式数组的当前字符
private String temporary = null;
private String theta = null;
private float a = 0;
private float b = 0;
//文本框recordwork用于保存栈的操作步骤
private TextArea recordwork;
public expFrame(Root root) {
super("表达式求值算法演示窗口 (南昌航空工业学院-软件学院)", root);
init();
}
public void init() {
addWindowListener(new WindowClose(this, false));
super.initBase();
String as[] = {
"OperandType EvaluateExpression( ) { // OP为运算符集合",
" InitStack( OPTR ) ; Push(OPTR,'#') // OPET为运算符栈",
" InitStack( OPND ) ; c = getchar(); // 设OPEN运算数栈",
" while( c ! = '#' || GetTop(OPTR) ! =' #' ) {",
" if( !In( c,OP ) ) { // 不是运算符则进栈",
" Push(OPND,c) ; c = getchar(); }",
" else ",
" switch( Precede(GetTop(OPTR),c) ) ) {",
" case ' < ' : // 栈顶元素优先权低 ",
" Push( OPTR , c ) ; c = getchar() ; break;",
" case ' = ' : // 脱括号并接收下一个字符",
" Pop(OPTR,GetTop(OPTR) ); c = getchar() ; break;",
" case ' > ' : // 退栈并将运算结果入栈 ",
" Pop( OPND , b ) ;",
" Pop( OPND , a ) ;",
" Pop( OPTR , theta )",
" Push( OPnD , Operate( a,theta,b ) ) ; break ; }",
" }",
" return GetTop( OPND ) ; } // 返回运算结果"}; //," }"};
codePanel = new CodeAnimationPanel(as);
this.setBackground(Color.lightGray);
recordwork = new TextArea(10, 200);
newButton.setToolTipText("建立演示数据");
newButton.setText("新 建");
newButton.addActionListener(this);
helpButton.setToolTipText("查看帮助");
helpButton.setText("帮 助");
helpButton.addActionListener(this);
stayButton.setToolTipText("暂停演示");
stayButton.setText("暂 停");
stayButton.addActionListener(this);
stepButton.setToolTipText("单步执行演示 ");
stepButton.setText("单 步");
stepButton.addActionListener(this);
runButton.setToolTipText("执行演示");
runButton.setText("执行");
runButton.addActionListener(this);
runButton.setEnabled(false);
stepButton.setEnabled(false);
closeButton = new JButton("退 出");
closeButton.setToolTipText("退出演示窗口");
closeButton.addActionListener(this);
musicButton.addActionListener(this);
controlsPanel = new GlobalControls();
confirmButton.setToolTipText("输入表达式完成后单击");
confirmButton.setText(" 确定 ");
confirmButton.addActionListener(this);
jTextField1.setText(" 变 量 名");
jTextField2.setText(" 变 量 当 前 值");
jTextField3.setText(" c");
jTextField5.setText(" a");
jTextField7.setText(" b");
jTextField9.setText(" theta");
jTextField1.setEditable(false);
jTextField2.setEditable(false);
jTextField3.setEditable(false);
jTextField4.setEditable(false);
jTextField5.setEditable(false);
jTextField6.setEditable(false);
jTextField7.setEditable(false);
jTextField8.setEditable(false);
jTextField9.setEditable(false);
jTextField10.setEditable(false);
expJTextField.setToolTipText("请输入正确的表达式单击确定");
jPanel3.setLayout(gridLayout);
jPanel1.setLayout(null);
jPanel2.setLayout(borderLayout3);
jPanel4.setLayout(borderLayout4);
jPanel5.setLayout(null);
jPanel5.setBackground(Color.lightGray);
jPanel4.setBackground(Color.lightGray);
jPanel51.setBackground(Color.lightGray);
jPanel1.setBackground(Color.lightGray);
jPanel2.setBackground(Color.lightGray);
centerPanel.setLayout(borderLayout1);
jPanel4.add(jLabel1, BorderLayout.WEST);
jPanel4.add(expJTextField, BorderLayout.CENTER);
jPanel4.add(confirmButton, BorderLayout.EAST);
jLabel3.setForeground(Color.red);
jPanel5.add(jPanel51);
for (int m = 0; m < optrRim.length; m++) {
optrRim[m] = new JLabel();
optrRim[m].setBackground(new Color(24, 130, 54));
optrRim[m].setOpaque(true);
if (m == 0) {
optrRim[m].setSize(2, 240);
optrRim[m].setLocation(basePoint.x, basePoint.y);
}
else if (m == 1) {
optrRim[m].setSize(100, 2);
optrRim[m].setLocation(basePoint.x, basePoint.y + 240);
}
else {
optrRim[m].setSize(2, 240);
optrRim[m].setLocation(basePoint.x + 98, basePoint.y);
}
jPanel5.add(optrRim[m]);
}
optrLabel.setSize(100, 25);
optrLabel.setLocation(basePoint.x, basePoint.y + 250);
jPanel5.add(optrLabel);
for (int n = 0; n < openRim.length; n++) {
openRim[n] = new JLabel();
openRim[n].setBackground(new Color(110, 140, 60));
openRim[n].setOpaque(true);
if (n == 0) {
openRim[n].setSize(2, 240);
openRim[n].setLocation(basePoint.x + 120, basePoint.y);
}
else if (n == 1) {
openRim[n].setSize(100, 2);
openRim[n].setLocation(basePoint.x + 120, basePoint.y + 240);
}
else {
openRim[n].setSize(2, 240);
openRim[n].setLocation(basePoint.x + 218, basePoint.y);
}
jPanel5.add(openRim[n]);
}
openLabel.setSize(100, 25);
openLabel.setLocation(basePoint.x + 120, basePoint.y + 250);
jPanel5.add(openLabel);
optrTop.setBackground(new Color(20, 154, 78));
openTop.setBackground(new Color(50, 124, 50));
openTop.setOpaque(false);
optrTop.setOpaque(false);
openTop.setSize(94, 20);
optrTop.setSize(94, 20);
optrTop.setVisible(false);
openTop.setVisible(false);
jPanel5.add(optrTop);
jPanel5.add(openTop);
jPanel51.setSize(150, 280);
jPanel51.setLocation(250, basePoint.y - 10);
jPanel51.setLayout(gridLayout3);
jPanel51.add(jLabel2);
jPanel51.add(jLabel3);
jPanel51.add(jLabel4);
jPanel51.add(recordwork);
jPanel4.setSize(350, 30);
jPanel4.setLocation(0, 15);
jPanel5.setSize(350, 400);
jPanel5.setLocation(0, 45);
jPanel1.add(jPanel4);
jPanel1.add(jPanel5);
controlsPanel.delay = 900;
centerPanel.add(jPanel1, BorderLayout.CENTER);
centerPanel.add(jPanel2, BorderLayout.EAST);
jPanel2.add(jPanel3, BorderLayout.CENTER);
jPanel2.add(codePanel, BorderLayout.NORTH);
jPanel3.add(jTextField1);
jPanel3.add(jTextField2);
jPanel3.add(jTextField3);
jPanel3.add(jTextField4);
jPanel3.add(jTextField5);
jPanel3.add(jTextField6);
jPanel3.add(jTextField7);
jPanel3.add(jTextField8);
jPanel3.add(jTextField9);
jPanel3.add(jTextField10);
codePanel.highlight(2);
super.controlPanel.add(newButton);
super.controlPanel.add(runButton);
super.controlPanel.add(stepButton);
super.controlPanel.add(stayButton);
super.controlPanel.add(helpButton);
super.controlPanel.add(closeButton);
super.controlPanel.add(musicButton);
this.setBackground(Color.lightGray);
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
}
//函数Base()用于单击新建后完成一些变量值的复原为初始值
public void Base() {
this.movFlag = false;
flag = 0;
runFlag = false;
tag = 0;
doneFlag = false;
optrTop.setVisible(false);
openTop.setVisible(false);
for (int i = 0; i <= optrCount; i++)
optrJLabel[i].setVisible(false);
for (int j = 0; j <= openCount; j++)
openJLabel[j].setVisible(false);
optrCount = 0;
openCount = 0;
codePart = 0;
jLabel3.setText("");
codePanel.highlight(2);
jLabel3.setText("");
//jLabel5.setText("");
jTextField4.setText("");
jTextField6.setText("");
jTextField8.setText("");
jTextField10.setText("");
expJTextField.setEditable(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -