分享
 
 
 

Java记事本和界面(一)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

package proe;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.text.*;

import javax.swing.event.*;

import javax.swing.undo.*;

import java.util.*;

/**

*<p>Title: 程序编辑器</p>

*<p>Description: 增强的文本编辑程序</p>

*<p>Copyright: Copyright (c) 2005</p>

*<p>Company: </p>

*@author 红色彼岸

*@version 0.1

*/

class ProEFrame extends JFrame{

private JFileChooser fileChooser = new JFileChooser();

private JMenuBar menuBar;

private JMenu file, edit, format, help;

private JMenuItem newItem, openItem, saveItem, saveAsItem, pageSetItem, printItem, exitItem;

private JMenuItem undoItem, redoItem, cutItem, copyItem, pasteItem, deleteItem, findItem, findNextItem, replaceItem, goToLineItem, selectAllItem, timeItem;

private JMenuItem autoNextLineItem, fontItem;

private JMenuItem helpItem, aboutItem;

private JTextArea textArea;

private JScrollPane scrollPane;

private final int TAB_SIZE = 4;

private File fileName;

private boolean isChange = false;

private final UndoManager undo = new UndoManager();

public ProEFrame(String title) {

//设置窗体的标题栏和默认关闭操作

super(title);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

//设置菜单栏和菜单的内容

menuBar = new JMenuBar();

file = new JMenu("文件(F)");

edit = new JMenu("编辑(E)");

format = new JMenu("格式(O)");

help = new JMenu("帮助(H)");

setQuickKey(file, 'F');

setQuickKey(edit, 'E');

setQuickKey(format, 'O');

setQuickKey(help, 'H');

newItem = new JMenuItem("新建(N)");

openItem = new JMenuItem("打开(O)...");

saveItem = new JMenuItem("保存(S)");

saveAsItem = new JMenuItem("另存为...");

pageSetItem = new JMenuItem("页面设置(U)...");

printItem = new JMenuItem("打印(P)...");

exitItem = new JMenuItem("退出");

setQuickKey(newItem, 'N');

setQuickKey(openItem, 'O');

setQuickKey(saveItem, 'S');

setQuickKey(pageSetItem, 'U');

setQuickKey(printItem, 'P');

file.add(newItem);

file.add(openItem);

file.add(saveItem);

file.add(saveAsItem);

file.addSeparator();

file.add(pageSetItem);

file.add(printItem);

file.addSeparator();

file.add(exitItem);

undoItem = new JMenuItem("撤销(Z)");

redoItem = new JMenuItem("恢复(R)");

cutItem = new JMenuItem("剪切(X)");

copyItem = new JMenuItem("复制(C)");

pasteItem = new JMenuItem("粘贴(V)");

deleteItem = new JMenuItem("删除");

findItem = new JMenuItem("查找(F)...");

findNextItem = new JMenuItem("查找下一个");

replaceItem = new JMenuItem("替换...");

goToLineItem = new JMenuItem("转到(G)...");

selectAllItem = new JMenuItem("全选(A)");

timeItem = new JMenuItem("时间/日期");

setQuickKey(undoItem, 'Z');

setQuickKey(redoItem, 'R');

setQuickKey(cutItem, 'X');

setQuickKey(copyItem, 'C');

setQuickKey(pasteItem, 'V');

setQuickKey(findItem, 'F');

setQuickKey(goToLineItem, 'G');

setQuickKey(selectAllItem, 'A');

edit.add(undoItem);

edit.add(redoItem);

edit.addSeparator();

edit.add(cutItem);

edit.add(copyItem);

edit.add(pasteItem);

edit.add(deleteItem);

edit.addSeparator();

edit.add(findItem);

edit.add(findNextItem);

edit.add(replaceItem);

edit.add(goToLineItem);

edit.addSeparator();

edit.add(selectAllItem);

edit.add(timeItem);

autoNextLineItem = new JMenuItem("自动换行(W)");

fontItem = new JMenuItem("字体...");

setQuickKey(autoNextLineItem, 'W');

format.add(autoNextLineItem);

format.add(fontItem);

helpItem = new JMenuItem("帮助主题(H)");

aboutItem = new JMenuItem("关于ProE");

setQuickKey(helpItem, 'H');

help.add(helpItem);

help.add(aboutItem);

menuBar.add(file);

menuBar.add(edit);

menuBar.add(format);

menuBar.add(help);

setJMenuBar(menuBar);

textArea = new JTextArea();

textArea.setTabSize(TAB_SIZE);

//支持撤销/恢复操作的监听方法

textArea.getDocument().addUndoableEditListener(new UndoableEditListener() {

public void undoableEditHappened(UndoableEditEvent evt) {

undo.addEdit(evt.getEdit());

}

});

//监听文档内容是否发生变化

textArea.getDocument().addDocumentListener(

new DocumentListener() {

public void changedUpdate(DocumentEvent de) {

isChange = true;

}

public void insertUpdate(DocumentEvent de) {

isChange = true;

}

public void removeUpdate(DocumentEvent de) {

isChange = true;

}

}

);

scrollPane = new JScrollPane(textArea);

this.getContentPane().add(scrollPane, BorderLayout.CENTER);

//测试用的方法

MenuItemEventHandler menuItemHandler = new MenuItemEventHandler();

newItem.addActionListener(menuItemHandler);

openItem.addActionListener(menuItemHandler);

saveItem.addActionListener(menuItemHandler);

saveAsItem.addActionListener(menuItemHandler);

pageSetItem.addActionListener(menuItemHandler);

printItem.addActionListener(menuItemHandler);

exitItem.addActionListener(menuItemHandler);

undoItem.addActionListener(menuItemHandler);

redoItem.addActionListener(menuItemHandler);

cutItem.addActionListener(menuItemHandler);

copyItem.addActionListener(menuItemHandler);

pasteItem.addActionListener(menuItemHandler);

deleteItem.addActionListener(menuItemHandler);

findItem.addActionListener(menuItemHandler);

findNextItem.addActionListener(menuItemHandler);

replaceItem.addActionListener(menuItemHandler);

goToLineItem.addActionListener(menuItemHandler);

selectAllItem.addActionListener(menuItemHandler);

timeItem.addActionListener(menuItemHandler);

autoNextLineItem.addActionListener(menuItemHandler);

fontItem.addActionListener(menuItemHandler);

helpItem.addActionListener(menuItemHandler);

aboutItem.addActionListener(menuItemHandler);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

exitPerformed();

}

});

}

//辅助方法:为菜单或者菜单项设定快捷键

private void setQuickKey(AbstractButton button, char quickKey) {

String type = button.getClass().getSimpleName();

if(type.equals("JMenu"))

button.setMnemonic(quickKey);

else if(type.equals("JMenuItem")) {

JMenuItem menuItem = (JMenuItem)button;

menuItem.setAccelerator(KeyStroke.getKeyStroke(quickKey, InputEvent.CTRL_MASK));

}

}

//辅助方法:测试JTextArea中文本是否有变化

private boolean hasChange() {

return isChange;

}

//内部方法:响应菜单事件

private void newPerformed() {

int value = -1;

if(hasChange())

if(fileName != null && fileName.exists()) {

String[] buttonTexts = {"保存", "不保存", "取消"};

value = JOptionPane.showOptionDialog(this, "源程序已经改变,请确认是否保存!", "保存更改?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[0]);

if(value == JOptionPane.YES_OPTION)

savePerformed();

else if(value == JOptionPane.NO_OPTION) {

fileName = null;

textArea.setText("");

setTitle("程序编辑器");

return;

}

else if(value == JOptionPane.CANCEL_OPTION)

return;

}

else {

saveAsPerformed();

fileName = null;

textArea.setText("");

setTitle("程序编辑器");

return;

}

fileName = null;

textArea.setText("");

setTitle("程序编辑器");

isChange = false;

return;

}

private void openPerformed() {

BufferedReader input = null;

String text;

int ret = fileChooser.showOpenDialog(this);

if(ret != JFileChooser.APPROVE_OPTION)

return;

newPerformed();

fileName = fileChooser.getSelectedFile();

if(fileName.exists() && fileName.canRead()) {

setTitle("程序编辑器: " + fileName.getName());

try{

input = new BufferedReader(new FileReader(fileName));

while((text = input.readLine()) != null)

textArea.append(text + "\n");

input.close();

}

catch(IOException e) {

JOptionPane.showMessageDialog(this, "文件打开错误,没有打开");

}

finally {

input = null;

}

}

isChange = false;

}

private void savePerformed() {

if(!hasChange())

return;

PrintWriter output = null;

if(fileName != null && fileName.exists()) {

try{

output = new PrintWriter(fileName);

output.write(textArea.getText());

}

catch(IOException e)

{

JOptionPane.showMessageDialog(this, "文件写入错误,没有保存");

}

finally {

output.close();

output = null;

}

isChange = false;

}

else

saveAsPerformed();

}

private void saveAsPerformed() {

if(!hasChange())

return;

PrintWriter output = null;

int ret = fileChooser.showSaveDialog(this);

if(ret != JFileChooser.APPROVE_OPTION) {

isChange = true;

return;

}

fileName = fileChooser.getSelectedFile();

setTitle("程序编辑器: " + fileName.getName());

try{

output = new PrintWriter(fileName);

output.write(textArea.getText());

}

catch(IOException e){

JOptionPane.showMessageDialog(this, "文件写入错误,没有保存");

}

finally{

output.close();

output = null;

}

isChange = false;

}

private void pageSetPerformed() {

System.out.println("显示页面设置对话框");

}

private void printPerformed() {

System.out.println("显示打印对话框");

}

private void exitPerformed() {

String[] buttonTexts = {"保存退出", "不保存退出", "取消"};

int value = -1;

if(hasChange()) {

value = JOptionPane.showOptionDialog(this, "源程序已经改变,请是否选择保存或直接退出!", "退出程序", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[0]);

switch(value)

{

case JOptionPane.YES_OPTION:

savePerformed();

break;

case JOptionPane.NO_OPTION:

System.exit(0);

break;

case JOptionPane.CANCEL_OPTION:

isChange = true;

return;

}

}

else

System.exit(0);

}

private void undoPerformed() {

try{

if(undo.canUndo())

undo.undo();

}catch(CannotUndoException e) {

JOptionPane.showMessageDialog(this, "无法执行撤销操作", "撤销异常", JOptionPane.WARNING_MESSAGE);

}

}

private void redoPerformed() {

try{

if(undo.canRedo())

undo.redo();

}catch(CannotRedoException e) {

JOptionPane.showMessageDialog(this, "无法执行恢复操作", "恢复异常", JOptionPane.WARNING_MESSAGE);

}

}

private void cutPerformed() {

textArea.cut();

}

private void copyPerformed() {

textArea.copy();

}

private void pastePerformed() {

textArea.paste();

}

private void deletePerformed() {

textArea.replaceRange(null, textArea.getSelectionStart(), textArea.getSelectionEnd());

}

private void findPerformed() {

String findString = JOptionPane.showInputDialog(this, );

}

private void findNextPerformed() {

System.out.println("查找下一个");

}

private void replacePerformed() {

System.out.println("替换");

}

private void goToLinePerformed() {

System.out.println("转到");

}

private void selectAllPerformed() {

textArea.selectAll();

}

private void timePerformed() {

textArea.insert((new Date()).toString(), textArea.getSelectionEnd());

}

private void autoNextLinePerformed() {

System.out.println("自动换行");

}

private void fontPerformed() {

System.out.println("字体");

}

private void helpPerformed() {

System.out.println("帮助主题");

}

private void aboutPerformed() {

System.out.println("关于程序");

}

/**

*<p>Title: </p>

*<p>Description: 处理菜单出发的事件</p>

*<p>Copyright: Copyright (c) 2005</p>

*<p>Company: </p>

*@author 红色彼岸

*@version 0.1

*/

class MenuItemEventHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

JMenuItem item = (JMenuItem)e.getSource();

if(item == newItem)

newPerformed();

else if(item == openItem)

openPerformed();

else if(item == saveItem)

savePerformed();

else if(item == saveAsItem)

saveAsPerformed();

else if(item == pageSetItem)

pageSetPerformed();

else if(item == printItem)

printPerformed();

else if(item == exitItem)

exitPerformed();

else if(item == undoItem)

undoPerformed();

else if(item == redoItem)

redoPerformed();

else if(item == cutItem)

cutPerformed();

else if(item == copyItem)

copyPerformed();

else if(item == pasteItem)

pastePerformed();

else if(item == deleteItem)

deletePerformed();

else if(item == findItem)

findPerformed();

else if(item == findNextItem)

findNextPerformed();

else if(item == replaceItem)

replacePerformed();

else if(item == goToLineItem)

goToLinePerformed();

else if(item == selectAllItem)

selectAllPerformed();

else if(item == timeItem)

timePerformed();

else if(item == autoNextLineItem)

autoNextLinePerformed();

else if(item == fontItem)

fontPerformed();

else if(item == helpItem)

helpPerformed();

else if(item == aboutItem)

aboutPerformed();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有