Java程序编写记事本方法大全
- JAVA认证
- 关注:4.51K次
java语言可以实现很多程序,那么用java怎么编写记事本呢?下面跟yjbys小编一起来看看吧!
import .*;
import t.*;
import g.*;
import .*;
public class Notepad extends JFrame {
// 系统组件声明
private JMenuBar menuBar = new JMenuBar();
private JEditorPane content = new JEditorPane();
private JScrollPane scroll = new JScrollPane(content);
private JFileChooser filechooser = new JFileChooser() ;
private BorderLayout bord = new BorderLayout();
private JLabel statusBar = new JLabel();
private JPanel pane = new JPanel();
private File file = null;
// 定义文件菜单
private JMenu fileMenu = new JMenu();
private JMenuItem newMenuItem = new JMenuItem();
private JMenuItem openMenuItem = new JMenuItem();
private JMenuItem saveMenuItem = new JMenuItem();
private JMenuItem saveAsMenuItem = new JMenuItem();
private JMenuItem pageSetupMenuItem = new JMenuItem();
private JMenuItem printMenuItem = new JMenuItem();
private JMenuItem exitMenuItem = new JMenuItem();
// 定义风格菜单
private JMenu styleMenu = new JMenu();
private ButtonGroup styleMenuGroup = new ButtonGroup();
private JRadioButtonMenuItem javaStyleMenuItem = new JRadioButtonMenuItem();
private JRadioButtonMenuItem metalStyleMenuItem = new JRadioButtonMenuItem();
private JRadioButtonMenuItem windowsStyleMenuItem = new JRadioButtonMenuItem();
// 定义帮助菜单
private JMenuItem aboutMenuItem = new JMenuItem();
private JMenuItem helpTopicMenuItem = new JMenuItem();
private JMenu helpMenu = new JMenu();
// 构造函数
public Notepad(){
initComponents();
}
private void initComponents(){
// 添加子菜单项到文件菜单
ext("/u6587/u4ef6 (F)");
ext(" 新建(N) Ctrl+N");
ext(" 打开(O)... Ctrl+O");
ext(" 保存(S) Ctrl+S");
ext(" 另存为(A)...");
ext(" 页面设置(U)...");
ext(" 打印(P)... Ctrl+P");
ext(" 退出");
(newMenuItem);
(openMenuItem);
(saveMenuItem);
(saveAsMenuItem);
eparator();
(pageSetupMenuItem);
(printMenuItem);
eparator();
(exitMenuItem);
// 添加子菜单项到风格菜单
ext("风格(S)");
ext("Java默认");
ext("Metal风格");
ext("Windows风格");
(javaStyleMenuItem);
(metalStyleMenuItem);
(windowsStyleMenuItem);
(javaStyleMenuItem);
(metalStyleMenuItem);
(windowsStyleMenuItem);
// 添加子菜单项到帮助菜单
ext("帮助(H)");
ext(" 帮助主题(H)");
ext(" 关于记事本(A)");
(helpTopicMenuItem);
eparator();
(aboutMenuItem);
// 定义文件菜单下的事件监听
ctionListener(new newMenuItem_actionAdapter(this));
ctionListener(new openMenuItem_actionAdapter(this));
ctionListener(new saveMenuItem_actionAdapter(this));
ctionListener(new saveAsMenuItem_actionAdapter(this));
ctionListener(new pageSetupMenuItem_actionAdapter(this));
ctionListener(new printMenuItem_actionAdapter(this));
ctionListener(new exitMenuItem_actionAdapter(this));
// 定义风格菜单下的`事件监听
ctionListener(new javaStyleMenuItem_actionAdapter(this));
ctionListener(new metalStyleMenuItem_actionAdapter(this));
ctionListener(new windowsStyleMenuItem_actionAdapter(this));
// 定义帮助菜单下的事件监听
ctionListener(new helpTopicMenuItem_actionAdapter(this));
ctionListener(new aboutMenuItem_actionAdapter(this));
// 填加菜单到菜单栏
(fileMenu);
(styleMenu);
(helpMenu);
// 对主窗口的一些设置
efaultCloseOperation(EXIT_ON_CLOSE);
itle("无标题 - /u8bb0/u4e8b/u672c");
ize(640,480);
setJMenuBar(menuBar);
ayout(bord);
("Center",scroll);
setContentPane(pane);
}
// 定义新建菜单项方法
public void newMenuItemActionPerformed(ActionEvent evt){
file = null;
if(!(""ls(ext()))){
Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };
int s = OptionDialog(null, "/u6587/u4ef6 "+getTitle()+" /u7684/u6587/u5b57/u5df2/u7ecf/u6539/u53d8/u3002/n/u60f3/u4fdd/u5b58/u6587/u4ef6/u5417/uff1f", "/u8bb0/u4e8b/u672c",
ULT_OPTION, ING_MESSAGE,
null, options, options[0]);
switch(s){
case 0:
int returnVal=SaveDialog(this);
if(returnVal == OVE_OPTION) {
file=electedFile();
try{
FileWriter fw=new FileWriter(file);
e(ext());
setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");
e();
}
catch(Exception e){
tStackTrace();
}
break;
}
case 1:
ext("");
setTitle("无标题 - /u8bb0/u4e8b/u672c");
}
}
}
// 定义打开菜单项方法
public void openMenuItemActionPerformed(ActionEvent evt){
try {
file = null;
int returnVal = OpenDialog(this);
if(returnVal == OVE_OPTION){
file = electedFile();
FileReader fr = new FileReader(file);
int len = (int)th();
char[] buffer = new char[len];
(buffer,0,len);
e();
ext(new String(buffer));
}
}
catch(Exception e){
tStackTrace();
}
}
// 定义退出菜单项方法
public void exitMenuItem_actionPerformed(ActionEvent e){
if(!(""ls(ext()))){
Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };
int s = OptionDialog(null, "文件的文字已经改变。/n想保存文件吗?", "/u8bb0/u4e8b/u672c",
ULT_OPTION, ING_MESSAGE,
null, options, options[0]);
switch(s){
case 0:
int returnVal=SaveDialog(this);
if(returnVal == OVE_OPTION) {
file=electedFile();
try{
FileWriter fw=new FileWriter(file);
e(ext());
setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");
e();
}
catch(Exception ex){
tStackTrace();
}
break;
}
case 1:
(0);
}
}
else{
(0);
}
}
// 保存事件
public void saveMenuItemActionPerformed(ActionEvent evt){
int returnVal=SaveDialog(this);
if(returnVal == OVE_OPTION) {
file=electedFile();
try{
FileWriter fw=new FileWriter(file);
e(ext());
setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");
e();
}
catch(Exception e){
tStackTrace();
}
}
}
// 另存为事件
public void saveAsMenuItemActionPerformed(ActionEvent evt){
ialogTitle("另存为...");
int returnVal = SaveDialog(this);
if(returnVal == OVE_OPTION) {
file=electedFile();
try{
FileWriter fw=new FileWriter(file);
e(ext());
setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");
e();
}
catch(Exception e){
tStackTrace();
}
}
}
// 页面设置事件
public void pageSetupMenuItemActionPerformed(ActionEvent evt){
MessageDialog(null,"此功能正在开发中...");
}
// 打印事件
public void printMenuItemActionPerformed(ActionEvent evt){
MessageDialog(null,"打印中...");
}
// 更新风格外观方法
void changeLookFeel(String className) {
try {
ookAndFeel(className);
}
catch (Exception e) {
tln(e);
}
teComponentTreeUI(this);
}
// Java风格事件
public void javaStyleMenuItemActionPerformed(ActionEvent evt){
changeLookFeel("lLookAndFeel");
}
// Motif风格事件
public void metalStyleMenuItemActionPerformed(ActionEvent evt){
changeLookFeel("fLookAndFeel");
}
// MAC风格事件
public void windowsStyleMenuItemActionPerformed(ActionEvent evt){
changeLookFeel("owsLookAndFeel");
}
// 帮助事件
public void helpTopicMenuItemActionPerformed(ActionEvent evt){
MessageDialog(null,"/u9700/u8981/u5e2e/u52a9/u5417/uff1f");
}
// 关于事件
public void aboutMenuItemActionPerformed(ActionEvent evt){
MessageDialog(null,"/n/u7a0b/u5e8f/u540d/u79f0/uff1aJava /u8bb0/u4e8b/u672c/n/u4ee3/u7801/u7f16/u5199/uff1aCavien/n/u4f5c/u8005/u7f51/u7ad9/uff1ahttp:/@");
}
// 主函数
public static void main(String args[]) {
Notepad notepad = new Notepad();
isible(true);
}
}
// 定义新建事件类
class newMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
newMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
enuItemActionPerformed(evt);
}
}
// 定义打开事件类
class openMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
openMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
MenuItemActionPerformed(evt);
}
}
// 定义保存事件类
class saveMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
saveMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
MenuItemActionPerformed(evt);
}
}
// 定义另存为事件类
class saveAsMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
saveAsMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
AsMenuItemActionPerformed(evt);
}
}
// 定义页面设置事件类
class pageSetupMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
pageSetupMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
SetupMenuItemActionPerformed(evt);
}
}
// 定义打印事件类
class printMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
printMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
tMenuItemActionPerformed(evt);
}
}
// 定义Java风格事件类
class javaStyleMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
javaStyleMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
StyleMenuItemActionPerformed(evt);
}
}
// 定义Java风格事件类
class metalStyleMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
metalStyleMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
lStyleMenuItemActionPerformed(evt);
}
}
// 定义Java风格事件类
class windowsStyleMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
windowsStyleMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
owsStyleMenuItemActionPerformed(evt);
}
}
// 定义帮助主题事件类
class helpTopicMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
helpTopicMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
TopicMenuItemActionPerformed(evt);
}
}
// 定义关于软件事件类
class aboutMenuItem_actionAdapter implements ActionListener{
Notepad adaptee;
aboutMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
tMenuItemActionPerformed(evt);
}
}
// 定义退出事件类
class exitMenuItem_actionAdapter implements ActionListener {
Notepad adaptee;
exitMenuItem_actionAdapter(Notepad adaptee){
tee = adaptee;
}
public void actionPerformed(ActionEvent evt){
MenuItem_actionPerformed(evt);
}
- 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/itrz/java/lwp6xo.html