Last active
December 18, 2015 13:29
-
-
Save Gaubee/5790654 to your computer and use it in GitHub Desktop.
简单的文本编辑器。
知识点:菜单栏、快捷键、文件流、剪切板
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import java.awt.datatransfer.*; | |
import java.io.*; | |
public class myEditor extends JFrame { | |
String filename; | |
JTextArea tx; | |
Clipboard clip = getToolkit().getSystemClipboard(); | |
myEditor() { | |
setLayout(new GridLayout(1, 1)); | |
tx = new JTextArea(); | |
add(tx); | |
JMenuBar mb = new JMenuBar(); | |
JMenu F = new JMenu("文件(F)"); | |
F.setMnemonic('F'); | |
JMenuItem n = new JMenuItem("新建"); | |
n.setAccelerator(KeyStroke.getKeyStroke('N', KeyEvent.CTRL_MASK, false)); | |
JMenuItem o = new JMenuItem("打开"); | |
o.setAccelerator(KeyStroke.getKeyStroke('O', KeyEvent.CTRL_MASK, false)); | |
JMenuItem s = new JMenuItem("保存"); | |
s.setAccelerator(KeyStroke.getKeyStroke('S', KeyEvent.CTRL_MASK, false)); | |
JMenuItem e = new JMenuItem("退出"); | |
// e.setAccelerator(KeyStroke.getKeyStroke('E',KeyEvent.CTRL_MASK,false)); | |
n.addActionListener(new New()); | |
F.add(n); | |
o.addActionListener(new Open()); | |
F.add(o); | |
s.addActionListener(new Save()); | |
F.add(s); | |
e.addActionListener(new Exit()); | |
F.add(e); | |
mb.add(F); | |
JMenu E = new JMenu("编辑(E)"); | |
E.setMnemonic('E'); | |
JMenuItem clear = new JMenuItem("清空"); | |
JMenuItem cut = new JMenuItem("剪切"); | |
cut.setAccelerator(KeyStroke.getKeyStroke('X', KeyEvent.CTRL_MASK, false)); | |
JMenuItem copy = new JMenuItem("复制"); | |
copy.setAccelerator(KeyStroke.getKeyStroke('C', KeyEvent.CTRL_MASK, false)); | |
JMenuItem paste = new JMenuItem("粘贴"); | |
paste.setAccelerator(KeyStroke.getKeyStroke('V', KeyEvent.CTRL_MASK, false)); | |
clear.addActionListener(new Clear()); | |
E.add(clear); | |
cut.addActionListener(new Cut()); | |
E.add(cut); | |
copy.addActionListener(new Copy()); | |
E.add(copy); | |
paste.addActionListener(new Paste()); | |
E.add(paste); | |
mb.add(E); | |
setJMenuBar(mb); | |
} | |
class Clear implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
tx.setText(""); | |
} | |
} | |
class New implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
tx.setText(" "); | |
setTitle(filename); | |
} | |
} | |
class Open implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
FileDialog fd = new FileDialog(myEditor.this, "请选择文件", FileDialog.LOAD); | |
fd.setVisible(true); | |
if (fd.getFile() != null) { | |
filename = fd.getDirectory() + fd.getFile(); | |
setTitle(filename); | |
ReadFile(); | |
} | |
tx.requestFocus(); | |
} | |
} | |
class Save implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
FileDialog fd = new FileDialog(myEditor.this, "保存文件", FileDialog.SAVE); | |
fd.setVisible(true); | |
if (fd.getFile() != null) { | |
filename = fd.getDirectory() + fd.getFile(); | |
setTitle(filename); | |
try { | |
DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); | |
String line = tx.getText(); | |
BufferedReader br = new BufferedReader(new StringReader(line)); | |
while ((line = br.readLine()) != null) { | |
d.writeBytes(line + "\r\n"); | |
d.close(); | |
} | |
} catch (Exception ex) { | |
System.out.println("警告:找不到文件!"); | |
} | |
tx.requestFocus(); | |
} | |
} | |
} | |
class Exit implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
System.exit(0); | |
} | |
} | |
void ReadFile() { | |
BufferedReader d; | |
StringBuffer sb = new StringBuffer(); | |
try { | |
d = new BufferedReader(new FileReader(filename)); | |
String line; | |
while ((line = d.readLine()) != null) | |
sb.append(line + "\n"); | |
tx.setText(sb.toString()); | |
d.close(); | |
} catch (FileNotFoundException fe) { | |
System.out.println("未找到文件"); | |
} catch (IOException ioe) {} | |
} | |
class Cut implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
String sel = tx.getSelectedText(); | |
StringSelection ss = new StringSelection(sel); | |
clip.setContents(ss, ss); | |
tx.replaceRange(" ", tx.getSelectionStart(), tx.getSelectionEnd()); | |
} | |
} | |
class Copy implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
String sel = tx.getSelectedText(); | |
StringSelection clipString = new StringSelection(sel); | |
clip.setContents(clipString, clipString); | |
} | |
} | |
class Paste implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
Transferable cliptran = clip.getContents(myEditor.this); | |
try { | |
String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); | |
tx.replaceRange(sel, tx.getSelectionStart(), tx.getSelectionEnd()); | |
} catch (Exception exc) { | |
System.out.println("注意:粘贴板中无字符串内容!"); | |
} | |
} | |
} | |
public static void main(String args[]) { | |
Frame f = new myEditor(); | |
f.setSize(600, 480); | |
f.setLocation(120, 80); | |
f.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment