2013-10-29 36 views
-1

目前我正在使用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); 

    } 
    } 
     }** 
+0

'「但在代碼我所寫使得在JFrame的所有面板打印,而不是重新繪製它。」「 - 這種說法混淆了我。如果您很快就沒有得到很好的答案,請考慮爲我們澄清您的問題描述。 –

+1

另外,請考慮格式化您的代碼,使您的縮進一致並有意義,並擺脫代碼中存在的**,因爲它們會分散注意力,妨礙代碼格式化。謝謝。 –

+2

關於,'if(result ==「aboveHT」|| result ==「coolAir」)' - 絕對不要使用==來比較字符串。使用'equals(...)'或'equalsIgnoreCase(...)'。 –

回答

2

請勿在任何paintXxx方法中修改任何組件的狀態。這將觸發級聯的repaint事件,這將繼續被調用,直到CPU處於熱點並且程序無響應...

paintXxx方法用於提供執行組件自定義繪製的功能,不修改其狀態。

相反,構造在構造的基本用戶界面和提供一些裝置,通過該字段的狀態是可以改變的,例如,經由updateState方法...在Java中

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

    }   
} 

String比較是通過String#equals方法完成。使用==是簡單地比較對象的內存引用,它曾經是true

不知道更多的可能性非常低,我想創建一個JFrame的實例,添加MyPanel給它簡單地調用MyPanel#updateState當你需要改變這是國家。

即使運行相同的操作系統,現代用戶界面也可以在各種平臺上運行,字體呈現方式也有所不同,從而使靜態放置的大小組件無法使用。相反,您應該使用佈局管理器API,該API旨在用最少的工作解決此問題。

Laying out components within a container

+0

非常感謝。它工作得很好。 – user2872321

+0

@ user2872321那麼,這是一個很好的改變;) – MadProgrammer