java 记事本换行_[求助]记事本自动换行
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
我編的一個記事本??自動換行該如何實現
import?java.io.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.awt.print.*;
public?class?Jishiben?extends?Frame?implements?ActionListener
{
FileDialog?fileDlg;
String?str,?fileName;
int?p;
byte?byteBuf[]=new?byte[1000000];
TextArea?ta=new?TextArea();
MenuBar?mb=new?MenuBar();
Menu?m1=new?Menu("文件");
MenuItem?New=new?MenuItem("新建");
MenuItem?open=new?MenuItem("打開");
MenuItem?save=new?MenuItem("保存");
MenuItem?sealset=new?MenuItem("打印設置");
MenuItem?seal=new?MenuItem("打印");
MenuItem?exit=new?MenuItem("退出");
Menu?m2=new?Menu("編輯");
MenuItem?copy=new?MenuItem("復制");
MenuItem?cut=new?MenuItem("剪切");
MenuItem?paste=new?MenuItem("粘貼");
MenuItem?delete=new?MenuItem("刪除");
MenuItem?all=new?MenuItem("全選");
MenuItem?time=new?MenuItem("時間/日期");
Menu?m3=new?Menu("格式");
CheckboxMenuItem?newline=new?CheckboxMenuItem("自動換行");
PrinterJob?prt=PrinterJob.getPrinterJob();
Jishiben()
{
setTitle("Lite記事本?1.0");
setSize(600,380);
add("Center",?ta);
addWindowListener(new?WindowAdapter()
{
public?void?windowClosing(WindowEvent?e)
{
System.exit(0);
}
});
m1.setEnabled(true);
m1.add(New);
m1.add(open);
m1.add(save);
m1.addSeparator();
m1.add(sealset);
m1.add(seal);
m1.addSeparator();
m1.add(exit);
mb.add(m1);
m2.add(copy);
m2.add(cut);
m2.add(paste);
m2.add(delete);
m2.addSeparator();
m2.add(all);
m2.add(time);
m2.add(newline);
mb.add(m2);
m3.add(newline);
mb.add(m3);
m4.add(about);
mb.add(m4);
setMenuBar(mb);
New.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
sealset.addActionListener(this);
seal.addActionListener(this);
exit.addActionListener(this);
copy.addActionListener(this);
cut.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
all.addActionListener(this);
time.addActionListener(this);
show();
}
public?void?actionPerformed(ActionEvent?e)
{
if?(e.getSource()==New)//?新建
ta.setText("");
if?(e.getSource()==open)//?打開
{
fileDlg=new?FileDialog(this,"打開文件");
fileDlg.show();
fileName=fileDlg.getFile();
try
{
FileInputStream?in=new?FileInputStream(fileName);
in.read(byteBuf);
in.close();
str=new?String(byteBuf);
ta.setText(str);
setTitle("記事本?-?"+fileName);
}
catch(IOException?ioe)
{
}
}
if?(e.getSource()==save)//?保存文件
{
fileDlg=new?FileDialog(this,"保存文件",FileDialog.SAVE);
fileDlg.show();
fileName=fileDlg.getFile();
str=ta.getText();
byteBuf=str.getBytes();
try
{
FileOutputStream?out=new?FileOutputStream(fileName);
out.write(byteBuf);
out.close();
}
catch(IOException?ioe)
{
}
}
if(e.getSource()==sealset)//?打印頁面設置
{
prt.printDialog();
}
if(e.getSource()==seal)//?打印
{
try
{
prt.print();
}
catch(Exception?ew)
{
}
}
if?(e.getSource()==exit)//?退出
System.exit(0);
if?(e.getSource()==copy)//?復制
{
str=ta.getSelectedText();
}
if?(e.getSource()==cut)//?剪切
{
str=ta.getSelectedText();
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
if?(e.getSource()==paste)//?粘貼
{
p=ta.getCaretPosition();
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
ta.insert(str,p);
}
if(e.getSource()==delete)//?刪除
{
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
if(e.getSource()==all)//?全選
{
ta.selectAll();
}
if(e.getSource()==time)//?插入日期/時間
{
java.text.SimpleDateFormat?formatter=new?java.text.SimpleDateFormat("yyyy/MM/dd?HH:mm:ss");
java.util.Date?currentTime=new?java.util.Date();
String?datetime=formatter.format(currentTime).toString();
ta.setText(ta.getText()+datetime);
}
}
}
總結
以上是生活随笔為你收集整理的java 记事本换行_[求助]记事本自动换行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 徐静蕾电脑字体(徐静蕾字体是什么字体)
- 下一篇: 一键生图快捷键(一键保存图片快捷键)