📄 mytext.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyText {
public static void main(String[] args) {
new FWindow("未命名文档");
}
}
class FWindow extends JFrame implements ActionListener,ItemListener{
JMenuBar menub;
JPanel panel,panel0;
JMenu mFile,mEdite,mFamat,mHelp,mView;
JMenuItem itOpen,itNew,itSave,itOSave,itEnd,itCopy,itCopy0,itCut,itCut0,itPaste,itPaste0,itSAll,itCls,itHelp,itFont,itFColor,itBColor,itHuanh,itTools,itFamats;
JTextArea text;
JTextField t;
JScrollPane Jpane;
JComboBox listFont,listFSize;
FWindow(String s){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
endF();
}
});
setTitle(s);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setSize(800,600);
setVisible(true);
/*字体字号设置1*/
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
listFont=new JComboBox(fontName);
listFSize=new JComboBox();
listFont.setSelectedIndex(135);
for (int i=1;i<=72;i++)
listFSize.addItem(i);
listFSize.setPreferredSize(new Dimension(80,30));
listFSize.setSelectedIndex(11);
FlowLayout flow =new FlowLayout();
FlowLayout flow2 =new FlowLayout();
panel=new JPanel();
panel0=new JPanel();
panel.setLayout(flow);
flow.setAlignment(FlowLayout.LEFT);
panel0.setLayout(flow2);
flow2.setAlignment(FlowLayout.LEFT);
JButton newB,openB,saveB,copyB,cutB,pasteB,fontB,colorB,qingxieB,jiacuB,lockB,unlockB;
newB=new JButton(new ImageIcon("tb2/NEW.GIF"));
openB=new JButton(new ImageIcon("tb2/OPEN.GIF"));
saveB=new JButton(new ImageIcon("tb2/SAVE.GIF"));
copyB=new JButton(new ImageIcon("tb2/COPY.GIF"));
cutB=new JButton(new ImageIcon("tb2/CUT.GIF"));
pasteB=new JButton(new ImageIcon("tb2/PASTE.GIF"));
fontB=new JButton(new ImageIcon("tb2/font.gif"));
colorB=new JButton(new ImageIcon("tb2/color.gif"));
jiacuB=new JButton(new ImageIcon("tb2/jiacu.gif"));
qingxieB=new JButton(new ImageIcon("tb2/qingxie.gif"));
lockB=new JButton(new ImageIcon("tb/lock2.gif"));
unlockB=new JButton(new ImageIcon("tb/lock.gif"));
newB.setPreferredSize(new Dimension(24,23));
openB.setPreferredSize(new Dimension(24,23));
saveB.setPreferredSize(new Dimension(24,23));
copyB.setPreferredSize(new Dimension(24,23));
cutB.setPreferredSize(new Dimension(24,23));
pasteB.setPreferredSize(new Dimension(24,23));
fontB.setPreferredSize(new Dimension(24,23));
colorB.setPreferredSize(new Dimension(24,23));
jiacuB.setPreferredSize(new Dimension(24,23));
qingxieB.setPreferredSize(new Dimension(24,23));
lockB.setPreferredSize(new Dimension(24,23));
unlockB.setPreferredSize(new Dimension(24,23));
t=new JTextField(15);
t.setHorizontalAlignment(JTextField.LEFT);
t.setEditable(false);
panel.add(newB);
panel.add(openB);
panel.add(saveB);
panel.add(copyB);
panel.add(cutB);
panel.add(pasteB);
panel.add(t);
panel.add(lockB);
panel.add(unlockB);
panel0.add(fontB);
panel0.add(listFont);
listFont.addItemListener(this);
panel0.add(listFSize);
listFSize.addItemListener(this);
panel0.add(colorB);
panel0.add(qingxieB);
panel0.add(jiacuB);
this.add(panel,BorderLayout.NORTH);
this.add(panel0,BorderLayout.SOUTH);
menub=new JMenuBar();
mFile=new JMenu("文件(F)");
mFile.setMnemonic(KeyEvent.VK_F);
mEdite=new JMenu("编辑(E)");
mEdite.setMnemonic(KeyEvent.VK_E);
mFamat=new JMenu("格式(G)");
mFamat.setMnemonic(KeyEvent.VK_G);
mHelp=new JMenu("帮助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mView=new JMenu("视图(V)");
mView.setMnemonic(KeyEvent.VK_V);
itOpen=new JMenuItem("打开",new ImageIcon("tb/OPEN.GIF"));
itNew=new JMenuItem("新建",new ImageIcon("tb/NEW.GIF"));
itSave=new JMenuItem("保存",new ImageIcon("tb/SAVE.GIF"));
itOSave=new JMenuItem("另存为",new ImageIcon("tb/SAVE.GIF"));
itEnd=new JMenuItem(" 退出");
itCopy=new JMenuItem("复制",new ImageIcon("tb/COPY.GIF"));
itCopy0=new JMenuItem("复制");
itPaste=new JMenuItem("粘贴",new ImageIcon("tb/PASTE.GIF"));
itPaste0=new JMenuItem("粘贴");
itCut=new JMenuItem("剪切",new ImageIcon("tb/CUT.GIF"));
itCut0=new JMenuItem("剪切");
itSAll=new JMenuItem(" 全选");
itCls=new JMenuItem(" 清除");
itHelp=new JMenuItem("关于");
itFont=new JMenuItem("字体",new ImageIcon("tb2/font.gif"));
itFColor=new JMenuItem("字体颜色",new ImageIcon("tb2/color.gif"));
itBColor=new JMenuItem(" 背景颜色");
itHuanh=new JMenuItem(" √自动换行");
itTools=new JMenuItem(" √工具栏");
itFamats=new JMenuItem(" √格式栏");
itOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
itNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
itSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
itCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
itPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
itCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
itSAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
text=new JTextArea();
Jpane=new JScrollPane(text);
text.setLineWrap(true);
text.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent arg0) {
keyP();
}
});
mFile.add(itNew);
mFile.add(itOpen);
mFile.add(itSave);
mFile.add(itOSave);
mFile.add(itEnd);
mFile.addSeparator();
mEdite.add(itCopy);
mEdite.add(itCut);
mEdite.add(itPaste);
mEdite.add(itSAll);
mEdite.add(itCls);
mHelp.add(itHelp);
mFamat.add(itFont);
mFamat.add(itFColor);
mFamat.add(itBColor);
mFamat.add(itHuanh);
mView.add(itTools);
mView.add(itFamats);
menub.add(mFile);
menub.add(mEdite);
menub.add(mFamat);
menub.add(mView);
menub.add(mHelp);
/*弹出式菜单*/
final JPopupMenu pMenu=new JPopupMenu();
pMenu.add(itCopy0);
pMenu.add(itCut0);
pMenu.add(itPaste0);
text.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
pMenu.show(text,e.getX(),e.getY());
}
});
setJMenuBar(menub);
this.add(Jpane,BorderLayout.CENTER);
itHelp.addActionListener(this);
itNew.addActionListener(this);
newB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
newF();
}
});
itOpen.addActionListener(this);
openB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
open();
}
});
itSave.addActionListener(this);
saveB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
save();
}
});
itOSave.addActionListener(this);
itCopy.addActionListener(this);
itCopy0.addActionListener(this);
copyB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
text.copy();
}
});
itCut.addActionListener(this);
itCut0.addActionListener(this);
cutB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
text.cut();
}
});
itPaste.addActionListener(this);
itPaste0.addActionListener(this);
pasteB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
text.paste();
}
});
colorB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
colorF();
}
});
qingxieB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
qingxie();
}
});
jiacuB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jiacu();
}
});
lockB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
lock();
}
});
unlockB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
lock();
}
});
itSAll.addActionListener(this);
itCls.addActionListener(this);
itEnd.addActionListener(this);
itFont.addActionListener(this);
itFColor.addActionListener(this);
itBColor.addActionListener(this);
itHuanh.addActionListener(this);
itTools.addActionListener(this);
itFamats.addActionListener(this);
validate();
}
public void actionPerformed(ActionEvent e) {
if((e.getSource()==itOpen)||(e.getSource()=="openB")){
open();
}
else if((e.getSource().equals(itCopy))||(e.getSource().equals(itCopy0))) text.copy();
else if((e.getSource().equals(itCut))||(e.getSource().equals(itCut0))){
text.cut();
if(!(t.getText().equals("文档已更改,注意保存!"))){
t.setText("文档已更改,注意保存!");
}
}
else if((e.getSource().equals(itPaste))||(e.getSource().equals(itPaste0))) {
text.paste();
if(!(t.getText().equals("文档已更改,注意保存!"))){
t.setText("文档已更改,注意保存!");
}
}
else if(e.getSource().equals(itSAll)){
text.requestFocus(true);
text.selectAll();
}
else if(e.getSource().equals(itCls)) text.setText("");
else if(e.getSource().equals(itSave)) save();
else if(e.getSource().equals(itOSave)) saveAs();
else if(e.getSource().equals(itFont)) fontT();
else if(e.getSource().equals(itFColor)) colorF();
else if(e.getSource().equals(itBColor)) colorB();
else if(e.getSource().equals(itHuanh)) huanH();
else if(e.getSource().equals(itEnd)) endF();
else if(e.getSource().equals(itNew)) newF();
else if(e.getSource().equals(itHelp)) {
JOptionPane.showMessageDialog(this,"版权所有,翻版必究!\n所有 者:解海涛\n学 号:200607034219","版权信息",
JOptionPane.WARNING_MESSAGE);
}
else if(e.getSource().equals(itTools)) {
if(itTools.getLabel().equals(" √工具栏")){
itTools.setLabel(" 工具栏");
panel.setVisible(false);
}
else {
itTools.setLabel(" √工具栏");
panel.setVisible(true);
}
}
else if(e.getSource().equals(itFamats)) {
if(itFamats.getLabel().equals(" √格式栏")){
itFamats.setLabel(" 格式栏");
panel0.setVisible(false);
}
else {
itFamats.setLabel(" √格式栏");
panel0.setVisible(true);
}
};
}
public void endF(){
if(!(t.getText().equals("文档已更改,注意保存!"))){
System.exit(0);
}
else{
int n=JOptionPane.showConfirmDialog(this,"文件已更改,是否保存?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION) {
save();
System.exit(0);
}
else if(n==JOptionPane.NO_OPTION) {
System.exit(0);
}
else if(n==JOptionPane.CANCEL_OPTION) {
}
}
}
public void lock(){
long i=text.getText().length();
String s=new String();
for (int j=0;j<i;j++){
s=s+(char)(text.getText().charAt(j)^'w');
}
text.setText(s);
if(!(t.getText().equals("文档已更改,注意保存!"))&&text.getText()!=null){
t.setText("文档已更改,注意保存!");
}
}
public void fontT(){//字体
fontText fonts=new fontText(this);
}
public void qingxie(){
if(!(text.getFont().isItalic())&&!(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.ITALIC,text.getFont().getSize());
text.setFont(f);
}
else if((text.getFont().isItalic())&&(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.BOLD,text.getFont().getSize());
text.setFont(f);
}
else if(!(text.getFont().isItalic())&&(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.BOLD+Font.ITALIC,text.getFont().getSize());
text.setFont(f);
}
else if((text.getFont().isItalic())&&!(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.PLAIN,text.getFont().getSize());
text.setFont(f);
}
}
public void jiacu(){
if(!(text.getFont().isItalic())&&!(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.BOLD,text.getFont().getSize());
text.setFont(f);
}
else if((text.getFont().isItalic())&&(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.ITALIC,text.getFont().getSize());
text.setFont(f);
}
else if(!(text.getFont().isItalic())&&(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.PLAIN,text.getFont().getSize());
text.setFont(f);
}
else if((text.getFont().isItalic())&&!(text.getFont().isBold())){
Font f=new Font(text.getFont().getName(),Font.BOLD+Font.ITALIC,text.getFont().getSize());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -