服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

通过---java程序--打开文本!


  import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class jfilechooser extends jframe implements actionlistener
{
//===================================================
jbutton button;
jtextarea text;
jtextpane textpane;
fileinputstream readfile;
jscrollpane scroll;
container con;
jfilechooser chooser=new jfilechooser();
//==========================================

public jfilechooser()
{
super("有文件选择器的窗口");
button=new jbutton("打开文件选择器");
button.addactionlistener(this);
textpane=new jtextpane();
scroll=new jscrollpane(textpane);

setsize( new dimension(400,400));
setvisible(true);
con=getcontentpane();
con.add(button,borderlayout.north);
con.add(scroll,borderlayout.center);
addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
dispose();system.exit(0);}});
}
//====================================================
public void actionperformed(actionevent e)
{
if(e.getsource()==button)
{
string s;
int state =chooser.showopendialog(null);
file file=chooser.getselectedfile();
if( file != null && state==jfilechooser.approve_option)
{
try{
readfile=new fileinputstream(file);
textpane.read(readfile,this);
}

catch(ioexception e1)
{
e1.printstacktrace();
}

}
}
}
//======================================================
}

--------------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

class jfilechooser extends jframe implements actionlistener
{
//===================================================
jbutton button;
jtextarea text;
jtextpane textpane;
fileinputstream readfile;
jscrollpane scroll;

jfilechooser chooser=new jfilechooser();
jpanel con;
//==========================================

public jfilechooser()
{
super("有文件选择器的窗口");
button=new jbutton("打开文件选择器");
button.addactionlistener(this);
textpane=new jtextpane();
scroll=new jscrollpane(textpane);

//setsize( new dimension(400,400));
//setvisible(true);
con=(jpanel)this.getcontentpane();
con.add(button,borderlayout.north);
con.add(scroll,borderlayout.center);
addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
dispose();system.exit(0);}});
}
//====================================================
public void actionperformed(actionevent e)
{
if(e.getsource()==button)
{
string s;
int state =chooser.showopendialog(null);
file file=chooser.getselectedfile();
if( file != null && state==jfilechooser.approve_option)
{
try{
readfile=new fileinputstream(file);
textpane.read(readfile,this);
}

catch(ioexception e1)
{
e1.printstacktrace();
}

}
}
}
//======================================================
}

public class mywindow
{
public static void main(string args[])
{

jfilechooser win=new jfilechooser();
win.setsize( new dimension(400,400));
win.pack();
win.validate();
win.show();
dimension screensize = toolkit.getdefaulttoolkit().getscreensize();
dimension framesize = win.getsize();
if (framesize.height > screensize.height)
{
framesize.height = screensize.height;
}
if (framesize.width > screensize.width) {
framesize.width = screensize.width;
}
win.setlocation((screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2);

}
}
================================================================================================================================================

注意:
====
====
chooser对象不能在构造函数中初始化!
应该在成员变量中初始化:
jfilechooser chooser=new jfilechooser();

大家可以编译测试!可以讨论!因为处理方法有多种!
这篇文章我曾经也发表在www.51cmm.com上
=========================================
有问题,可以发email: dw5189@163.com
==========================================

提供最新版的jcreator pro build 3.00.094(中文显示不好)

http://www.jcreator.com/download.htm
------------------------------------
username: p145460-brian luke

key: 00he3t-mx6f41-7e9cfn-68745a-rd1paj-0gkkpp-yyjvdr-cxjxhg-6gfxx8

扫描关注微信公众号