📄 txt.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.tools.hat.internal.parser.Reader;
public class txt extends JFrame implements ActionListener
{
JTextArea ta;
JMenuItem open,save,myexit,open1,save1;
JMenuBar mb;
JPopupMenu popm;
JScrollPane sp;
public void createmenu()
{
mb=new JMenuBar();
JMenu m1=new JMenu("编辑");
mb.add(m1);
open=new JMenuItem("打开");
open.addActionListener(this);
save=new JMenuItem("保存");
save.addActionListener(this);
myexit=new JMenuItem("退出");
myexit.addActionListener(this);
m1.add(open);m1.add(save);m1.addSeparator(); m1.add(myexit);
}
public txt ()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
ta=new JTextArea(10,20);
add(ta,BorderLayout.CENTER);
Panel p1=new Panel();
add(p1,BorderLayout.SOUTH);
setVisible(true);
setSize(400,400);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
setLocation(x,y);
createmenu();
setJMenuBar(mb);
sp = new JScrollPane(ta);
getContentPane().add(sp);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
try
{
openfile();
}
catch(IOException ex){}
if(e.getActionCommand()=="保存")
{
try {
savefile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}
}
public void savefile() throws IOException
{
FileDialog fd=new FileDialog(this,"保存",FileDialog.SAVE);
fd.setVisible(true);
FileWriter fw=new FileWriter( fd.getDirectory()+fd.getFile());
for(int i=0;i<ta.getText().length();i++)
{
fw.write(ta.getText().charAt(i));
}
fw.close();
}
public void openfile() throws IOException
{
FileDialog fd=new FileDialog(this,"打开",FileDialog.LOAD);
fd.setVisible(true);
FileReader fr=new FileReader( fd.getDirectory()+fd.getFile());
int n=0;
int row=10;
while((n=fr.read())!=-1)
{
ta.append(""+(char)n);
row--;
if(row==0)
{
ta.append("\n");
row=20;
}
}
fr.close();
}
public static void main(String []args)
{
txt t=new txt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -