目前我正在开发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);
}
}
}**
答案 0 :(得分:2)
请勿在任何paintXxx
方法中修改任何组件的状态。这将触发一系列repaint
事件,这些事件将继续被调用,直到您的CPU运行正常并且您的程序没有响应...
paintXxx
方法用于提供对组件执行自定义绘制的功能,而不是修改其状态。
相反,在构造函数中构造基本UI并提供一些方法,通过这些方法可以更改字段的状态,例如,通过updateState
方法...
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class MyPanel extends JPanel {
private JTextField t;
private JTextField v;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
JLabel l = new JLabel("THE STATUS OF AC IS ", JLabel.CENTER);
JLabel p = new JLabel("THE STATUS OF HEATER IS", JLabel.CENTER);
t = new JTextField("");
v = new JTextField("");
this.add(l);
this.add(p);
this.add(t);
this.add(v);
}
public void updateState(String result) {
String ac, ab;
if ("aboveHT".equals(result) || "coolAir".equals(result)) {
ac = "cooler on";
} else {
ac = "cooler off";
}
if ("belowHt".equals(result)) {
ab = "Heater on";
} else {
ab = "Heater off";
}
t.setText(ac);
v.setText(ab);
}
}
Java中的 String
比较是通过String#equals
方法完成的。使用==
只是比较对象内存引用,它的可能性非常低true
我不知道更多,我会创建一个JFrame
的实例,向其添加MyPanel
,只需在需要更改状态时调用MyPanel#updateState
。
现代UI预计能够在各种平台上运行,即使在运行相同的操作系统时,字体的呈现方式也有所不同,使得任何静态放置和大小的组件都无法使用。相反,您应该使用布局管理器API,该API旨在解决此问题,而您只需要最少的工作。