import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.text.SimpleDateFormat
import java.util.Date
import javax.swing.JFileChooser
import java.io.*
import javax.swing.JOptionPane
class Notepad {
public static void main(String[] args) {
NotepadFrame notepad = new NotepadFrame()
NotepadListener listener = new NotepadListener(notepad)
notepad.init()
notepad.addToMenuBar()
notepad.addActionListener(listener)
notepad.setVisible(true)
}
}
/**
* 记事本界面实现类
**
*/
class NotepadFrame extends JFrame {
private static final long serialVersionUID = 1L
// 创建菜单条对象
JMenuBar jmenu = new JMenuBar()
// 创建文本域组建对象
public JTextArea textarea = new JTextArea()
// 创建滚动条对象
JScrollPane jsp = new JScrollPane(textarea)
// 创建状态栏
JLabel timeLabel = new JLabel()
// 文本区域
JTextArea jta = new JTextArea()
// 状态栏
JToolBar jtb = new JToolBar()
// 创建文件选择对话框
JFileChooser chooser = new JFileChooser()
// 菜单
JMenu file = new JMenu("文件(F)")
JMenu edit = new JMenu("编辑(E)")
JMenu view = new JMenu("查看(V)")
JMenu set = new JMenu("格式(O)")
JMenu help = new JMenu("帮助(H)")
// 文件菜单项
JMenuItem newFile = new JMenuItem("新建(N)") // new ImageIcon("images\\OpenFile.gif")
JMenuItem openFile = new JMenuItem("打开(O)",new ImageIcon("images\\OpenFile.gif"))
JMenuItem saveFile = new JMenuItem("保存(S)")
JMenuItem ansaveFile = new JMenuItem("另存为(A)")
JMenuItem exitFile = new JMenuItem("退出(X)")
// 编辑菜单项
JMenuItem back = new JMenuItem("撤销(U)")
JMenuItem cut = new JMenuItem("剪切(T)")
JMenuItem copy = new JMenuItem("复制(C)")
JMenuItem paste = new JMenuItem("粘贴(P)")
JMenuItem delete = new JMenuItem("删除(L)")
JMenuItem find = new JMenuItem("查找(F)")
JMenuItem replace = new JMenuItem("替换(R)")
JMenuItem all = new JMenuItem("全选(A)")
JMenuItem time = new JMenuItem("时间/日期")
// 格式菜单项
JMenuItem autoChangeRow = new JMenuItem("自动换行")
JMenuItem font = new JMenuItem("字体")
// 查看菜单项
JMenuItem bar = new JMenuItem("状态栏")
// 帮助菜单项
JMenuItem viewHelp = new JMenuItem("查看帮助")
JMenuItem aboutNotepad = new JMenuItem("关于记事本")
// d出式菜单
JPopupMenu popmenu = new JPopupMenu()
JMenuItem itemUndo = new JMenuItem("撤销(U)", 'U')
JMenuItem itemRedo = new JMenuItem("恢复(R)", 'R')
JMenuItem itemCut = new JMenuItem("剪切(T)", 'T')
JMenuItem itemCopy = new JMenuItem("复制(P)", 'P')
JMenuItem itemPaste = new JMenuItem("粘贴(C)", 'C')
JMenuItem itemDel = new JMenuItem("删除(P)", 'P')
JMenuItem itemAll = new JMenuItem("全选(A)", 'A')
/**
* 初始化
*/
public void init() {
// 设置记事本大小
setSize(800, 600)
// 设置标题
setTitle("无标题 —— 记事本")
// 窗口居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()
Dimension frameSize = getSize()
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2)
// 关闭窗口
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
// 加入控件到窗体
add(jmenu, BorderLayout.NORTH)
add(jta, BorderLayout.CENTER)
add(jtb, BorderLayout.SOUTH)
}
/**
* 把菜单选项及快捷键加入到菜单条中
*/
public void addToMenuBar() {
// 添加文件菜单项
file.setMnemonic('F')
newFile.setMnemonic(KeyEvent.VK_N)
newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK))
file.add(newFile)
openFile.setMnemonic(KeyEvent.VK_O)
openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK))
file.add(openFile)
saveFile.setMnemonic(KeyEvent.VK_S)
saveFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK))
file.add(saveFile)
ansaveFile.setMnemonic(KeyEvent.VK_A)
file.add(ansaveFile)
exitFile.setMnemonic(KeyEvent.VK_X)
file.add(exitFile)
jmenu.add(file)
// 添加编辑菜单项
edit.setMnemonic('E')
back.setMnemonic('U')
back.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK))
edit.add(back)
cut.setMnemonic('T')
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK))
edit.add(cut)
copy.setMnemonic('C')
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK))
edit.add(copy)
paste.setMnemonic('P')
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK))
edit.add(paste)
delete.setMnemonic('L')
delete.setAccelerator(KeyStroke.getKeyStroke("DELETE"))
edit.add(delete)
find.setMnemonic('F')
find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK))
edit.add(find)
replace.setMnemonic('R')
replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK))
edit.add(replace)
all.setMnemonic('A')
all.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK))
edit.add(all)
time.setAccelerator(KeyStroke.getKeyStroke("F5"))
edit.add(time)
jmenu.add(edit)
// 格式菜单项
font.setMnemonic('O')
set.add(autoChangeRow)
set.add(font)
jmenu.add(set)
// 查看菜单项
view.setMnemonic('V')
view.add(bar)
jmenu.add(view)
// 帮助菜单项
help.setMnemonic('H')
help.add(viewHelp)
help.add(aboutNotepad)
jmenu.add(help)
// 添加d出菜单选项
itemUndo.setIcon(new ImageIcon("images\\Undo.gif"))
popmenu.add(itemUndo)
itemRedo.setIcon(new ImageIcon("images\\Redo.gif"))
popmenu.add(itemRedo)
popmenu.addSeparator()
itemCut.setIcon(new ImageIcon("images\\Cut.gif"))
popmenu.add(itemCut)
itemCopy.setIcon(new ImageIcon("images\\Copy.gif"))
popmenu.add(itemCopy)
itemPaste.setIcon(new ImageIcon("images\\Paste.gif"))
popmenu.add(itemPaste)
itemDel.setIcon(new ImageIcon("images\\Del.gif"))
popmenu.add(itemDel)
popmenu.addSeparator()
popmenu.add(itemAll)
}
/**
* 给事件源添加监听器
* @param listener 事件监听器
*/
public void addActionListener(ActionListener listener) {
// 对文件菜单项进行监听
newFile.addActionListener(listener)
openFile.addActionListener(listener)
saveFile.addActionListener(listener)
ansaveFile.addActionListener(listener)
exitFile.addActionListener(listener)
// 对编辑菜单项进行监听
back.addActionListener(listener)
cut.addActionListener(listener)
copy.addActionListener(listener)
paste.addActionListener(listener)
delete.addActionListener(listener)
find.addActionListener(listener)
replace.addActionListener(listener)
all.addActionListener(listener)
time.addActionListener(listener)
// 对格式菜单项进行监听
autoChangeRow.addActionListener(listener)
font.addActionListener(listener)
// 对查看菜单项进行监听
bar.addActionListener(listener)
// 对帮助菜单项进行监听
viewHelp.addActionListener(listener)
aboutNotepad.addActionListener(listener)
// 对d出式菜单进行监听
itemUndo.addActionListener(listener)
itemRedo.addActionListener(listener)
itemCut.addActionListener(listener)
itemCopy.addActionListener(listener)
itemPaste.addActionListener(listener)
itemDel.addActionListener(listener)
itemAll.addActionListener(listener)
}
}
class NotepadListener implements ActionListener {
private NotepadFrame notepad
public NotepadListener(NotepadFrame notepad) {
this.notepad = notepad
}
/**
* 文件菜单具体实现
* @param e
*/
public void fileActionEvent(ActionEvent e) {
// 新建
if (e.getSource() == notepad.newFile) {
}
// 打开
if (e.getSource() == notepad.openFile) {
// 设置类型为打开方式
notepad.chooser.setDialogType(JFileChooser.OPEN_DIALOG)
// 显示打开文件对话框
notepad.chooser.showOpenDialog(notepad)
// 获取文件所在路径
File f = notepad.chooser.getSelectedFile()
if (f != null) {
// 读取文件内容
String content = UtilityMethod.readFile(f)
// 设置文件内容显示
notepad.jta.setText(content)
notepad.setTitle(f.getName() + " —— 记事本")
}
}
// 另存为
if (e.getSource() == notepad.saveFile) {
// 设置类型为保持方式
notepad.chooser.setDialogType(JFileChooser.SAVE_DIALOG)
// 显示保存文件对话框
notepad.chooser.showSaveDialog(notepad)
// 获得文件选择路径
File file = notepad.chooser.getSelectedFile()
if (file != null) {
// 写入文本与的字符串到文件中
UtilityMethod.writeFile(file, notepad.jta.getText())
notepad.setTitle(file.getName() + " —— 记事本")
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fileActionEvent(e)
}
}
class UtilityMethod {
public static String getCurrentTime(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format)
Date date = new Date()
return sdf.format(date)
}
public static String readFile(File f) {
FileInputStream fis = null
byte[] b = null
try {
fis = new FileInputStream(f)
b = new byte[fis.available()]
fis.read(b)
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "对不起,找不到指定的文件!", "打开文件出错", JOptionPane.WARNING_MESSAGE)
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "对不起,出现IO异常,请稍候再试!", "读取文件出错", JOptionPane.WARNING_MESSAGE)
} finally {
if (fis != null) {
try {
fis.close()
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "对不起,出现IO异常,请稍候再试!", "关闭输入流出错", JOptionPane.WARNING_MESSAGE)
}
}
}
return new String(b)
}
public static void writeFile(File file, String text) {
FileOutputStream fos = null
try {
fos = new FileOutputStream(file)
fos.write(text.getBytes())
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "对不起,找不到指定的文件!", "保存文件出错", JOptionPane.WARNING_MESSAGE)
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "对不起,出现IO异常,请稍候再试!", "写入文件出错", JOptionPane.WARNING_MESSAGE)
} finally {
if (fos != null) {
try {
fos.close()
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "对不起,出现IO异常,请稍候再试!", "关闭输出流出错", JOptionPane.WARNING_MESSAGE)
}
}
}
}
}
class Ball {public void play() {
System.out.println("玩球儿...")
}
}
class Football extends Ball {
public void play() {
System.out.println("使用足球运动")
}
}
class Basketball extends Ball {
public void play() {
System.out.println("使用篮球运动")
}
}
public class TestMain {
public static void main(String[] args) {
TestMain tm = new TestMain()
tm.testPlay()
}
public void testPlay() {
Ball ball = new Football()
ball.play()
ball = new Basketball()
ball.play()
}
}
/*
D:\>javac TestMain.java
D:\>java TestMain
使用足球运动
使用篮球运动
*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)