-1

目前我正在研究 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);

    }
    }
      }**
4

1 回答 1

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);

    }        
}

StringJava中的比较是通过该String#equals方法完成的。使用==只是比较对象内存引用,它存在的可能性非常低true

在不了解更多信息的情况下,我会创建一个 a 的实例JFrame,添加到它并在您需要更改它的状态时MyPanel简单地调用它。MyPanel#updateState

现代 UI 预计能够在各种平台上运行,即使在运行相同的操作系统时,字体的呈现方式也会有所不同,从而使任何静态放置和大小的组件都无法使用。相反,您应该使用布局管理器 API,该 API 旨在以最少的工作量解决此问题。

请参阅在容器中布置组件

于 2013-10-29T22:23:44.183 回答