Inner Class Suspend og Resume
Hey Godt nytår!!!Jeg har en en inner class som jeg skal have til at suspende og resume via en knap. Mit problem er at jeg ikke kan komme i kontakt med den. Min fil ser ud som følger:
public class RightPanel extends JPanel
{
………
public RightPanel()
{
………………………………………..
//makes the StepCheck CheckBox
StepCheck = new JCheckBox("Stepvis");
StepCheck.setBackground (Color.gray);
StepCheck.setHorizontalAlignment(JCheckBox.CENTER);
StepCheck.setEnabled (false);
StepCheck.addActionListener(new ButtonListener());
// Opretter knapper
start = new JButton("Start");
step = new JButton("Next step");
stop = new JButton("Stop");
step.setEnabled (false);
stop.setEnabled (false);
start.addActionListener(new ButtonListener());
stop.addActionListener(new ButtonListener());
step.addActionListener(new ButtonListener());
// Tilf�jer knapper til panel
ControlRightDownDownPanel.add (StepCheck);
ControlRightDownDownPanel.add (start);
ControlRightDownDownPanel.add (stop);
ControlRightDownDownPanel.add (step);
}
class Consumer extends Thread
{
opgave2b producer;
Consumer(opgave2b p) {
producer = p;
}
private int i=0;
public void run() {
int a,b,d,n;
int x,y;
String[] tal;
try {
while ( true ) {
String message = producer.getMessage();
//System.out.println("Got message: " + message);
_ //guiFrame.Textbox.append ("f(x,y) : "+ message+" \n");
tal = message.split (" ");
RightPanel.Textbox.append ("f(x,y) : " + tal[5] + "\n");
RightPanel.Textbox.setCaretPosition( RightPanel.Textbox.getText().length() );
a = (int) Double.parseDouble(tal[0]);
b = (int) Double.parseDouble(tal[1]);
d = (int) Double.parseDouble(tal[2]);
x = (int) Double.parseDouble(tal[3]);
y = (int) Double.parseDouble(tal[4]);
i = Integer.parseInt(tal[6]);
n = (int) Double.parseDouble(tal[7]);
grafer(a,b,d,x,y,i,n);
_
sleep( 200 );
holdt();
_// start.setEnabled (false);
}
}
catch( InterruptedException e ) { }
}
public void holdt () throws InterruptedException
{
if (stepping==0){
if (run ==1){
stop();}
}
else{
System.out.println("DET er run: " + run);
if (run ==0){
run=2;
System.out.println("DET er run: I 0 " + run);}
if (run ==1){
stop();}
if (run ==2){
suspend();
sleep(5000);
System.out.println("DET er run: I SUSPEND " + run);
run=3;
}
if (run ==3){
resume();
System.out.println("DET er run: I RESUMED " + run);
//run=2;
}
}
}
}
public synchronized void grafer (int a,int b, int d, int x, int y,int i,int n) throws InterruptedException
{
Graph o = new Graph();
Graph.go go = o.new go();
go.inner(a,b,d,x,y,i,n);
_//_repaint();
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(event.getSource() == start)
{
run = 0;
stepping = 0;
stop.setEnabled (true);
StepCheck.setEnabled (true);
RightPanel.Textbox.setText(null);
opgave2b producer = new opgave2b();
producer.start();
//new Consumer( opgave2b ).start();
new Consumer( producer ).start();
int length = filename.length();
//System.out.println(testing);
if (length == 0){
RightPanel.log.append("You have not choosen a .fac file \nso eilon51.fac is beeing used as \ndefault\n\n");
filename = "eilon51.fac";
opgave2b.filename = filename;
//Graph.filename = filename;
}
else{
opgave2b.filename = filename;
//Graph.filename = filename;
}
if (opgave2b.vaegt == 0)
{
RightPanel.log.append("Also, you have not choosen a \nscenario therefore scenario 1 is \nset as default\n");
opgave2b.vaegt = 1;
}
//System.out.println(comeon);
repaint();
}
if(event.getSource() == stop)
{
run = 1;
StepCheck.setEnabled (false);
}
_
if(event.getSource() == step)
{
run = 3;
System.out.println("Step");
}
_
if(event.getActionCommand().equals("1"))
{
System.out.println("valgt nummer 1");
opgave2b.vaegt = 1;
}
if(event.getActionCommand().equals("2"))
{
System.out.println("valgt nummer 2");
opgave2b.vaegt = 2;
}____
if(event.getActionCommand().equals("3"))
{
System.out.println("valgt nummer 3");
opgave2b.vaegt = 3;
}
_
if(StepCheck.isSelected())
{
start.setEnabled (false);
step.setEnabled (true);
stepping=1;
//run = 3;
//System.out.println(stepping);
}
else
{
start.setEnabled (true);
step.setEnabled (false);
stepping=0;
//System.out.println(stepping);
}
}
}
}
Det er i klassen Consumer under metoden run jeg skal have den til at suspende, ved at trykke en knap. Bagefter ved at trykke en knap skal tråden resumes() og så suspends() igen.
Håber der er nogle der kan hjælpe!!
På forhånd tak