目前我正在研究 DEVSJAVA 模型并尝试为这些模型制作 GUI。在下面的代码中,我编写了一个displayphase类。在这个类中,每当有新输入到达时,它都会进入delext 函数,然后读取变量“结果”的值并调用showstate 函数,然后它应该将结果绘制在JFrame,当另一个输入到达时,它应该在 JFrame 上重新绘制。
但是在我编写的代码中,所有面板都打印在 JFrame 上,而不是重新绘制它。我知道错误在于每次进入 showstate 函数时都添加新面板。但我无法让它工作。请帮我解决这个错误。
package assignment2;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import simView.*;
import genDevs.modeling.*;
import genDevs.simulation.*;
import GenCol.*;
import simView.*;
public class displayphase extends ViewableAtomic{//ViewableAtomic is used instead
//of atomic due to its
//graphics capability
protected entity job;
protected double cul,cll,hul,hll,temp,varout,output,a,b,c,d,g;
protected double processing_time,temperature,temp1,temp2;
protected boolean acStatus,heaterStatus;
protected String result;
**JFrame f=new JFrame();**
public displayphase(){
this("displayphase"
);
}
public displayphase(String name){
super(name);
addInport("in");
addOutport("out");
addInport("in1");
addOutport("out1");
addOutport("out2");
addOutport("out3");
addRealTestInput("in1",71);
addRealTestInput("in",75);
addTestInput("in",new job("job","coolair",72.5),1);
addTestInput("in",new job("job",true,false,60),0);
addRealTestInput("in",3,1);
// processing_time = Processing_time;
}
public void initialize(){
phase = "passive";
sigma = INFINITY;
a=0;b=0;c=0;d=0;g=0;
job = new entity("job");
super.initialize();
}
public void deltext(double e,message x)
{
Continue(e);
if (phaseIs("passive"))
for (int i=0; i< x.getLength();i++)
if (somethingOnPort(x,"in"))
{ job dummy;
entity ent = x.getValOnPort("in",i);
// doubleEnt tem = (doubleEnt) job;
dummy = (job) ent;
temp= dummy.getthetemperature();
result = dummy.getthestate();
heaterStatus = dummy.isHeaterStatus();
System.out.println("The phase in ac"+result);
temperature = temp;
holdIn("busy",processing_time);
}
if (phaseIs("passive"))
for (int i=0; i< x.getLength();i++)
if (somethingOnPort(x,"in1"))
{ job dummy;
entity ent = x.getValOnPort("in1",i);
// doubleEnt tem = (doubleEnt) job;
dummy = (job) ent;
temp= dummy.getthetemperature();
result = dummy.getthestate();
System.out.println("The phase in heater"+result);
heaterStatus = dummy.isHeaterStatus();
temperature=temp;
holdIn("busy",processing_time);
}
**showstate()**;
}
public void deltint( )
{
passivate();
}
public void deltcon(double e,message x)
{
/*deltint();
deltext(20,x);
*/}
public message out( )
{
return outputNameOnPort(result,"out");
}
**public void showstate(){
f.add(new MyPanel());
f.repaint();
f.pack();
f.setVisible(true);
}
class MyPanel extends JPanel {
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
String ac,ab;
if(result == "aboveHT" || result=="coolAir")
ac = "cooler on";
else
ac = "cooler off";
/* else if(result == "belowHT" || result == "passive" || result=="belowH")
ac = "cooler off";*/
if(result == "belowHt" )
ab = "Heater on";
else
ab="Heater off";
//String ac=String.valueOf(timesacon());
// JFrame f=new JFrame();
JLabel l=new JLabel("THE STATUS OF AC IS ",JLabel.CENTER);
JLabel p=new JLabel("THE STATUS OF HEATER IS",JLabel.CENTER);
/* p.setVerticalTextPosition(JLabel.BOTTOM);
p.setHorizontalTextPosition(JLabel.CENTER);*/
JTextField t=new JTextField("");
t.setSize(80, 40);
t.setText(ac);
Point p1=new Point(100, 100);
t.setLocation(100,100);
l.setLocation(80, 80);
JTextField v=new JTextField("");
v.setSize(80,40);
v.setText(ab);
Point p2=new Point(100,200);
v.setLocation(p2);
p.setLocation(80, 180);
this.add(l);
this.add(p);
this.add(t);
this.add(v);
this.setSize(500, 300);
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.pack();
this.setVisible(true);
}
}
}**