记事本算是一个小项目,对于新人来说,是适合不过的,既用不到网络编程,也永不到数据库的编程,也暂时不管国际化等!
项目分析:主类继承了JFrame类,是程序的入口点,这个类放入了菜单栏,状态栏和文本的编辑区域,构造方法里面初始化了菜单的各各按钮。并且定义了事件的抽象数组,这些抽象的数组分别各自定义,这些类都继承了AbstratAction,实现具体的事件处理!
需要注意的地方:由于我们需要在各个类里面处理事件,用到MyPad里面的属性,或者说是用到里面的变量!因此,我们的每个事件处理类都把MyPad这个类做为其属性,在构造方法时候作为参数传过去!
-
package cn.com.shizongger;
-
-
import java.awt.BorderLayout;
-
import java.awt.Color;
-
import java.awt.Container;
-
import java.awt.Font;
-
import java.awt.event.WindowEvent;
-
import java.awt.event.WindowListener;
-
-
import javax.swing.*;
-
import cn.com.action.*;
-
-
public class MyPad extends JFrame {
-
-
public JTextPane textPane = new JTextPane(); //文本窗格,编辑窗口
-
-
public JLabel statusBar = new JLabel(); //状态栏
-
-
public JFileChooser fileChooser = new JFileChooser(); //文件选择器
-
-
Action[] actions = null;
-
-
//构造方法
-
public MyPad() {
-
super("记事本1.0版本");
-
-
//设置文本的字体和文字大小
-
textPane.setBackground(Color.LIGHT_GRAY);
-
textPane.setForeground(Color.ORANGE);
-
textPane.setFont(new Font("TimesRoman", Font.PLAIN, 18));
-
-
Action[] actions = { //Action数组,各种操作的命令
-
new NewAction(this),
-
new OpenAction(this),
-
new SavaAction(this),
-
new CutAction(this),
-
new CopyAction(this),
-
new PasteAction(this),
-
new AboutAction(this),
-
new ExitAction(this)
-
};
-
this.actions = actions; //将构造方法的actions赋值给类的属性actions
-
-
this.setJMenuBar(createJMenuBar(actions)); //设置菜单栏
-
Container container = this.getContentPane(); //获取容器
-
container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具栏
-
container.add(textPane, BorderLayout.CENTER);
-
container.add(statusBar, BorderLayout.SOUTH);
-
-
this.addWindowListener(new WindowListener() {
-
-
@Override
-
public void windowActivated(WindowEvent arg0) {
-
}
-
-
@Override
-
public void windowClosed(WindowEvent arg0) {
-
}
-
-
@Override
-
public void windowClosing(WindowEvent arg0) {
-
System.exit(0);
-
}
-
-
@Override
-
public void windowDeactivated(WindowEvent arg0) {
-
}
-
-
@Override
-
public void windowDeiconified(WindowEvent arg0) {
-
}
-
-
@Override
-
public void windowIconified(WindowEvent arg0) {
-
}
-
-
@Override
-
public void windowOpened(WindowEvent arg0) {
-
}
-
-
});
-
}
-
-
//创建菜单栏,参数是各个事件,返回菜单栏JMenuBar
-
private JMenuBar createJMenuBar(Action[] actions) {
-
JMenuBar menuBar = new JMenuBar(); //实例化菜单栏
-
JMenu menuFile = new JMenu("文件");
-
JMenu menuEdit = new JMenu("编辑");
-
JMenu menuAbout = new JMenu("帮助");
-
-
//添加新菜单项
-
menuFile.add(new JMenuItem(actions[0]));
-
menuFile.add(new JMenuItem(actions[1]));
-
menuFile.add(new JMenuItem(actions[2]));
-
menuFile.add(new JMenuItem(actions[3]));
-
menuFile.add(new JMenuItem(actions[4]));
-
menuFile.add(new JMenuItem(actions[5]));
-
menuFile.add(new JMenuItem(actions[6]));
-
menuFile.add(new JMenuItem(actions[7]));
-
-
menuBar.add(menuFile);
-
menuBar.add(menuEdit);
-
menuBar.add(menuAbout);
-
-
return menuBar;
-
}
-
-
//创建工具条,
-
private JToolBar createJToolBar(Action[] actions) {
-
JToolBar toolBar = new JToolBar(); //实例化工具条
-
-
for(int i = 0; i < actions.length; i++) {
-
JButton button = new JButton(actions[i]); //实例化新按钮
-
button.setRequestFocusEnabled(false); //设置不需要焦点
-
toolBar.add(button); //增加按钮到工具栏
-
}
-
-
return toolBar;
-
}
-
-
public static void main(String[] args) {
-
MyPad frame = new MyPad();
-
frame.setSize(500, 500);
-
frame.setVisible(true);
-
frame.setLocationRelativeTo(null);
-
}
-
}
-
package cn.com.action;
-
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
import cn.com.shizongger.MyPad;
-
-
public class CutAction extends AbstractAction {
-
MyPad myPad = null;
-
-
public CutAction(MyPad myPad) {
-
super("剪切");
-
this.myPad = myPad;
-
}
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
myPad.textPane.cut();
-
}
-
}
-
package cn.com.action;
-
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
import cn.com.shizongger.MyPad;
-
-
public class CopyAction extends AbstractAction {
-
MyPad myPad = null;
-
-
public CopyAction(MyPad myPad) {
-
super("复制");
-
this.myPad = myPad;
-
}
-
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
myPad.textPane.copy();
-
}
-
}
-
package cn.com.action;
-
-
import java.awt.event.ActionEvent;
-
-
import javax.swing.AbstractAction;
-
-
import cn.com.shizongger.MyPad;
-
-
public class PasteAction extends AbstractAction {
-
-
private MyPad myPad = null;
-
-
public PasteAction(MyPad myPad) {
-
super("粘贴");
-
this.myPad = myPad;
-
}
-
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
myPad.textPane.paste();
-
}
-
}
-
package cn.com.action;
-
-
import java.awt.event.ActionEvent;
-
-
import javax.swing.AbstractAction;
-
import javax.swing.JOptionPane;
-
-
import cn.com.shizongger.MyPad;
-
-
public class AboutAction extends AbstractAction {
-
-
MyPad myPad = null;
-
-
public AboutAction(MyPad myPad) {
-
super("关于");
-
this.myPad = myPad;
-
}
-
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
String output = "文本编辑器1.0版本\n" +
-
"作者:张仕宗\n" +
-
"日期:2015.5,8\n" +
-
"这是学习java以来的第一个比较综合的项目\n" +
-
"你的支持是我前进的动力";
-
-
JOptionPane.showMessageDialog(myPad, output);
-
}
-
}
-
package cn.com.action;
-
-
import java.awt.event.*;
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
import javax.swing.*;
-
-
import cn.com.shizongger.MyPad;
-
-
public class ExitAction extends AbstractAction {
-
-
private MyPad myPad = null;
-
-
public ExitAction(MyPad myPad) {
-
super("退出");
-
this.myPad = myPad;
-
}
-
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
File f = myPad.fileChooser.getSelectedFile();
-
-
//如果文件不存在,那么直接推出
-
if(f != null) {
-
int option = JOptionPane.showConfirmDialog(null, "要保存文本么?");
-
if(JOptionPane.YES_OPTION == option){
-
try {
-
OutputStream os = new FileOutputStream(f);
-
try {
-
os.write(myPad.textPane.getText().getBytes());
-
} catch (IOException e1) {
-
e1.printStackTrace();
-
}
-
} catch (FileNotFoundException e1) {
-
e1.printStackTrace();
-
}
-
System.exit(0);
-
}else if(JOptionPane.CANCEL_OPTION == option) {
-
return;
-
}
-
}
-
System.exit(0);
-
}
-
}
阅读(1097) | 评论(0) | 转发(0) |