📄 kpad.java
字号:
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Kpad extends JFrame{
JTextArea theArea;
Color tColor,bColor;
JScrollPane inputArea;
JMenuBar MBar;
UndoManager undo;
MyUndoManager myUndo;
JToolBar toolBar;
JButton undoButton,redoButton;
JToggleButton boldJtb,italicJtb;
JPopupMenu Popup1,Popup2;
JMenuItem undoPop,redoPop;
JFrame colorf;
Font newFont;
JComboBox jcbfont,jcbfontSize;
String[] fontSizeStr=new String[500];
static final String fontStr[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
static final String fontAttributeStr[]={"常规","粗体","斜体","粗斜体"};
static final String AboutMsg="KpaD Ver 1.0\n\nWritten By WizarKID\n\nhttp://wizarkid.com\nSome Rights Reserved.";
public Kpad(){
super("KpaD");
setBounds(100,100,750,500);
buildContent();
buildMenuBar();
// buildToolBar();
// buildPopupMenu();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
protected void buildContent(){
theArea=new JTextArea();
theArea.setLineWrap(false);
newFont=new Font("宋体",0,12);
theArea.setFont(newFont);
theArea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
bColor=new Color(204,204,204);
tColor=new Color(0,0,0);
theArea.setBackground(bColor);
theArea.setForeground(tColor);
myUndo=new MyUndoManager();
theArea.getDocument().addUndoableEditListener(myUndo);
inputArea=new JScrollPane(theArea);
inputArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
inputArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(inputArea);
for (int i=0;i<500;i++)
{
fontSizeStr[i]=Integer.toString(i+1);
}
}
protected void buildMenuBar(){
MBar= new JMenuBar();
MBar.setOpaque(true);
JMenu mfile=buildFileMenu();
JMenu medit=buildEditMenu();
JMenu mform=buildFormMenu();
JMenu mview=buildViewMenu();
JMenu mhelp=buildHelpMenu();
mfile.setMnemonic('F');
MBar.add(mfile);
MBar.add(medit);
MBar.add(mform);
MBar.add(mview);
MBar.add(mhelp);
setJMenuBar(MBar);
}
/* protected void buildToolBar(){
toolBar=new JToolBar();
toolBar.setFloatable(true);
JButton JB;
JB=toolBar.add(new ActionSet("新建",new ImageIcon(getClass().getResource("icons/new.png"))));
JB.setActionCommand("New");
JB.setToolTipText("新建");
JB=toolBar.add(new ActionSet("打开",new ImageIcon(getClass().getResource("icons/open.png"))));
JB.setActionCommand("Open");
JB.setToolTipText("打开");
toolBar.addSeparator();
JB=toolBar.add(new ActionSet("另存为",new ImageIcon(getClass().getResource("icons/save.png"))));
JB.setActionCommand("Save");
JB.setToolTipText("另存为");
toolBar.addSeparator();
JB=toolBar.add(new ActionSet("剪切",new ImageIcon(getClass().getResource("icons/cut.png"))));
JB.setActionCommand("Cut");
JB.setToolTipText("剪切");
JB=toolBar.add(new ActionSet("复制",new ImageIcon(getClass().getResource("icons/copy.png"))));
JB.setActionCommand("Copy");
JB.setToolTipText("复制");
JB=toolBar.add(new ActionSet("粘贴",new ImageIcon(getClass().getResource("icons/paste.png"))));
JB.setActionCommand("Paste");
JB.setToolTipText("粘贴");
toolBar.addSeparator();
undoButton=new JButton(new ActionSet(null,new ImageIcon(getClass().getResource("icons/undo.png"))));
toolBar.add(undoButton);
undoButton.setActionCommand("Undo");
undoButton.setToolTipText("撤消");
undoButton.setEnabled(false);
redoButton=new JButton(new ActionSet(null,new ImageIcon(getClass().getResource("icons/redo.png"))));
toolBar.add(redoButton);
redoButton.setActionCommand("Redo");
redoButton.setToolTipText("恢复");
redoButton.setEnabled(false);
toolBar.addSeparator();
boldJtb=new JToggleButton(new ActionSet(null,new ImageIcon(getClass().getResource("icons/bold.png"))));
boldJtb.setActionCommand("Bold");
boldJtb.setToolTipText("粗体");
toolBar.add(boldJtb);
italicJtb=new JToggleButton(new ActionSet(null,new ImageIcon(getClass().getResource("icons/italic.png"))));
italicJtb.setActionCommand("Italic");
italicJtb.setToolTipText("斜体");
toolBar.add(italicJtb);
toolBar.addSeparator();
JB=toolBar.add(new ActionSet("颜色",new ImageIcon(getClass().getResource("icons/changecolor.png"))));
JB.setActionCommand("ChangeColor");
JB.setToolTipText("文字颜色");
toolBar.addSeparator();
jcbfont=new JComboBox(fontStr);
jcbfont.setSelectedIndex(getOldFontIndex());
jcbfont.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
String newFontName=(String)ie.getItem();
int oldAttributeIndex=getOldAttributeIndex(),oldFontSizeIndex=getOldSizeIndex();
theArea.setFont(new Font(newFontName,oldAttributeIndex,Integer.parseInt(fontSizeStr[oldFontSizeIndex])));
}
});
toolBar.add(jcbfont);
toolBar.addSeparator();
toolBar.add(new JLabel(new ImageIcon(getClass().getResource("icons/tt.png"))));
toolBar.addSeparator();
jcbfontSize=new JComboBox(fontSizeStr);
jcbfontSize.setSelectedIndex(getOldSizeIndex());
jcbfontSize.setEditable(true);
jcbfontSize.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
if(ie.getStateChange()==ItemEvent.SELECTED)
{
int oldFontIndex=getOldFontIndex(),oldAttributeIndex=getOldAttributeIndex(),newFontSize;
try{
newFontSize=Integer.parseInt((String)ie.getItem());
theArea.setFont(new Font(fontStr[oldFontIndex],oldAttributeIndex,newFontSize));
}catch(NumberFormatException ex){
}
}
}
});
jcbfontSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
int oldFontIndex=getOldFontIndex(),oldAttributeIndex=getOldAttributeIndex(),oldSizeIndex=getOldSizeIndex();
int newFontSize=oldSizeIndex;
String tmp=(String)jcbfontSize.getSelectedItem();
try{
newFontSize=Integer.parseInt(tmp);
theArea.setFont(new Font(fontStr[oldFontIndex],oldAttributeIndex,newFontSize));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null, "必须输入整数!", "输入错误", JOptionPane.ERROR_MESSAGE);
jcbfontSize.setSelectedIndex(oldSizeIndex);
theArea.setFont(new Font(fontStr[oldFontIndex],oldAttributeIndex,oldSizeIndex));
}
}
});
toolBar.add(jcbfontSize);
getContentPane().add(toolBar,BorderLayout.NORTH);
}
protected void buildPopupMenu(){
PopupPanel1 pp1=new PopupPanel1();
PopupPanel2 pp2=new PopupPanel2();
getContentPane().add(pp1,BorderLayout.SOUTH);
toolBar.add(pp2);
pp1.setVisible(false);
pp2.setVisible(false);
}
*/
public JMenu buildFileMenu(){
JMenu file=new JMenu("文件(F)");
JMenuItem newf=file.add(new ActionSet("新建(N)",null));
JMenuItem open=file.add(new ActionSet("打开(O)...",null));
JMenuItem save=file.add(new ActionSet("另存为(S)",null));
JMenuItem quit=file.add(new ActionSet("退出(X)",null));
newf.setMnemonic('N');
open.setMnemonic('O');
save.setMnemonic('S');
quit.setMnemonic('X');
newf.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,false));
open.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,false));
save.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK,false));
newf.setActionCommand("New");
open.setActionCommand("Open");
save.setActionCommand("Save");
quit.setActionCommand("Quit");
file.add(newf);
file.add(open);
file.add(save);
file.addSeparator();
file.add(quit);
return file;
}
public JMenu buildEditMenu(){
JMenu edit=new JMenu("编辑(E)");
JMenuItem undo=edit.add(new ActionSet("撤消(U)",null));
JMenuItem redo=edit.add(new ActionSet("恢复(R)",null));
JMenuItem cut=edit.add(new ActionSet("剪切(T)",null));
JMenuItem copy=edit.add(new ActionSet("复制(C)",null));
JMenuItem paste=edit.add(new ActionSet("粘贴(P)",null));
JMenuItem selectAll=edit.add(new ActionSet("全选(A)",null));
JMenuItem timeAndDate=edit.add(new ActionSet("时间/日期(D)",null));
undo.setMnemonic('U');
redo.setMnemonic('R');
cut.setMnemonic('T');
copy.setMnemonic('C');
paste.setMnemonic('P');
selectAll.setMnemonic('A');
timeAndDate.setMnemonic('D');
undo.setAccelerator(KeyStroke.getKeyStroke('Z',Event.CTRL_MASK,false));
redo.setAccelerator(KeyStroke.getKeyStroke('Y',Event.CTRL_MASK,false));
cut.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK,false));
copy.setAccelerator(KeyStroke.getKeyStroke('C',Event.CTRL_MASK,false));
paste.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK,false));
selectAll.setAccelerator(KeyStroke.getKeyStroke('A',Event.CTRL_MASK,false));
timeAndDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
undo.setActionCommand("Undo");
redo.setActionCommand("Redo");
cut.setActionCommand("Cut");
copy.setActionCommand("Copy");
paste.setActionCommand("Paste");
selectAll.setActionCommand("SelectAll");
timeAndDate.setActionCommand("TimeAndDate");
edit.add(undo);
edit.add(redo);
edit.addSeparator();
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.addSeparator();
edit.add(selectAll);
edit.add(timeAndDate);
return edit;
}
public JMenu buildFormMenu(){
JMenu form=new JMenu("格式(O)");
JCheckBoxMenuItem autoWrap=new JCheckBoxMenuItem(new ActionSet("自动换行(W)",null));
form.add(autoWrap);
JMenuItem font=form.add(new ActionSet(" 字体(F)...",null));
JMenuItem color=form.add(new ActionSet(" 文字颜色(C)...",null));
JMenuItem backColor=form.add(new ActionSet(" 背景颜色(B)...",null));
autoWrap.setMnemonic('W');
font.setMnemonic('F');
color.setMnemonic('C');
backColor.setMnemonic('B');
autoWrap.setActionCommand("AutoWrap");
font.setActionCommand("Font");
color.setActionCommand("ChangeColor");
backColor.setActionCommand("ChangeBackColor");
form.add(autoWrap);
form.add(font);
form.add(color);
form.add(backColor);
return form;
}
public JMenu buildViewMenu(){
JMenu view=new JMenu("查看(V)");
JCheckBoxMenuItem toolBar=new JCheckBoxMenuItem(new ActionSet("工具栏(L)",null));
toolBar.setSelected(true);
toolBar.setMnemonic('L');
toolBar.setActionCommand("ShowToolBar");
view.add(toolBar);
return view;
}
public JMenu buildHelpMenu(){
JMenu help=new JMenu("帮助(H)");
JMenuItem mainHelp=help.add(new ActionSet("帮助主题(H)",null));
JMenuItem about=help.add(new ActionSet("关于(A)",null));
mainHelp.setMnemonic('H');
about.setMnemonic('A');
mainHelp.setActionCommand("MainHelp");
about.setActionCommand("About");
help.add(mainHelp);
help.add(about);
return help;
}
public void openDocument(){
JFileChooser chooser=new JFileChooser();
chooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
File file=null;
int result=chooser.showOpenDialog(this);
chooser.setApproveButtonText("确定");
chooser.setDialogTitle("打开文件");
if(result==chooser.APPROVE_OPTION)
{
theArea.setText("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -