📄 buildwindow.java
字号:
package llanguage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import scanner.Scanner;
import gen.Gen;
public class BuildWindow extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private final int WINWIDTH = 800; // 窗口大小
private final int WINHEIGHT = 600;
private JPanel jPanelForTa = new JPanel(); // 用于包装taPro,taSpace,taInfo的JPanel容器
private JPanel jPanelForButton = new JPanel(); // 用于包装Button的JPanel容器
private JScrollPane jspP;
private JScrollPane jspI;
private JTextArea taProName; // 程序输入区
private JTextArea taSpace; // 用于隔开taPro和taInfo
private JTextArea taInfo; // 用于显示编译结果
private JButton scanner; // 五个按钮
private JButton parser;
private JButton attr;
private JButton gen;
private JButton open;
FileDialog fileDialog; // 询问方式获取文件名
String filePath;
String fileName="";
void init() { // 初始化
this.setSize(WINWIDTH, WINHEIGHT); // 设置窗口大小
this.setTitle("Compiler of Language L");
this.setLayout(new BorderLayout());
this.setResizable(false);
jPanelForTa = new JPanel(); // 实例化
jPanelForButton = new JPanel();
taProName = new JTextArea("", 10, 40); // 文本区实例化
taSpace = new JTextArea("Information of this programe", 1, 60);
taInfo = new JTextArea("", 18, 40);
scanner = new JButton("Scanner"); // 按钮实例化
parser = new JButton("Parser");
parser.setEnabled(false);
attr = new JButton("Attr");
attr.setEnabled(false);
gen = new JButton("Gen");
open = new JButton("Open");
jPanelForTa.setLayout(new BorderLayout()); // 设置相应参数
taProName.setBackground(new Color(220, 255, 220));
taProName.setEditable(false);
taSpace.setBackground(new Color(200, 200, 200));
taSpace.setEditable(false);
taInfo.setBackground(new Color(220, 255, 220));
taInfo.setEditable(false);
jspP = new JScrollPane(taProName); // 滚动条
jspI = new JScrollPane(taInfo);
jPanelForButton.add(scanner); // 添加实例
jPanelForButton.add(parser);
jPanelForButton.add(attr);
jPanelForButton.add(gen);
jPanelForButton.add(open);
jPanelForTa.add(jspP, "North");
jPanelForTa.add(taSpace, "Center");
jPanelForTa.add(jspI, "South");
this.add(jPanelForTa, "North");
this.add(jPanelForButton, "South");
this.setVisible(true);
scanner.addActionListener(this); // 按钮添加监听事件
parser.addActionListener(this);
attr.addActionListener(this);
gen.addActionListener(this);
open.addActionListener(this);
fileDialog = new FileDialog(this, "选择文件");
}
public JTextArea gettaProName() {
return taProName;
}
public JTextArea gettaInfo() {
return taInfo;
}
public String getFilePath() {
return filePath;
}
public String getFileName() {
return fileName;
}
public void actionPerformed(ActionEvent arg) {
if (arg.getSource() == open) {
try {
fileDialog.setVisible(true);
filePath = fileDialog.getDirectory();
fileName = fileDialog.getFile();
if (!fileName.endsWith(".txt")) {
JOptionPane.showMessageDialog(null,
"Please choose a text file");
return;
}
taProName.append("You have chosen this file: " + filePath
+ fileName + "\n");
} catch (NullPointerException npe) {
}
}
if (arg.getSource() == scanner) {
Scanner s = new Scanner();
s.startScanner();
}
if (arg.getSource() == parser) {
}
if (arg.getSource() == attr) {
}
if (arg.getSource() == gen) {
Gen g=new Gen();
g.startGen();
}
}
private static BuildWindow bw;
static{
bw=new BuildWindow();
}
public static BuildWindow getBuildWindow()
{
return bw;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -