java 用户界面的问题
一个关于javaUGI和threat的问题!1.使用GUI建一个面板。里边有两个按钮,一个是开始,一个是停止。2.在这两个按钮下方建两个文本框,一个能实现输入文档名(TX...
一个关于java UGI和threat的问题!
1.使用GUI建一个面板。里边有两个按钮,一个是开始,一个是停止。
2.在这两个按钮下方建两个文本框,一个能实现输入文档名(TXT文件名)后按开始键判断文件夹里有没有这个文档。另一个是如果文件夹里有这个文档点击开始按钮,可以读这个文档里面的内容到这个文档框里。如果没有在这个文档框里显示没有此文档。3.点击停止键时,停止读文档。再点开始能继续读文档。
(会用到thread)
帮忙解决一下吧!谢谢了! 展开
1.使用GUI建一个面板。里边有两个按钮,一个是开始,一个是停止。
2.在这两个按钮下方建两个文本框,一个能实现输入文档名(TXT文件名)后按开始键判断文件夹里有没有这个文档。另一个是如果文件夹里有这个文档点击开始按钮,可以读这个文档里面的内容到这个文档框里。如果没有在这个文档框里显示没有此文档。3.点击停止键时,停止读文档。再点开始能继续读文档。
(会用到thread)
帮忙解决一下吧!谢谢了! 展开
2个回答
2010-12-17
展开全部
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Test extends JFrame {
private ReadFile read;
public Test() {
init();
}
private void init() {
final JTextArea jtaFile = new JTextArea();
final JTextArea jtaContent = new JTextArea();
JButton btnBegin = new JButton("开始");
btnBegin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (read == null) {
File file = new File(jtaFile.getText().trim());
if (!file.exists()) {
jtaContent.setText("文件不存在");
} else {
jtaContent.setText("文件存在");
read = new ReadFile();
read.setJtaFile(jtaFile);
read.setJtaContent(jtaContent);
read.setBegin(true);
read.start();
}
} else {
read.setBegin(true);
}
}
});
JButton btnStop = new JButton("停止");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (read != null) {
read.setBegin(false);
}
}
});
JPanel pnlTop = new JPanel(new GridLayout(1, 2));
pnlTop.add(btnBegin);
pnlTop.add(btnStop);
JPanel pnlCenter = new JPanel(new GridLayout(1, 2));
pnlCenter.add(jtaFile);
pnlCenter.add(jtaContent);
setLayout(new BorderLayout());
add(pnlTop, BorderLayout.NORTH);
add(pnlCenter, BorderLayout.CENTER);
this.setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args) {
new Test().setVisible(true);
}
}
class ReadFile extends Thread {
private boolean begin = false;
private JTextArea jtaContent;
private JTextArea jtaFile;
public void run () {
BufferedReader br = null;
try {
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(new File(jtaFile.getText().trim()))));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "";
while (true) {
if (begin) {
try {
str = br.readLine();
if (str == null) {
break;
}
jtaContent.setText(jtaContent.getText() + "\n" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 休眠1秒
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @return the begin
*/
public boolean isBegin() {
return begin;
}
/**
* @param begin the begin to set
*/
public void setBegin(boolean begin) {
this.begin = begin;
}
/**
* @param jtaContent the jtaContent to set
*/
public void setJtaContent(JTextArea jtaContent) {
this.jtaContent = jtaContent;
}
/**
* @param jtaFile the jtaFile to set
*/
public void setJtaFile(JTextArea jtaFile) {
this.jtaFile = jtaFile;
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Test extends JFrame {
private ReadFile read;
public Test() {
init();
}
private void init() {
final JTextArea jtaFile = new JTextArea();
final JTextArea jtaContent = new JTextArea();
JButton btnBegin = new JButton("开始");
btnBegin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (read == null) {
File file = new File(jtaFile.getText().trim());
if (!file.exists()) {
jtaContent.setText("文件不存在");
} else {
jtaContent.setText("文件存在");
read = new ReadFile();
read.setJtaFile(jtaFile);
read.setJtaContent(jtaContent);
read.setBegin(true);
read.start();
}
} else {
read.setBegin(true);
}
}
});
JButton btnStop = new JButton("停止");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (read != null) {
read.setBegin(false);
}
}
});
JPanel pnlTop = new JPanel(new GridLayout(1, 2));
pnlTop.add(btnBegin);
pnlTop.add(btnStop);
JPanel pnlCenter = new JPanel(new GridLayout(1, 2));
pnlCenter.add(jtaFile);
pnlCenter.add(jtaContent);
setLayout(new BorderLayout());
add(pnlTop, BorderLayout.NORTH);
add(pnlCenter, BorderLayout.CENTER);
this.setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args) {
new Test().setVisible(true);
}
}
class ReadFile extends Thread {
private boolean begin = false;
private JTextArea jtaContent;
private JTextArea jtaFile;
public void run () {
BufferedReader br = null;
try {
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(new File(jtaFile.getText().trim()))));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "";
while (true) {
if (begin) {
try {
str = br.readLine();
if (str == null) {
break;
}
jtaContent.setText(jtaContent.getText() + "\n" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 休眠1秒
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @return the begin
*/
public boolean isBegin() {
return begin;
}
/**
* @param begin the begin to set
*/
public void setBegin(boolean begin) {
this.begin = begin;
}
/**
* @param jtaContent the jtaContent to set
*/
public void setJtaContent(JTextArea jtaContent) {
this.jtaContent = jtaContent;
}
/**
* @param jtaFile the jtaFile to set
*/
public void setJtaFile(JTextArea jtaFile) {
this.jtaFile = jtaFile;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询