目前我正在使用DEVSJAVA模型並嘗試爲這些模型創建GUI。 在下面的代碼我已經寫了displayphase class.In這個類,每當一個新的輸入到達它進入delext功能,然後讀取的varaible「結果」的值,並調用showstate功能,然後它應該在JFrame上繪製結果,當另一個輸入到達時它應該在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);
}
}
}**
'「但在代碼我所寫使得在JFrame的所有面板打印,而不是重新繪製它。」「 - 這種說法混淆了我。如果您很快就沒有得到很好的答案,請考慮爲我們澄清您的問題描述。 –
另外,請考慮格式化您的代碼,使您的縮進一致並有意義,並擺脫代碼中存在的**,因爲它們會分散注意力,妨礙代碼格式化。謝謝。 –
關於,'if(result ==「aboveHT」|| result ==「coolAir」)' - 絕對不要使用==來比較字符串。使用'equals(...)'或'equalsIgnoreCase(...)'。 –