开始和停止按钮都只能使用一次,我想点击开始就读取数据显示出来,然后点击停止就暂停,数据不消失,然后再点击开始,又可以开始继续读取数据,如此循环,请问如何做到呢??代码如下:package mainGui;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument.Content;
@SuppressWarnings({ "unused", "serial" })
class Gui extends Thread {
private boolean isPaused=true;
private JFrame frame = new JFrame("Demo");
private Label name;
private TextArea content;
private JButton start=new JButton("start");
private JButton stop=new JButton("stop");
public Gui(){
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }});
frame.setLayout(null);
frame.setSize(480,620);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setFocusable(false);
start.setBounds(180, 520, 100, 50);
stop.setBounds(300, 520, 100, 50);
content=new TextArea("");
content.setBounds(10, 110, 460, 400);
frame.add(content);
frame.add(start);
frame.add(stop);
start.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if (isPaused==true) {
isPaused = false;
stop.setEnabled(true);
//notify();
}
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if (isPaused==false) {
isPaused = true;
stop(); }}});
start();
}
@Override
public void run() {
while (true) {
while(isPaused);
File file=new File("F://text2.txt");
if(file.exists()&&file.isFile()){
try{
BufferedReader input=new BufferedReader(new FileReader(file));
String text;
while((text=input.readLine())!=null){
content.append(String.format("%s%n", text));
}
}catch(IOException e1){
e1.printStackTrace();
System.out.println("file error");
}
}
}
}
public static void main(String [] args){
Gui gui=new Gui();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument.Content;
@SuppressWarnings({ "unused", "serial" })
class Gui extends Thread {
private boolean isPaused=true;
private JFrame frame = new JFrame("Demo");
private Label name;
private TextArea content;
private JButton start=new JButton("start");
private JButton stop=new JButton("stop");
public Gui(){
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }});
frame.setLayout(null);
frame.setSize(480,620);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setFocusable(false);
start.setBounds(180, 520, 100, 50);
stop.setBounds(300, 520, 100, 50);
content=new TextArea("");
content.setBounds(10, 110, 460, 400);
frame.add(content);
frame.add(start);
frame.add(stop);
start.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if (isPaused==true) {
isPaused = false;
stop.setEnabled(true);
//notify();
}
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
if (isPaused==false) {
isPaused = true;
stop(); }}});
start();
}
@Override
public void run() {
while (true) {
while(isPaused);
File file=new File("F://text2.txt");
if(file.exists()&&file.isFile()){
try{
BufferedReader input=new BufferedReader(new FileReader(file));
String text;
while((text=input.readLine())!=null){
content.append(String.format("%s%n", text));
}
}catch(IOException e1){
e1.printStackTrace();
System.out.println("file error");
}
}
}
}
public static void main(String [] args){
Gui gui=new Gui();
}
}
public synchronized void todo(){
for(int i = 0 ; i <= 10000 ; i ++){
if(condition){
try{
wait();
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
System.out.println("线程运行中:" + i);
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
} public void setCondition(boolean condition){
this.condition = condition;
} public synchronized void wakeUp(){
notifyAll();
// System.out.println("Finish wake up");
} public static void main(String[] args){
} private boolean condition = false;
}
import javax.swing.JToggleButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
public class MyButton extends JToggleButton implements ActionListener{
public MyButton(String text,Job job){
super(text);
this.job = job;
addActionListener(this);
} @Override
public void actionPerformed(ActionEvent event){
if(isSelected()){
job.setCondition(false);
new Thread(new Runnable(){
@Override
public void run(){
job.wakeUp();
}
}).start();
if(thread == null){
thread = new Thread(new Runnable(){
@Override
public void run(){
job.todo();
}
});
thread.start();
}
setText("线程执行中,点击暂停");
setBackground(Color.YELLOW);
}else{
setText("线程暂停,点击继续执行.");
setBackground(Color.RED);
job.setCondition(true);
}
}
private Job job = null;
private Thread thread = null;
}
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Container;public class Test{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createWindow();
}
});
}
private static void createWindow(){
JFrame window = new JFrame("Thread");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
window.setSize(screenSize.width / 2,screenSize.height / 2);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = window.getContentPane();
content.setLayout(new FlowLayout());
Job job = new Job();
MyButton button = new MyButton("点击启动线程",job);
content.add(button);
window.setVisible(true);
}
}