📄 notepad.java
字号:
{
public void actionPerformed(ActionEvent e)
{
try
{
String[] args = new String[2];
args[0] = "java";
args[1] = "source.Compiler";
csta.setText("***三地址代码:***\n");
console.getOutput(args);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(transItem,
ex.toString() + ":\n没有可翻译文件!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//编译java菜单项
compileItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
fileName = JOptionPane.showInputDialog(compileItem,
"要编译的java文件", "编译", JOptionPane.PLAIN_MESSAGE);
String[] args = new String[4];
args[0] = "javac";
args[1] = "-d";
args[2] = ".";
args[3] = fileName;
csta.setText("***编译[" + fileName + "]文件***\n");
console.getOutput(args);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(compileItem,
ex.toString() + ":\n没有可编译文件!", "出错啦!",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
//运行java菜单项
runItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String classStr = JOptionPane.showInputDialog(runItem,
"要运行的java类", "运行", JOptionPane.PLAIN_MESSAGE);
String[] args = new String[2];
args[0] = "java";
args[1] = classStr;
csta.setText("***运行[" + classStr + "]类***\n");
console.getOutput(args);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(runItem,
ex.toString() + ":\n没有可运行文件!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//生成文档菜单项
docItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String classStr = JOptionPane.showInputDialog(docItem,
"要生成的java类文档", "生成文档", JOptionPane.PLAIN_MESSAGE);
String[] args = new String[2];
args[0] = "javadoc";
args[1] = classStr;
csta.setText("***生成[" + classStr + "]类文档***\n");
console.getOutput(args);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(docItem,
ex.toString() + ":\n没有可运行文件!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//运行小应用程序,小程序查看器运行
appletViewItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String appletStr = JOptionPane.showInputDialog(appletViewItem,
"要运行applet的文件", "运行", JOptionPane.PLAIN_MESSAGE);
if(!(appletStr.substring(appletStr.lastIndexOf(".") + 1).equalsIgnoreCase("java"))
&& !(appletStr.substring(appletStr.lastIndexOf(".") + 1).equalsIgnoreCase("html")))
throw new Exception();
Runtime.getRuntime().exec("appletviewer.exe " + appletStr);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(appletViewItem,
ex.toString() + ":\n没有可运行文件!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//运行小应用程序,浏览器运行
appletHtmlItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String appletStr = JOptionPane.showInputDialog(appletViewItem,
"要运行网页的文件", "运行", JOptionPane.PLAIN_MESSAGE);
if(!(appletStr.substring(appletStr.lastIndexOf(".") + 1).equalsIgnoreCase("htm"))
&& !(appletStr.substring(appletStr.lastIndexOf(".") + 1).equalsIgnoreCase("html")))
throw new Exception();
Runtime.getRuntime().exec("cmd.exe /C explorer.exe " + appletStr);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(appletHtmlItem,
ex.toString() + ":\n没有可运行文件!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//java常用命令查询
commandItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String[] cmdStr = {"appletviewer", "apt", "extcheck", "HtmlConverter",
"idlj", "jar", "jarsigner", "java", "javac", "javadoc", "javah",
"javap", "javaw", "javaws", "jconsole", "jdb", "jps", "jstat",
"jstatd", "keytool", "kinit", "klist", "ktab", "native2ascii",
"orbd", "pack200", "packager", "policytool", "rmic", "rmid",
"rmiregistry", "serialver", "servertool", "tnameserv", "unpack200"};
String commondStr = (String)JOptionPane.showInputDialog(commandItem,
"需要查找的命令", "Java命令查询", JOptionPane.PLAIN_MESSAGE,
null, cmdStr, "java");
String[] args = new String[2];
args[0] = commondStr;
args[1] = "-h";
csta.setText("***查询[" + commondStr + "]命令***\n");
console.getOutput(args);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(appletHtmlItem,
ex.toString() + ":\n没有可运行文件!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//打开命令提示符菜单
cmdItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Runtime.getRuntime().exec("cmd.exe /K start cmd.exe");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(cmdItem,
ex.toString() + ":\n运行时错误!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
toolsMenu.add(transItem);
toolsMenu.add(compileItem);
toolsMenu.add(runItem);
toolsMenu.add(docItem);
appletRun.add(appletViewItem);
appletRun.add(appletHtmlItem);
toolsMenu.add(appletRun);
toolsMenu.add(commandItem);
toolsMenu.add(cmdItem);
menuBar.add(toolsMenu);
//只读模式
readOnlyModeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setFont(new Font("宋体", Font.BOLD, 16));
jta.setEditable(false);
readOnlyModeItem.setEnabled(false);
readWriteModeItem.setEnabled(true);
popCopyItem.setEnabled(false);
popPasteItem.setEnabled(false);
popCutItem.setEnabled(false);
popDelItem.setEnabled(false);
popSubSingleItem.setEnabled(false);
popSubWholeItem.setEnabled(false);
copyItem.setEnabled(false);
pasteItem.setEnabled(false);
cutItem.setEnabled(false);
deleteItem.setEnabled(false);
subSingleItem.setEnabled(false);
subWholeItem.setEnabled(false);
toLowItem.setEnabled(false);
toUpItem.setEnabled(false);
popUpLowItem.setEnabled(false);
upLowItem.setEnabled(false);
popToLowItem.setEnabled(false);
popToUpItem.setEnabled(false);
}
});
//可编辑模式
readWriteModeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setFont(new Font("宋体", Font.PLAIN, 16));
jta.setEditable(true);
readOnlyModeItem.setEnabled(true);
readWriteModeItem.setEnabled(false);
popCopyItem.setEnabled(true);
popPasteItem.setEnabled(true);
popCutItem.setEnabled(true);
popDelItem.setEnabled(true);
popSubSingleItem.setEnabled(true);
popSubWholeItem.setEnabled(true);
copyItem.setEnabled(true);
pasteItem.setEnabled(true);
cutItem.setEnabled(true);
deleteItem.setEnabled(true);
subSingleItem.setEnabled(true);
subWholeItem.setEnabled(true);
toLowItem.setEnabled(true);
toUpItem.setEnabled(true);
popUpLowItem.setEnabled(true);
upLowItem.setEnabled(true);
popToLowItem.setEnabled(true);
popToUpItem.setEnabled(true);
}
});
//系统配置菜单项
configItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
config.setVisible(true);
config.systemStyleChange(Notepad.this);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(configItem,
ex.toString() + ":\n运行时错误!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
advancedMenu.add(readOnlyModeItem);
advancedMenu.add(readWriteModeItem);
advancedMenu.add(configItem);
menuBar.add(advancedMenu);
//复制弹出菜单项
popCopyItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
pasteItem.setEnabled(true);
popPasteItem.setEnabled(true);
}
});
//粘贴弹出菜单项
popPasteItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
});
//剪切弹出菜单项
popCutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
pasteItem.setEnabled(true);
popPasteItem.setEnabled(true);
}
});
//删除弹出菜单项
popDelItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
pasteItem.setEnabled(false);
popPasteItem.setEnabled(false);
}
});
//全选弹出菜单项
popSelectItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
}
});
//查找弹出菜单项
popFindItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String tempWord = JOptionPane.showInputDialog(popFindItem,
"要查找的单词:", "查找", JOptionPane.PLAIN_MESSAGE);
String content = jta.getText();
int findLength = tempWord.length();
int conLength = content.length();
if(findWord.equals(tempWord))
{
if((start <= conLength - findLength) && (start >=0))
{
start = content.indexOf(findWord, start);
if(start == -1)
throw new Exception();
jta.select(start, start + findLength);
start = start + findLength;
}
}
else
{
start = 0;
findWord = tempWord;
start = content.indexOf(findWord, start);
if(start == -1)
throw new Exception();
jta.select(start, start + findLength);
start = start + findLength;
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(popFindItem,
ex.toString() + ":\n没有找到!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//替换单个弹出菜单项
popSubSingleItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = "";
String substitudeStr = "";
try
{
if(textTemp.substring(selectStart, selectEnd).equals(""))
{
sourceStr = JOptionPane.showInputDialog(popSubSingleItem,
"要替换的文本:", "替换第一个", JOptionPane.PLAIN_MESSAGE);
substitudeStr = JOptionPane.showInputDialog(popSubSingleItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.replaceFirst(sourceStr, substitudeStr);
jta.setText(textTemp);
int length = substitudeStr.length();
selectStart = textTemp.indexOf(substitudeStr);
selectEnd = selectStart + length;
jta.select(selectStart, selectEnd);
}
else
{
sourceStr = textTemp.substring(selectStart, selectEnd);
substitudeStr = JOptionPane.showInputDialog(popSubSingleItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.substring(0, selectStart) +
substitudeStr + textTemp.substring(selectEnd);
jta.setText(textTemp);
int length = substitudeStr.length();
selectEnd = selectStart + length;
jta.select(selectStart, selectEnd);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(popSubSingleItem,
ex.toString() + ":\n输入错误!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//全部替换弹出菜单项
popSubWholeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = "";
String substitudeStr = "";
try
{
if(textTemp.substring(selectStart, selectEnd).equals(""))
{
sourceStr = JOptionPane.showInputDialog(popSubWholeItem,
"要替换的文本:", "全部替换", JOptionPane.PLAIN_MESSAGE);
}
else
{
sourceStr = textTemp.substring(selectStart, selectEnd);
}
substitudeStr = JOptionPane.showInputDialog(popSubWholeItem,
"替换为文本:", "替换为", JOptionPane.PLAIN_MESSAGE);
textTemp = textTemp.replace(sourceStr, substitudeStr);
jta.setText(textTemp);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(popSubWholeItem,
ex.toString() + ":\n输入错误!",
"出错啦!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
//转换为大写弹出菜单项
popToUpItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String textTemp = jta.getText();
int selectStart = jta.getSelectionStart();
int selectEnd = jta.getSelectionEnd();
String sourceStr = textTemp.substring(selectStart, selectEnd);
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -