?? analyser.java
字號(hào):
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Analyser extends JFrame{
private JLabel inputLabel,outputLabel,imageLabel;
private JButton open,scan,save,clear,exit,help,about;
protected JTextArea inputArea,outputArea;
private JScrollPane input,output;
private Scaner scaner;
//構(gòu)造函數(shù),用于界面初始化
public Analyser()
{
//使用本機(jī)系統(tǒng)外觀(如果有一個(gè))的 LookAndFeel 類(lèi)的名稱(chēng)設(shè)置當(dāng)前的默認(rèn)外觀。
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {}
//設(shè)置菜單欄及其菜單項(xiàng)
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
//文件菜單欄及其菜單項(xiàng)
JMenu fileMenu=new JMenu("文件");
JMenuItem openItem=new JMenuItem("打開(kāi)");
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.openFile();
}
});
fileMenu.add(openItem);
JMenuItem saveItem=new JMenuItem("保存結(jié)果");
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.saveFile();
}
});
fileMenu.add(saveItem);
fileMenu.addSeparator(); //設(shè)置菜單項(xiàng)分隔線(xiàn)
JMenuItem exitItem=new JMenuItem("退出");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
bar.add(fileMenu);
//操作菜單欄及其菜單項(xiàng)
JMenu executeMenu=new JMenu("操作");
JMenuItem scanItem=new JMenuItem("掃描");
scanItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.scan();
}
});
executeMenu.add(scanItem);
JMenuItem clearItem=new JMenuItem("清空");
clearItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.clear();
}
});
executeMenu.add(clearItem);
bar.add(executeMenu);
//幫助菜單欄及其菜單項(xiàng)
JMenu helpMenu=new JMenu("幫助");
JMenuItem useItem=new JMenuItem("如何使用");
useItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.help();
}
});
helpMenu.add(useItem);
helpMenu.addSeparator(); //設(shè)置菜單項(xiàng)分隔線(xiàn)
JMenuItem aboutItem=new JMenuItem("關(guān)于...");
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.about();
}
});
helpMenu.add(aboutItem);
bar.add(helpMenu);
//設(shè)置布局為null
setLayout(null);
//按鈕組的設(shè)置
open=new JButton(new ImageIcon("image\\open.gif"));
open.setToolTipText("打開(kāi)文件");
scan=new JButton(new ImageIcon("image\\execute.gif"));
scan.setToolTipText("執(zhí)行掃描");
save=new JButton(new ImageIcon("image\\save.gif"));
save.setToolTipText("保存結(jié)果");
clear=new JButton(new ImageIcon("image\\clear.gif"));
clear.setToolTipText("清除屏幕");
exit=new JButton(new ImageIcon("image\\exit.gif"));
exit.setToolTipText("退出程序");
help=new JButton(new ImageIcon("image\\help.gif"));
help.setToolTipText("幫助");
about=new JButton(new ImageIcon("image\\about.gif"));
about.setToolTipText("關(guān)于");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.openFile();
}
});
open.setBounds(5,4,31,25);
add(open);
scan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.scan();
}
});
scan.setBounds(46,4,31,25);
add(scan);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.saveFile();
}
});
save.setBounds(87,4,29,26);
add(save);
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.clear();
}
});
clear.setBounds(126,4,31,25);
add(clear);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
exit.setBounds(167,4,31,25);
add(exit);
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.about();
}
});
about.setBounds(208,4,31,25);
add(about);
help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
scaner.help();
}
});
help.setBounds(249,8,48,18);
add(help);
//標(biāo)簽和文本區(qū)域的設(shè)置
imageLabel=new JLabel(new ImageIcon("image\\image.gif"));
imageLabel.setToolTipText("這是個(gè)用Java編寫(xiě)的詞法分析器");
imageLabel.setBounds(445,0,89,35);
add(imageLabel);
//頂部輸入?yún)^(qū)域的設(shè)置
inputLabel=new JLabel("輸入?yún)^(qū)域");
inputLabel.setFont(new Font("ScanSerif",Font.PLAIN,14));
inputLabel.setBounds(5,37,70,25);
add(inputLabel);
inputArea=new JTextArea(25,11);
inputArea.setForeground(new Color(50,30,230));
inputArea.setFont(new Font("Monospaced",Font.PLAIN,15));
input=new JScrollPane(inputArea);
input.setBounds(5,60,533,300);
add(input);
//底部輸出結(jié)果的設(shè)置
outputLabel=new JLabel("輸出結(jié)果");
outputLabel.setFont(new Font("ScanSerif",Font.PLAIN,14));
outputLabel.setBounds(5,363,70,25);
add(outputLabel);
outputArea=new JTextArea(25,11);
outputArea.setForeground(new Color(50,40,230));
outputArea.setFont(new Font("Monospaced",Font.PLAIN,14));
outputArea.setEditable(false);
output=new JScrollPane(outputArea);
output.setBounds(5,387,533,120);
add(output);
//程序顯示設(shè)置
setTitle("詞法分析器");
setVisible(true);
setResizable(false);
setBounds(300,100,550,570);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate(); //當(dāng)重新添加新子控件時(shí),調(diào)用此方法來(lái)使添加生效,相當(dāng)于刷新
scaner=new Scaner(this); //為該程序添加控制器,控制器主要完成后臺(tái)工作
}
public static void main(String[] args)
{
new Analyser(); //啟動(dòng)程序
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -