《Java编程思想》读书笔记(10)

None.gif packagecom.vitamin.UI;
None.gifimportjavax.swing.
* ;
None.gifimportjavax.swing.filechooser.FileFilter;
None.gif
None.gifimportjava.awt.BorderLayout;
None.gifimportjava.awt.Container;
None.gifimportjava.awt.GridLayout;
None.gifimportjava.awt.
event . * ;
None.gifimportjava.io.File;
None.gifimportjava.io.FileWriter;
None.gifimportjava.io.IOException;
None.gif
None.gifimportcom.vitamin.Console.
* ;
None.gif
None.gif
public class FileTestextendsJFrame
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
privateJButtonbtnOpen=newJButton("Open");
InBlock.gif
privateJButtonbtnSave=newJButton("Save");
InBlock.gif
privateJPanelp=null;
InBlock.gif
privateJTextFieldtfDir=newJTextField();
InBlock.gif
privateJTextFieldtfFileName=newJTextField();
InBlock.gif
privateJScrollPanesp=newJScrollPane();
InBlock.gif
privateJEditorPaneep=newJEditorPane();
InBlock.gif
privateFilefile=null;
InBlock.gif
InBlock.gif
publicFileTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsuper();
InBlock.gif
//TODOAuto-generatedconstructorstub
InBlock.gif
this.initForm();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicvoidinitForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifContainercon
=this.getContentPane();
InBlock.gifcon.setLayout(
newBorderLayout());
InBlock.gif
this.tfDir.setEditable(false);
InBlock.gif
this.tfFileName.setEditable(false);
InBlock.gif
this.btnSave.setEnabled(false);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.btnOpen.addActionListener(newActionListener()dot.gif{
InBlock.gif
publicvoidactionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifJFileChooserfc
=newJFileChooser();
InBlock.giffc.setFileFilter(
newTextFileFilter());
InBlock.gif
intreturnVal=fc.showOpenDialog(FileTest.this);
InBlock.gif
if(returnVal==JFileChooser.APPROVE_OPTION)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffile
=fc.getSelectedFile();
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifep.setPage(file.toURL());
InBlock.giftfFileName.setText(file.getAbsolutePath());
InBlock.gifbtnSave.setEnabled(
true);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(IOExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
thrownewRuntimeException(ex);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(returnVal==JFileChooser.CANCEL_OPTION)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftfFileName.setText(
"你已经取消了文件的选择");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.btnSave.addActionListener(newActionListener()dot.gif{
InBlock.gif
publicvoidactionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gifFileWriterfw
=newFileWriter(file);
InBlock.gifep.write(fw);
InBlock.gifJOptionPane.showMessageDialog(FileTest.
this,"保存成功");
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(IOExceptionioe)dot.gif{
InBlock.gif
thrownewRuntimeException(ioe);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gifsp.getViewport().add(ep);
InBlock.gif
InBlock.gifp
=newJPanel();
InBlock.gifp.add(
this.btnOpen);
InBlock.gifp.add(
this.btnSave);
InBlock.gifcon.add(p,BorderLayout.SOUTH);
InBlock.gifcon.add(sp,BorderLayout.CENTER);
InBlock.gifp
=newJPanel();
InBlock.gifp.setLayout(
newGridLayout(2,1));
InBlock.gifp.add(
this.tfDir);
InBlock.gifp.add(
this.tfFileName);
InBlock.gifcon.add(p,BorderLayout.NORTH);
InBlock.gifconsole.run(
this,400,400);
ExpandedSubBlockEnd.gif}

InBlock.gif
publicclassTextFileFilterextends
ExpandedSubBlockStart.gifContractedSubBlock.gifjavax.swing.filechooser.FileFilter
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicbooleanaccept(Filef)dot.gif{
InBlock.gif
returnf.getName().endsWith(".txt")
InBlock.gif
||f.isDirectory();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicStringgetDescription()dot.gif{
InBlock.gif
return"TextFiles(*.txt)";
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**
InBlock.gif*@paramargs
ExpandedSubBlockEnd.gif
*/

InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifFileTestft
=newFileTest();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

做了个读写文件的小东西,功能很烂,有时间做个完整的记事本吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值