?? editordemo.java
字號:
package 記事本;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.*;
//簡單的文本編輯器
public class EditorDemo extends JFrame {
JMenuItem jnew,jopen,jsave,jcut,jcopy,jpaste,jabout,jexit,jlook;
JTextArea textPane = new JTextArea(); //文本窗格,編輯窗口
JLabel statusBar = new JLabel(); //狀態欄
JFileChooser filechooser = new JFileChooser(); //文件選擇器
JComboBox jc1,jc2,jc3,jc4,jc5;
String ziti="宋體";
int zihao=14;
int style=Font.PLAIN;
String s2[]={"Plain","Bold","Italic","Bold+Italic"};
String s1[]={"8","10","12","14","16","18","20","22","24"};
String s3[]={"宋體","黑體","隸書","楷體_GB2312"};
String s4[]={"Black","Green","Cyan","red","LightGray","Orange","Pink","Yellow"};
String s5[]={"Black","Green","Cyan","red","LightGray","Orange","Pink","Yellow"};
public EditorDemo() { //構造函數
super("簡單的文本編輯器"); //調用父類構造函數
Action[] actions = //Action數組,各種操作命令
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new AboutAction(),
new ExitAction()
};
jnew=new JMenuItem(actions[0]);
jopen=new JMenuItem(actions[1]);
jsave=new JMenuItem(actions[2]);//設置快捷鍵
jcut=new JMenuItem(actions[3]);
jcopy=new JMenuItem(actions[4]);
jpaste=new JMenuItem(actions[5]);
jabout=new JMenuItem(actions[6]);
jexit=new JMenuItem(actions[7]);
jlook=new JMenuItem("狀態欄(S)");
jlook.setEnabled(false);
jnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
jopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
jsave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
jcut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
jcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
jpaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
jabout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
jexit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
setJMenuBar(createJMenuBar(actions)); //設置菜單欄
Container container = getContentPane(); //得到容器
container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具欄
container.add(textPane, BorderLayout.CENTER); //增加文本窗格
container.add(statusBar, BorderLayout.SOUTH); //增加狀態欄
setSize(600, 400); //設置窗口尺寸
setVisible(true); //設置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口時退出程序
}
private JMenuBar createJMenuBar(Action[] actions) { //創建菜單欄
JMenuBar menubar = new JMenuBar(); //實例化菜單欄
JMenu menuFile = new JMenu("文件(F)"); //實例化菜單
JMenu menuEdit = new JMenu("編輯(E)");
JMenu menuAbout = new JMenu("幫助(H)");
JMenu menuformat =new JMenu("格式(O)");
JMenu menulook =new JMenu("查看(V)");
menuFile.setMnemonic('f');
menuFile.setMnemonic('F');
menuEdit.setMnemonic('e');
menuEdit.setMnemonic('E');
menuAbout.setMnemonic('H');
menuAbout.setMnemonic('h');
menuformat.setMnemonic('O');
menuformat.setMnemonic('o');
menulook.setMnemonic('V');
menulook.setMnemonic('v');
menulook.add(jlook);
//menuFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));////////設置快捷鍵
menuFile.add(jnew); //增加新菜單項
menuFile.add(jopen);
menuFile.add(jsave);
JMenuItem jm=new JMenuItem("另存為(A)");
jm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
menuFile.add(jm);
menuFile.addSeparator();
//JMenuItem jm1=new JMenuItem("打印");
//menuFile.addSeparator();
//jm1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
//menuFile.add(jm1);
JMenuItem jm2=new JMenuItem("插入日期");
jm2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
JMenuItem jm3=new JMenuItem("全選");
jm3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
JMenuItem jm4=new JMenuItem("自動換行");
jm4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK));
JMenuItem jm5=new JMenuItem("字體設置");
jm5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
menuEdit.add(jm2);
menuEdit.add(jm3);
menuformat.add(jm4);
menuformat.add(jm5);
menuFile.add(jexit);
menuEdit.addSeparator();
menuEdit.add(jcut);
menuEdit.add(jcopy);
menuEdit.add(jpaste);
menuAbout.add(jabout);
jm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = filechooser.showSaveDialog(EditorDemo.this); //顯示保存文件對話框
if (i == JFileChooser.APPROVE_OPTION) { //點擊對話框中保存按鈕
File f = filechooser.getSelectedFile(); //得到選擇的文件
try {
FileOutputStream out = new FileOutputStream(f); //得到文件輸出流
out.write(textPane.getText().getBytes()); //寫出文件
} catch (Exception ex) {
ex.printStackTrace(); //輸出出錯信息
}
}
}
});
/*/jm1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});*/
jm2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Date d=new Date();
textPane.append(d.toString());
}
});
jm3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textPane.selectAll();
}
});
jm4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (textPane.getLineWrap()) {
textPane.setLineWrap(false);
} else {
textPane.setLineWrap(true);
}
}
});
jm5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog jd=new JDialog();
Container con=jd.getContentPane();
con.setLayout(new FlowLayout());
jc1=new JComboBox(s1);
jc2=new JComboBox(s2);
jc3=new JComboBox(s3);
jc4=new JComboBox(s4);
jc5=new JComboBox(s5);
jc1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==jc1){
int n=jc1.getSelectedIndex();
zihao=Integer.parseInt(s1[n]);
}
textPane.setFont(new Font(ziti,style,zihao));
}
});
jc2.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==jc2){
int m=jc2.getSelectedIndex();
switch(m)
{
case 0:
style=Font.PLAIN;break;
case 1:
style=Font.BOLD;break;
case 2:
style=Font.ITALIC;break;
case 3:
style=Font.BOLD+Font.ITALIC;break;
}
}
textPane.setFont(new Font(ziti,style,zihao));
}
});
jc3.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==jc3){
int x=jc3.getSelectedIndex();
ziti=s3[x];
}
textPane.setFont(new Font(ziti,style,zihao));
}
});
jc4.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
int y=jc4.getSelectedIndex();
switch(y)
{
case 0:
textPane.setForeground(Color.black);break;
case 1:
textPane.setForeground(Color.green);break;
case 2:
textPane.setForeground(Color.cyan);break;
case 3:
textPane.setForeground(Color.red);break;
case 4:
textPane.setForeground(Color.lightGray);break;
case 5:
textPane.setForeground(Color.orange);break;
case 6:
textPane.setForeground(Color.pink);break;
case 7:
textPane.setForeground(Color.yellow);break;
}
}});
jc5.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
int z=jc5.getSelectedIndex();
switch(z)
{
case 0:
textPane.setBackground(Color.black);break;
case 1:
textPane.setBackground(Color.green);break;
case 2:
textPane.setBackground(Color.cyan);break;
case 3:
textPane.setBackground(Color.red);break;
case 4:
textPane.setBackground(Color.lightGray);break;
case 5:
textPane.setBackground(Color.orange);break;
case 6:
textPane.setBackground(Color.pink);break;
case 7:
textPane.setBackground(Color.yellow);break;
}
}});
con.add(jc1);
con.add(jc2);
con.add(jc3);
con.add(jc4);
con.add(jc5);
jd.setSize(300,100);
jd.setVisible(true);
}
});
menubar.add(menuFile); //增加菜單
menubar.add(menuEdit);
menubar.add(menuformat);
menubar.add(menulook);
menubar.add(menuAbout);
return menubar; //返回菜單欄
}
private JToolBar createJToolBar(Action[] actions) { //創建工具條
JToolBar toolBar = new JToolBar(); //實例化工具條
for (int i = 0; i < actions.length; i++) {
JButton bt = new JButton(actions[i]); //實例化新的按鈕
bt.setRequestFocusEnabled(false); //設置不需要焦點
toolBar.add(bt); //增加按鈕到工具欄
}
return toolBar; //返回工具欄
}
class NewAction extends AbstractAction { //新建文件命令
public NewAction() {
super("新建");
}
public void actionPerformed(ActionEvent e) {
textPane.setDocument(new DefaultStyledDocument()); //清空文檔
textPane.setFont(new Font(ziti,style,zihao));
textPane.setBackground(Color.white);
textPane.setForeground(Color.black);
}
}
class OpenAction extends AbstractAction { //打開文件命令
public OpenAction() {
super("打開");
}
public void actionPerformed(ActionEvent e) {
int i = filechooser.showOpenDialog(EditorDemo.this); //顯示打開文件對話框
if (i == JFileChooser.APPROVE_OPTION) { //點擊對話框中打開選項
File file = filechooser.getSelectedFile(); //得到選擇的文件
try{
FileInputStream fin =new FileInputStream(file);
InputStreamReader in =new InputStreamReader(fin);
BufferedReader reader =new BufferedReader(in);
String s=reader.readLine();
textPane.setText(s);
while((s=reader.readLine())!=null){
textPane.append("\n"+s);
}
in.close();
}catch(FileNotFoundException fe){}
catch (Exception ex) {
ex.printStackTrace(); //輸出出錯信息
}
}
}
}
class SaveAction extends AbstractAction { //保存命令
public SaveAction() {
super("保存");
}
public void actionPerformed(ActionEvent e) {
int i = filechooser.showSaveDialog(EditorDemo.this); //顯示保存文件對話框
if (i == JFileChooser.APPROVE_OPTION) { //點擊對話框中保存按鈕
File f = filechooser.getSelectedFile(); //得到選擇的文件
try {
FileOutputStream out = new FileOutputStream(f); //得到文件輸出流
out.write(textPane.getText().getBytes()); //寫出文件
} catch (Exception ex) {
ex.printStackTrace(); //輸出出錯信息
}
}
}
}
class ExitAction extends AbstractAction { //退出命令
public ExitAction() {
super("退出");
}
public void actionPerformed(ActionEvent e) {
System.exit(0); //退出程序
}
}
class CutAction extends AbstractAction { //剪切命令
public CutAction() {
super("剪切");
}
public void actionPerformed(ActionEvent e) {
textPane.cut(); //調用文本窗格的剪切命令
}
}
class CopyAction extends AbstractAction { //拷貝命令
public CopyAction() {
super("拷貝");
}
public void actionPerformed(ActionEvent e) {
textPane.copy(); //調用文本窗格的拷貝命令
}
}
class PasteAction extends AbstractAction { //粘貼命令
public PasteAction() {
super("粘貼");
}
public void actionPerformed(ActionEvent e) {
textPane.paste(); //調用文本窗格的粘貼命令
}
}
class AboutAction extends AbstractAction { //關于選項命令
public AboutAction() {
super("關于");
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(EditorDemo.this, "簡單的文本編輯器演示"); //顯示軟件信息
}
}
public static void main(String[] args) {
new EditorDemo();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -