📄 anewediter.java
字号:
if(fdag.getFile()!=null)
{
String st=fdag.getDirectory()+fdag.getFile();
try
{ file=new File(st);
FileOutputStream fout=new FileOutputStream(file);
DataOutputStream out=new DataOutputStream(fout);
out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getCanonicalPath(),"另存为信息",JOptionPane.PLAIN_MESSAGE);
f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
}
catch(Exception E)
{System.out.println("I/O错误!");
}isnewf=false;issaved=true;
}
}
if(e.getSource()==quitf)
{int ms;
if(tf.getText().length()!=0&&issaved==false)
{
ms=JOptionPane.showConfirmDialog(f,"文件"+file.getAbsolutePath()+"\n已经改变,想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(ms==0)
{if(isnewf==true)
{FileDialog fdag=new FileDialog(f,"保存",1);
fdag.setSize(300,250);
fdag.setVisible(true);
if(fdag.getFile()!=null)
{String st=fdag.getDirectory()+fdag.getFile();
try
{ file=new File(st);
FileOutputStream fout=new FileOutputStream(file);
DataOutputStream out=new DataOutputStream(fout);
out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
isnewf=false;issaved=true;
}
catch(Exception E)
{System.out.println("I/O错误!");}
}
}
else
{
try
{FileOutputStream fout=new FileOutputStream(file);
DataOutputStream out=new DataOutputStream(fout);
out.writeBytes(tf.getText());
out.close();
JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
isnewf=false;issaved=true;
}
catch(Exception E)
{System.out.println("I/O错误!");}
}
System.exit(0);
}
}
else{
System.exit(0);
}
}
if(e.getSource()==cancel){buffer=new String(tf.getText());
if(iskey==true)
{tf.setText(buffer.substring(0,buffer.length()-1));
iskey=false;cace=1;}
if(cace==1){tf.setText(temptext);cace=0;}
}
if(e.getSource()==cut){temptext=new String(tf.getText());tf.cut();issaved=false;iskey=false;cace=1;}//paste.setEnabled(true);
if(e.getSource()==copy){tf.copy();iskey=false;cace=1;}//paste.setEnabled(true);
if(e.getSource()==paste){temptext=new String(tf.getText());tf.paste();issaved=false;iskey=false;cace=1;}//paste.setEnabled(false);
if(e.getSource()==del){temptext=new String(tf.getText());tf.replaceSelection("");issaved=false;iskey=false;cace=1;}
if(e.getSource()==selectall){tf.selectAll();iskey=false;}
if(e.getSource()==gesi){setfont(); }
if(e.getSource()==forecolor){setColor(); }
if(e.getSource()==btn1)
{//
cbxstr1=(String)cb1.getSelectedItem();//tf.setText(cbxstr1);
cbxint3=Integer.parseInt((String)cb3.getSelectedItem());
if(((String)cb2.getSelectedItem()).compareTo("正常体")==0) tf.setFont(new Font(cbxstr1,Font.PLAIN,cbxint3));
if(((String)cb2.getSelectedItem()).compareTo("粗体")==0) tf.setFont(new Font(cbxstr1,Font.BOLD,cbxint3));
if(((String)cb2.getSelectedItem()).compareTo("斜体")==0) tf.setFont(new Font(cbxstr1,Font.ITALIC,cbxint3));
//tf.setText(String.valueOf(cbxint3));
//tf.setFont(cbxstr1,cbxint2,cbxint3);
issaved=false;
dialog.show(false);
}
if(e.getSource()==btn2){dialog.show(false);}
/* if(e.getSource()==cancel1){buffer=new String(tf.getText());
if(iskey==true)
{tf.setText(buffer.substring(0,tf.getText().length()-1));
iskey=false;cace=1;}
if(cace==1){tf.setText(temptext);cace=0;}
}
if(e.getSource()==copy1){tf.copy();iskey=false;cace=1;}
if(e.getSource()==cut1){temptext=new String(tf.getText());tf.cut();issaved=false;iskey=false;cace=1;}
if(e.getSource()==paste1){temptext=new String(tf.getText());tf.paste();issaved=false;iskey=false;cace=1;}
if(e.getSource()==del1){temptext=new String(tf.getText());tf.replaceSelection("");issaved=false;iskey=false;cace=1;}
if(e.getSource()==selall){tf.selectAll();iskey=false;}*/
}
class WinLis extends WindowAdapter
{public void windowClosing(WindowEvent e)
{int ms;
if(tf.getText().length()!=0&&issaved==false)
{ms=JOptionPane.showConfirmDialog(f,"文件"+file.getAbsolutePath()+"\n已经改变,想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(ms==0)
{if(isnewf==true)
{FileDialog fdag=new FileDialog(f,"保存",1);
fdag.setSize(300,250);
fdag.setVisible(true);
if(fdag.getFile()!=null)
{String st=fdag.getDirectory()+fdag.getFile();
try
{ file=new File(st);
FileOutputStream fout=new FileOutputStream(file);
DataOutputStream out=new DataOutputStream(fout);
out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
isnewf=false;issaved=true;
}
catch(Exception E)
{System.out.println("I/O错误!");}
}
}
else
{try
{FileOutputStream fout=new FileOutputStream(file);
DataOutputStream out=new DataOutputStream(fout);
out.writeBytes(tf.getText());
out.close();
JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
isnewf=false;issaved=true;
}
catch(Exception E)
{System.out.println("I/O错误!");}
}System.exit(0);
}
}
}
}
/*class MyMouseAdapter extends MouseAdapter
{public void mousePressed(MouseEvent e)
{if((e.getModifiers() & InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK)
pmenu.show(tf,e.getX(),e.getY());
}
}*/
/*class MyKeyAdapter extends KeyAdapter
{public void keyPressed(KeyEvent e)
{issaved=false;
if((e.getModifiers() & KeyEvent.VK_BACK_SPACE)!=KeyEvent.VK_BACK_SPACE)
temptext=tf.getText();//.substring(0,tf.getText().length()-1);
iskey=true;//cace=1;
}
} */
public static void main(String arg[])
{new Anewediter();
}
public void setfont()
{ dialog=new JDialog(f,"字体设置",true);
diacp=dialog.getContentPane();
diacp.setLayout(new FlowLayout());
jp=new JPanel(new GridLayout(4,2,4,4));
lb1=new JLabel("字体");
lb2=new JLabel("字形");
lb3=new JLabel("大小");
String s1[]={"宋体","黑体","楷体","隶书","华文行楷","Arial","Courier"};
cb1=new JComboBox(s1);
String s2[]={"正常体","粗体","斜体"};
cb2=new JComboBox(s2);
//int s3[]={10,15,20,25,30,35,40,45,50};
String s3[]={"10","15","20","25","30","35","40","45","50","60"};
//int s3[]={Font.PLAIN,Font.BOLD,Font.ITALIC};
cb3=new JComboBox(s3);
btn1=new JButton("确定");
btn2=new JButton("取消");
jp.add(lb1);jp.add(cb1);
jp.add(lb2);jp.add(cb2);
jp.add(lb3);jp.add(cb3);
btn1.addActionListener(this);
jp.add(btn1);
btn2.addActionListener(this);
jp.add(btn2);
diacp.add(jp);
Font ft=tf.getFont();
cb1.setSelectedItem(ft.getName());
if(ft.getStyle()==0)cb2.setSelectedItem("正常体");
if(ft.getStyle()==1)cb2.setSelectedItem("粗体");
if(ft.getStyle()==2)cb2.setSelectedItem("斜体");
cb3.setSelectedItem(String.valueOf(ft.getSize()));
//dialog.add(diacp);
dialog.setBounds(250,250,250,180);
dialog.setResizable(false);
dialog.setVisible(true);
}
public void setColor()
{ dialog=new JDialog(f,"前景设置",true);
diacp=dialog.getContentPane();
diacp.setLayout(new FlowLayout());
Color newcolor=JColorChooser.showDialog(diacp, "选择背景色", tf.getBackground());
if(newcolor!=null){
tf.setBackground(newcolor);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -