通过---JAVA程序--打开文本!
2008-01-05 08:40:41 来源:WEB开发网核心提示:import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class Jfilechooser extends JFrame implements ActionListener{//===JButton
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
更多精彩
赞助商链接