国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

通過(guò)---JAVA程序--打開(kāi)文本!

2019-11-18 15:15:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  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("打開(kāi)文件選擇器");
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("打開(kāi)文件選擇器");
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對(duì)象不能在構(gòu)造函數(shù)中初始化!
應(yīng)該在成員變量中初始化:
JFileChooser chooser=new JFileChooser();

大家可以編譯測(cè)試!可以討論!因?yàn)樘幚矸椒ㄓ卸喾N!
這篇文章我曾經(jīng)也發(fā)表在www.51cmm.com上
=========================================
有問(wèn)題,可以發(fā)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

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 容城县| 始兴县| 剑川县| 潮安县| 靖远县| 宜兴市| 台州市| 浏阳市| 全椒县| 堆龙德庆县| 龙里县| 乡宁县| 顺昌县| 蓝田县| 玛沁县| 临城县| 安徽省| 吉林省| 兴山县| 罗田县| 徐汇区| 杭锦后旗| 沙坪坝区| 满城县| 龙岩市| 阿坝县| 城市| 潜山县| 广南县| 繁峙县| 长宁区| 分宜县| 嘉祥县| 天柱县| 读书| 灌阳县| 阿拉善右旗| 烟台市| 延安市| 泰兴市| 隆林|