用delphi编写记事本程序的打开,保存,另存为,退出,剪切,复制,粘贴

用delphi编写记事本程序的打开,保存,另存为,退出,剪切,复制,粘贴,删除按键编码是什么?怎么搞?求大神帮助... 用delphi编写记事本程序的打开,保存,另存为,退出,剪切,复制,粘贴,删除按键编码是什么?怎么搞?求大神帮助 展开
 我来答
在晴天的雨伞
2016-06-01 · TA获得超过6869个赞
知道大有可为答主
回答量:5761
采纳率:86%
帮助的人:1223万
展开全部
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

public class MyText extends JFrame implements ActionListener{
private JLabel lb1;
private JMenuBar mb;
private JMenu 文件, 编辑, 格式, 帮助;
private JMenuItem 新建, 打开, 保存, 退出, 复制, 粘贴, 剪切, 全选, 字体, 关于;
private JTextArea editorArea;
private boolean isDirty = false;
private String strFileName = "未命名";
private static final String EDITOR_NAME = "MyText";

public MyText() {
super();
mb = new JMenuBar();
文件 = new JMenu("文件");
编辑 = new JMenu("编辑");
格式 = new JMenu("格式");
帮助 = new JMenu("帮助");
新建 = new JMenuItem("新建");
新建.addActionListener(this);
打开 = new JMenuItem("打开");
打开.addActionListener(this);
保存 = new JMenuItem("保存");
保存.addActionListener(this);
退出 = new JMenuItem("退出");
退出.addActionListener(this);
复制 = new JMenuItem("复制");
复制.addActionListener(this);
粘贴 = new JMenuItem("粘贴");
粘贴.addActionListener(this);
剪切 = new JMenuItem("剪切");
剪切.addActionListener(this);
全选 = new JMenuItem("全选");
全选.addActionListener(this);
字体 = new JMenuItem("字体");
字体.addActionListener(this);
关于 = new JMenuItem("关于");
关于.addActionListener(this);
mb.add(文件);
mb.add(编辑);
mb.add(格式);
mb.add(帮助);
文件.add(新建);
文件.add(打开);
文件.add(保存);
文件.add(退出);
编辑.add(复制);
编辑.add(粘贴);
编辑.add(剪切);
编辑.add(全选);
格式.add(字体);
帮助.add(关于);
setJMenuBar(mb);

Container container = getContentPane();
editorArea = new JTextArea();
editorArea.setLineWrap(true);
editorArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(!isDirty()){
setDirty(true);
}
}
});
JScrollPane scrollPane = new JScrollPane(editorArea);
container.add(scrollPane);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
setTitle(formatEditorTitle());
setSize(600, 400);
setVisible(true);
}

private boolean isDirty() {
return isDirty;
}

private void setDirty(boolean isDirty) {
this.isDirty = isDirty;
setTitle(formatEditorTitle());
}

public static void main(String args[]) {
@SuppressWarnings("unused")
MyText app = new MyText();
}

public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
if(item.equals(新建)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
clearEditorArea();
setDirty(false);
}else if(item.equals(打开)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
openFile();
}else if(item.equals(保存)){
saveFile();
}else if(item.equals(退出)){
dispose();
}else if(item.equals(复制)){
editorArea.copy();
}else if(item.equals(剪切)){
editorArea.cut();
}else if(item.equals(粘贴)){
editorArea.paste();
}else if(item.equals(全选)){
editorArea.selectAll();
}else if(item.equals(字体)){
FontDialog font = new FontDialog(this, editorArea.getFont());
editorArea.setFont(font.getSelectedFont());
}else if(item.equals(关于)){
AboutDialog about = new AboutDialog(this);
about.setVisible(true);
}
}

private String getFileName() {
return strFileName;
}

private void setFileName(String strFileName) {
this.strFileName = strFileName;
}

public String formatEditorTitle(){
StringBuffer strFileNm = new StringBuffer(getFileName());
strFileNm.append(isDirty()?"*":"");
strFileNm.append(" - ");
strFileNm.append(EDITOR_NAME);
return strFileNm.toString();
}

private void clearEditorArea(){
editorArea.selectAll();
editorArea.replaceSelection("");
}

private void openFile(){
JFileChooser openDialog = new JFileChooser();
openDialog.setFileFilter(new TxtFileFilter());
if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = openDialog.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String buff = br.readLine();
clearEditorArea();
while(buff != null){
editorArea.append(buff);
editorArea.append("\n");
buff = br.readLine();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(br != null)
br.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}

private void saveFile(){
JFileChooser saveDialog = new JFileChooser();
saveDialog.setFileFilter(new TxtFileFilter());
if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = saveDialog.getSelectedFile();
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String buff = editorArea.getText();
bw.write(buff);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(bw != null)
bw.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}

class TxtFileFilter extends FileFilter{

@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}

@Override
public String getDescription() {
return "*.txt(文本文件)";
}

}

class FontDialog extends JDialog{
private JComboBox cb_FontSize;
private JComboBox cb_FontStyle;
private JComboBox cb_FontNm;
private Font font;
Hashtable<Integer, String> style = new Hashtable<Integer, String>();

public FontDialog(){
this(null, null);
}

public FontDialog(Frame owner, Font font){
super(owner);
this.font = font == null?getFont():font;
setTitle("字体选择框");
setModal(true);
setResizable(false);
setSize(326, 164);
getContentPane().setLayout(null);

final JLabel lb_FontNm = new JLabel();
lb_FontNm.setText("字体名称");
lb_FontNm.setBounds(10, 10, 66, 16);
getContentPane().add(lb_FontNm);

cb_FontNm = new JComboBox();
cb_FontNm.setBounds(10, 28, 133, 25);
getContentPane().add(cb_FontNm);

cb_FontStyle = new JComboBox();
cb_FontStyle.setBounds(169, 28, 66, 25);
getContentPane().add(cb_FontStyle);

cb_FontSize = new JComboBox();
cb_FontSize.setBounds(258, 28, 53, 25);
getContentPane().add(cb_FontSize);

final JButton btn_OK = new JButton();
btn_OK.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
int styleCode = 0;
for(Enumeration<Integer> i = style.keys();i.hasMoreElements();){
styleCode = i.nextElement();
if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))
break;
}
Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());
setSelectedFont(font);
dispose();
}
});
btn_OK.setText("确定");
btn_OK.setBounds(58, 83, 76, 26);
getContentPane().add(btn_OK);

final JButton btn_Cancel = new JButton();
btn_Cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
btn_Cancel.setText("取消");
btn_Cancel.setBounds(169, 83, 76, 26);
getContentPane().add(btn_Cancel);

final JLabel lb_FontStyle = new JLabel();
lb_FontStyle.setText("字体样式");
lb_FontStyle.setBounds(169, 10, 66, 16);
getContentPane().add(lb_FontStyle);

final JLabel lb_FontSize = new JLabel();
lb_FontSize.setText("字体大小");
lb_FontSize.setBounds(258, 10, 66, 16);
getContentPane().add(lb_FontSize);

init();
setVisible(true);
}

private void init(){
GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
String ss[]=gg.getAvailableFontFamilyNames();
for(String s : ss)
cb_FontNm.addItem(s);
if(font != null)
cb_FontNm.setSelectedItem(font.getFamily());

style.put(Font.PLAIN, "标准");
style.put(Font.BOLD, "粗体");
style.put(Font.ITALIC, "斜体");
style.put(Font.BOLD+Font.ITALIC, "粗体&斜体");

cb_FontStyle.addItem(style.get(Font.PLAIN));
cb_FontStyle.addItem(style.get(Font.BOLD));
cb_FontStyle.addItem(style.get(Font.ITALIC));
cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));

if(font != null)
cb_FontStyle.setSelectedItem(style.get(font.getStyle()));

for(int i=8;i<23;i++)
cb_FontSize.addItem(i);
if(font != null)
cb_FontSize.setSelectedItem(font.getSize());

}

public Font getSelectedFont() {
return font;
}

public void setSelectedFont(Font font) {
this.font = font;
}
}

class AboutDialog extends JDialog{
public AboutDialog(JFrame owner){
super(owner);
setTitle("关于");
setSize(new Dimension(322, 163));
getContentPane().setLayout(null);

final JLabel version = new JLabel();
version.setText("MyText 1.0");
version.setBounds(74, 37, 66, 16);
getContentPane().add(version);

final JLabel copyright = new JLabel();
copyright.setText("Copyright (C) 2010");
copyright.setBounds(74, 59, 188, 16);
getContentPane().add(copyright);

final JSeparator separator = new JSeparator();
separator.setBounds(70, 90, 210, 2);
getContentPane().add(separator);

final JButton okButton = new JButton();
okButton.setBounds(235, 95, 50, 26);
getContentPane().add(okButton);
okButton.setText("Ok");
okButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式