📄 compileframe.java
字号:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class CompileFrame extends JFrame
implements ActionListener
{
final int COLUMN = 15;
final int LINE = 70;
JMenuBar menuBar;
JMenu fileMenu,operationMenu,toolMenu,helpMenu;
JMenuItem openItem,saveItem,compileItem,findErrorItem,copyItem,cutItem,pasteItem,helpItem,introduceItem,middleCodeItem;
JLabel textLabel;
JPanel textPanel;
public static TextArea textArea;
JPanel buttonPanel;
JPanel buttonlabelPanel;
JLabel buttonLabel;
JButton compileButton;
JButton findErrorButton;
JButton cutButton;
JButton copyButton;
JButton pasteButton;
JLabel errorLabel;
JPanel errorPanel;
public static TextArea errorArea;
FileDialog openDialog;
FileDialog saveDialog;
byte buffer[];
int bufferLength;
Clipboard clipboard = null;
WordAnalyze wordAnalyze;
ParsingMethod parsingMethod;
MiddleCode middleCode;
HelpFrame helpFrame;
IntroduceFrame introduceFrame;
MiddleCodeFrame middleCodeFrame;
public CompileFrame()
{
setTitle("COMPILER");
helpFrame = new HelpFrame();
introduceFrame = new IntroduceFrame();
middleCodeFrame = new MiddleCodeFrame();
buffer = new byte[1000000];
///////////////set the Menu///////////////
///////////MenuBar//////
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
////////////Menu////////
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenu operationMenu = new JMenu("Operation");
operationMenu.setMnemonic('O');
JMenu toolMenu = new JMenu("Tool");
toolMenu.setMnemonic('T');
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
/////////MenuItem/////////
JMenuItem openItem = new JMenuItem("Open",'O');
openItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
JMenuItem saveItem = new JMenuItem("Save",'S');
saveItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
JMenuItem compileItem = new JMenuItem("Compile");
compileItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_R,ActionEvent.CTRL_MASK));
JMenuItem findErrorItem = new JMenuItem("FindError",'F');
findErrorItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_F,ActionEvent.CTRL_MASK));
JMenuItem copyItem = new JMenuItem("Copy",'C');
copyItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
JMenuItem cutItem = new JMenuItem("Cut");
cutItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
JMenuItem pasteItem = new JMenuItem("Paste");
pasteItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
JMenuItem helpItem = new JMenuItem("Help");
helpItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_H,ActionEvent.CTRL_MASK));
JMenuItem introduceItem = new JMenuItem("Introduce");
introduceItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_I,ActionEvent.CTRL_MASK));
JMenuItem middleCodeItem = new JMenuItem("MiddleCode");
middleCodeItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_M,ActionEvent.CTRL_MASK));
//////add MenuItem///////
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
operationMenu.add(compileItem);
operationMenu.addSeparator();
operationMenu.add(findErrorItem);
toolMenu.add(copyItem);
toolMenu.addSeparator();
toolMenu.add(cutItem);
toolMenu.addSeparator();
toolMenu.add(pasteItem);
helpMenu.add(helpItem);
helpMenu.add(introduceItem);
helpMenu.add(middleCodeItem);
menuBar.add(fileMenu);
menuBar.add(operationMenu);
menuBar.add(toolMenu);
menuBar.add(helpMenu);
/////button area/////
buttonlabelPanel = new JPanel();
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,5));
buttonLabel= new JLabel("keyboard shortcuts");
compileButton = new JButton("Compile");
findErrorButton = new JButton("FindError");
copyButton = new JButton("Copy");
cutButton = new JButton("Cut");
pasteButton = new JButton("Paste");
buttonPanel.add(compileButton);
buttonPanel.add(findErrorButton);
buttonPanel.add(copyButton);
buttonPanel.add(cutButton);
buttonPanel.add(pasteButton);
buttonlabelPanel.setLayout(new BorderLayout());
buttonlabelPanel.add(buttonLabel,BorderLayout.NORTH);
buttonlabelPanel.add(buttonPanel,BorderLayout.CENTER);
////error text//////////////
errorLabel = new JLabel("error area");
errorPanel = new JPanel(new BorderLayout());
errorArea = new TextArea(COLUMN/2,LINE);
errorArea.setEditable(false);
errorPanel.add(errorArea,BorderLayout.CENTER);
errorPanel.add(errorLabel,BorderLayout.NORTH);
///////text area////////
textLabel = new JLabel("TextArea:the place for your programme");
textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textArea = new TextArea(COLUMN,LINE);
textArea.setEditable(true);
textPanel.add(textArea,BorderLayout.CENTER);
textPanel.add(textLabel,BorderLayout.NORTH);
/////////clipboard////////////
clipboard=getToolkit().getSystemClipboard();
////////add into frame//////////
getContentPane().setLayout(new BorderLayout());
getContentPane().add(textPanel,BorderLayout.CENTER);
getContentPane().add(errorPanel,BorderLayout.SOUTH);
getContentPane().add(buttonlabelPanel,BorderLayout.NORTH);
//////////file dialog////////////////
openDialog = new FileDialog(this,"open",FileDialog.LOAD);
saveDialog = new FileDialog(this,"save",FileDialog.SAVE);
////////the part of wordanalyzing//////////
wordAnalyze = new WordAnalyze();
////////the part of ParsingMethod//////////////
parsingMethod = new ParsingMethod();
//////////////////////////////////////////////
middleCode = new MiddleCode();
///////add actionlistener//////////////
openItem.addActionListener(this);
saveItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
helpItem.addActionListener(this);
introduceItem.addActionListener(this);
middleCodeItem.addActionListener(this);
compileButton.addActionListener(this);
findErrorButton.addActionListener(this);
cutButton.addActionListener(this);
copyButton.addActionListener(this);
pasteButton.addActionListener(this);
///////add windowlistener////////////
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
///////////main/////////////
public static void main(String[] args)
{
CompileFrame compileFrame = new CompileFrame();
compileFrame.setVisible(true);
compileFrame.pack();
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
///////////the MenuItem/////////
if(e.getSource() instanceof JMenuItem)
{
if("Open".equals(actionCommand))
{
openDialog.setVisible(true);
try
{
File file = new File(openDialog.getDirectory(),openDialog.getFile());
FileInputStream fileInputStream = new FileInputStream(file);
bufferLength = fileInputStream.read(buffer);
fileInputStream.close();
}
catch(Exception e2)
{
}
String string = new String(buffer);
textArea.setText(string);
}
else
if("Save".equals(actionCommand))
{
saveDialog.setVisible(true);
try
{
File file = new File(saveDialog.getDirectory(),saveDialog.getFile());
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
String string = textArea.getText();
bufferWriter.write(string,0,string.length());
bufferWriter.flush();
fileWriter.close();
}
catch(Exception e2)
{
}
}
else
if("Copy".equals(actionCommand))
{
copy();
}
else
if("Cut".equals(actionCommand))
{
cut();
}
else
if("Paste".equals(actionCommand))
{
paste();
}
else
if("Compile".equals(actionCommand))
{
///////////////////////////////
errorArea.setText("");
wordAnalyze.wordAnalyzeMethod();
parsingMethod.parsingAnalyzeMethod();
}
else
if("FindError".equals(actionCommand))
{
}
else
if("Help".equals(actionCommand))
{
helpFrame.setVisible(true);
}
else
if("Introduce".equals(actionCommand))
{
introduceFrame.setVisible(true);
}
else
if("MiddleCode".equals(actionCommand))
{
middleCodeFrame.setVisible(true);
}
}
//////the button///////
else
if(e.getSource() instanceof JButton)
{
if("Compile".equals(actionCommand))
{
///////////////////////////////
errorArea.setText("");
wordAnalyze.wordAnalyzeMethod();
parsingMethod.parsingAnalyzeMethod();
/* if(parsingMethod.successFlag)
{
middleCode.middleCodeCreate();
}*/
/* if(parsingMethod.successFlag)
{
middleCode.middleCodeCreate();
/* try
{
File file = new File("help","middlecode.txt");
if(!file.exists())
{
System.err.println("middlecode.txt text is not existing");
}
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String string = MiddleCodeFrame.textArea.getText();
bufferedWriter.write(string,0,string.length());
bufferedWriter.flush();
fileWriter.close();
}
catch(Exception e3)
{
}
}*/
}
else
if("FindError".equals(actionCommand))
{
}
else
if("Copy".equals(actionCommand))
{
copy();
}
else
if("Cut".equals(actionCommand))
{
cut();
}
else
if("Paste".equals(actionCommand))
{
paste();
}
}
}
public void copy()
{
String string = textArea.getSelectedText();
StringSelection text = new StringSelection(string);
clipboard.setContents(text,null);
}
public void cut()
{
String string = textArea.getSelectedText();
StringSelection text = new StringSelection(string);
clipboard.setContents(text,null);
int startPosition = textArea.getSelectionStart();
int endPosition = textArea.getSelectionEnd();
textArea.replaceRange("",startPosition,endPosition);
}
public void paste()
{
Transferable cnt = clipboard.getContents(this);
DataFlavor flv =DataFlavor.stringFlavor;
if(cnt.isDataFlavorSupported(flv))
{
try
{
String string;
string = (String)cnt.getTransferData(flv);
textArea.append(string);
}
catch(Exception ee)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -