📄 text editor.txt
字号:
public void actionPerformed(ActionEvent ae)
{
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+"
");
txt.setText(sb.toString());
d.close();
}catch(FileNotFoundException e){
System.out.println("File not found");
}catch(IOException e){ }
}
public void writeFile()
{
try{
DataOutputStream d=new DataOutputStream(new
FileOutputStream(filename));
String line=txt.getText();
BufferedReader br=new BufferedReader(new
StringReader(line));
while((line=br.readLine())!=null)
{
d.writeBytes(line+"
");
}d.close();
}catch(Exception e){
System.out.println("File not found");
}
}
class Cut implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String sel=txt.getSelectedText();
StringSelection ss=new StringSelection(sel);
clip.setContents(ss,ss);
txt.replaceRange("
",txt.getSelectionStart(),txt.getSelectionEnd());
}
}
class Copy implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String sel=txt.getSelectedText();
StringSelection clipstring=new StringSelection(sel);
clip.setContents(clipstring,clipstring);
}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Transferable
cliptran=clip.getContents(TextEditor.this);
try{
String
sel=(String)cliptran.getTransferData(DataFlavor.stringFlavor);
txt.replaceRange(sel,txt.getSelectionStart(),txt.getSelectionEnd());
}catch(Exception e){
System.out.println("not starting flavor");
}
}
}
class Win extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
ConfirmDialog cd=new ConfirmDialog();
if(!b)
{
System.exit(0);
}
}
}
class ConfirmDialog extends JPanel
{
public ConfirmDialog()
{
int result;
result=JOptionPane.showConfirmDialog(this,fn+" not saved do you
want to save"/*,"Message Box",JOptionPane.QUESTION_MESSAGE*/);
switch(result)
{
case JOptionPane.YES_OPTION:
FileDialog fd=new FileDialog(TextEditor.this,"Save
File",FileDialog.SAVE);
fd.setFile(fn);
fd.setDirectory(dn);
fd.show();
if(fd.getFile()!=null)
{
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
writeFile();
txt.requestFocus();
}
System.out.println("Yes button pressed");
break;
case JOptionPane.NO_OPTION:
dispose();
System.exit(0);
System.out.println("NO button pressed");
break;
case JOptionPane.CANCEL_OPTION:
setVisible(true);
//b=false;
repaint();
System.out.println("Cancel button pressed");
break;
case JOptionPane.CLOSED_OPTION:
System.out.println("Closed button pressed");
break;
}
}
}
class Size implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int style=f.getStyle();
String w=e.getActionCommand();
if(w=="10")
{
f= new Font("Courier",style,10);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="12")
{
f= new Font("Courier",style,12);
fsize=f.getSize();
txt.setFont(f);
repaint();
}
if(w=="14")
{
f= new Font("Courier",style,14);
fsize=f.getSize();
txt.setFont(f);
repaint();
}
if(w=="16")
{
f= new Font("Courier",style,16);
txt. setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="18")
{
f= new Font("Courier",style,18);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="20")
{
f= new Font("Courier",style,20);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="22")
{
f= new Font("Courier",style,22);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="24")
{
f= new Font("Courier",style,24);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="26")
{
f= new Font("Courier",style,26);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="28")
{
f= new Font("Courier",style,28);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
}
}
class FM extends Applet implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String b=e.getActionCommand();
if(b=="Bold")
{
f= new Font("Courier",Font.BOLD,fsize);
style=f.getStyle();
txt.setFont(f);
}
if(b=="Plain")
{
f= new Font("Courier",Font.PLAIN,fsize);
style=f.getStyle();
txt.setFont(f);
}
if(b=="Italic")
{
f= new Font("Courier",Font.ITALIC,fsize);
txt.setFont(f);
}
repaint();
}
}
class Type implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String lbl=e.getActionCommand();
if(lbl=="TimesRoman")
{
f=new Font("TimesRoman",style,fsize);
txt.setFont(f);
}
if(lbl=="Courier")
{
f=new Font("Courier",style,fsize);
txt.setFont(f);
}
if(lbl=="Helvetica")
{
f=new Font("Helvetica",style,fsize);
txt.setFont(f);
}
if(lbl=="Arial")
{
f=new Font("Arial",style,fsize);
txt.setFont(f);
}
if(lbl=="Arial Black")
{
f=new Font("ArialBlack",style,fsize);
txt.setFont(f);
}
if(lbl=="Century")
{
f=new Font("Century",style,fsize);
txt.setFont(f);
}
repaint();
}
}
class BC implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
st=e.getActionCommand();
JFrame jf=new JFrame("JColorChooser");
colorChooser c=new colorChooser();
c.setSize(400,300);
c.setVisible(true);
}
}
class colorChooser extends JFrame
{
Button ok;
JColorChooser jcc;
public colorChooser(){
setTitle("JColorChooser");
jcc=new JColorChooser();
JPanel content=(JPanel)getContentPane();
content.setLayout(new BorderLayout());
content.add(jcc,"Center");
ok=new Button("OK");
content.add(ok,"South");
ok.addActionListener(new B());
}
class B implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Color Is:"+jcc.getColor().toString());
if(st.equals("Background"))
txt.setBackground(jcc.getColor());
if(st.equals("Foreground"))
txt.setForeground(jcc.getColor());
setVisible(false);
}
}
}
public static void main(String args[])
{
Frame fm=new TextEditor();
fm.setSize(800,600);
fm.setVisible(true);
fm.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -