📄 mainclass.java
字号:
/**
* this is the main class
* @author:贺静
* @version: 1.2
*/
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.LookAndFeel;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JMenuItem;
import javax.swing.JCheckBox;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class MainClass extends JFrame implements ActionListener, MouseListener, ItemListener, DocumentListener {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JSplitPane jSplitPane = null;
private JScrollPane leftScrollPane = null;
private JTextArea inputTextArea, outTextArea;
private JScrollPane rightScrollPane = null;
private JPanel jPanel = null;
private JButton openButton, scanButton;
private JFileChooser fileChooser = new JFileChooser();
private JMenu jMenu = null;
private JMenuBar clearBar = null;
private JMenuItem inputItem, outputItem, allItem, pm1, pm2, pm3;
private JPopupMenu pm = new JPopupMenu();
private JCheckBox jCheckBox = null;
private MultilineLabel lineLabel=new MultilineLabel("");
private int lineNo;
// return javax.swing.JPanel
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), java.awt.BorderLayout.NORTH);
jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
// @return javax.swing.JSplitPane
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerLocation(330);
jSplitPane.setRightComponent(getRightScrollPane());
jSplitPane.setLeftComponent(getLeftScrollPane());
jSplitPane.setOneTouchExpandable(true);
}
return jSplitPane;
}
private JPanel getJPanel() {
if (jPanel == null) {
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
jPanel = new JPanel();
jPanel.setBackground(new java.awt.Color(224, 255, 255));
jPanel.setLayout(flowLayout);
jPanel.setPreferredSize(new java.awt.Dimension(10, 30));
jPanel.add(getOpenButton(), null);
jPanel.add(getScanButton(), null);
jPanel.add(getClearBar());
jPanel.add(getJCheckBox(), null);
}
return jPanel;
}
// return javax.swing.JMenuBar
private JMenuBar getClearBar() {
if (clearBar == null) {
clearBar = new JMenuBar();
clearBar.setBackground(new java.awt.Color(241, 255, 255));
clearBar.add(getJMenu());
}
return clearBar;
}
// return javax.swing.JScrollPane
private JScrollPane getLeftScrollPane() {
if (leftScrollPane == null) {
leftScrollPane = new JScrollPane();
leftScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("input"), BorderFactory
.createEmptyBorder()));
leftScrollPane.setBackground(new java.awt.Color(239, 255, 255));
lineLabel.setVisible(false);
lineLabel.setFont(new java.awt.Font("Dialog",
java.awt.Font.BOLD, 14));
leftScrollPane.setRowHeaderView(lineLabel);
leftScrollPane.setViewportView(getInputTextArea());
}
return leftScrollPane;
}
// return javax.swing.JTextArea
private JTextArea getInputTextArea() {
if (inputTextArea == null) {
inputTextArea = new JTextArea();
inputTextArea.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 14));
inputTextArea.addMouseListener(this);
pm = new JPopupMenu();
pm1 = new JMenuItem("剪切");
pm1.setBackground(new java.awt.Color(241, 255, 255));
pm2 = new JMenuItem("复制");
pm2.setBackground(new java.awt.Color(241, 255, 255));
pm3 = new JMenuItem("粘贴");
pm3.setBackground(new java.awt.Color(241, 255, 255));
pm.add(pm1);
pm.add(pm2);
pm.add(pm3);
inputTextArea.add(pm);
}
return inputTextArea;
}
// return javax.swing.JScrollPane
private JScrollPane getRightScrollPane() {
if (rightScrollPane == null) {
rightScrollPane = new JScrollPane();
rightScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("output"), BorderFactory
.createEmptyBorder()));
rightScrollPane.setBackground(new java.awt.Color(239, 255, 255));
rightScrollPane.setViewportView(getOutTextArea());
}
return rightScrollPane;
}
// @return javax.swing.JTextArea
private JTextArea getOutTextArea() {
if (outTextArea == null) {
outTextArea = new JTextArea();
outTextArea.setFont(new java.awt.Font("Dialog",
java.awt.Font.PLAIN, 14));
outTextArea.setEditable(false);
}
return outTextArea;
}
// @return javax.swing.JButton
private JButton getOpenButton() {
if (openButton == null) {
openButton = new JButton();
openButton.setText("Open");
openButton.setBackground(new java.awt.Color(241, 255, 255));
openButton.setFont(new java.awt.Font("DialogInput",
java.awt.Font.BOLD, 12));
openButton.setBorderPainted(false);
}
return openButton;
}
// return javax.swing.JButton
private JButton getScanButton() {
if (scanButton == null) {
scanButton = new JButton();
scanButton.setText("Analyze");
scanButton.setBackground(new java.awt.Color(241, 255, 255));
scanButton.setBorderPainted(false);
}
return scanButton;
}
/**
* This method initializes jCheckBox
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBox() {
if (jCheckBox == null) {
jCheckBox = new JCheckBox();
jCheckBox.setText("LineNo");
jCheckBox.setBackground(new java.awt.Color(241, 255, 255));
jCheckBox.addItemListener(this);
}
return jCheckBox;
}
/**
* This is the default constructor
*/
public MainClass() {
super();
initialize();
}
/**
* This method initializes this
*/
private void initialize() {
this.setBackground(new java.awt.Color(238, 238, 238));
this.setBounds(new java.awt.Rectangle(160, 50, 673, 547));
this.setContentPane(getJContentPane());
this.setTitle("词法分析");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
openButton.addActionListener(this);
scanButton.addActionListener(this);
inputItem.addActionListener(this);
outputItem.addActionListener(this);
allItem.addActionListener(this);
pm1.addActionListener(this);
pm2.addActionListener(this);
pm3.addActionListener(this);
}
// return javax.swing.JMenu
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
// jMenu.setBackground(new java.awt.Color(241,255,255));
jMenu.setText("Clear");
jMenu.setFont(new java.awt.Font("DialogInput", java.awt.Font.BOLD,
12));
inputItem = new JMenuItem("Clear InputBox");
inputItem.setBackground(new java.awt.Color(241, 255, 255));
outputItem = new JMenuItem("Clear OutputBox");
outputItem.setBackground(new java.awt.Color(241, 255, 255));
allItem = new JMenuItem("Clear All");
allItem.setBackground(new java.awt.Color(241, 255, 255));
jMenu.add(inputItem);
jMenu.add(outputItem);
jMenu.add(allItem);
}
return jMenu;
}
public void openFile() {
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File fileName = fileChooser.getSelectedFile();
BufferedReader input = new BufferedReader(new FileReader(
fileName));
String line = input.readLine();
inputTextArea.setText("");
while (line != null) {
inputTextArea.append(line + "\n");
line = input.readLine();
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (NullPointerException Ex) {
System.out.println();
} catch (Exception ex) {
System.out.println();
}
}
}
public void changeLabel(){
if (inputTextArea.getLineCount() != lineNo) {
lineNo = inputTextArea.getLineCount();
String s = "";
for (int j = 1; j <= lineNo; j++) {
s += j + "\n";
}
lineLabel.setText(s);
}
}
//scan.setBufferInput(s);
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openButton) {
openFile();
} else if (e.getSource() == scanButton) {
String s = inputTextArea.getText();
if (s.equals("")) {
outTextArea
.setText("ERROR: Please input strings in the leftbox!");
} else {
LexAna scan = new LexAna();
scan.setBufferInput(s);
Parse parse=new Parse(scan);
try {
AST program=parse.parseAna();
outTextArea.setText(parse.getPrint(program)
+"\n"+parse.getErrorToken());
} catch (Exception e1) {
// outTextArea.setText("Exception");
e1.printStackTrace();
}
}
} else if (e.getSource() == inputItem)
inputTextArea.setText("");
else if (e.getSource() == outputItem)
outTextArea.setText("");
else if (e.getSource() == allItem) {
inputTextArea.setText("");
outTextArea.setText("");
} else if (e.getSource() == pm1) {
inputTextArea.cut();
} else if (e.getSource() == pm2) {
inputTextArea.copy();
} else if (e.getSource() == pm3)
inputTextArea.paste();
}
public void mouseReleased(MouseEvent e) {
try {
if (e.isPopupTrigger()) {
pm.show(this, e.getX(), e.getY());
}
} catch (NullPointerException ex) {
} catch (Exception exception) {
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public static void main(String[] args) {
new MainClass();
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==jCheckBox){
if(jCheckBox.isSelected()){
inputTextArea.getDocument().addDocumentListener(this);
lineNo=0;
changeLabel();
lineLabel.setVisible(true);
}else{
inputTextArea.getDocument().removeDocumentListener(this);
lineLabel.setText("");
lineLabel.setVisible(false);
}}
}
public void insertUpdate(DocumentEvent e) {
changeLabel();
}
public void removeUpdate(DocumentEvent e) {
changeLabel();
}
public void changedUpdate(DocumentEvent e) {
changeLabel();
}
}
//create a label supporting multiline by extending JTextArea
class MultilineLabel extends JTextArea {
private static final long serialVersionUID = (long) 1.2;
public MultilineLabel(String s) {
super(s);
}
public void updateUI() {
super.updateUI();
setWrapStyleWord(true);
setHighlighter(null);
setEditable(false);
LookAndFeel.installBorder(this, "Label.border");
LookAndFeel.installColorsAndFont(this, "Label.background",
"Label.foreground", "Label.font");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -