1

JTabbedPane 内で JPanel を描画しているときに、問題を理解し解決するのに苦労しています。現在、JTabbedPane には 2 つのタブがあり、それぞれに JPanel (拡張) が含まれており、開始ボタンを押すと javax.swing.Timer が開始し、1 つの JPanel にグラフを 1 秒ごとに新しい行で描画します。タイマーが実行されてパネルに描画されている間に、他のタブ (まだ空) を選択すると、選択したパネルで drawString メソッドが描画を開始することがわかります。このパネルには、drawString メソッドへの呼び出しが含まれていません。関連コード:

public class Monitor extends JPanel{

**private Timer timer=new Timer(1000,new PerformanceEvent(this));**


public Monitor(){

    this.setBackground(Color.BLACK);
    this.setPreferredSize(new Dimension(400,211));
    this.setBounds(new Rectangle(16,44,400,211));
    this.setVisible(true);

}

/**
 * This method contains the Graphoc tool to draw on the panel
 * @param g
 */
 public void analize(Graphics g){

if((ammountOfTimesAnalizeCalled / 10)==1){


  g.setColor(Color.red);
  g.drawLine(left1, high1, left2, high2);
  //print high2 variable on file
  Performance.report(high2);

  prepareForNext();
  ammountOfTimesAnalizeCalled++;

  System.out.println(ammountOfTimesAnalizeCalled);
}


 /**
  * This method is used by the GUI to start the timer<br/>
  * The timer starts and will  calls analize
  */
 public void start(){

     timer.start();
 }

     /**
      * This method stops the TimerTask
      */
     public void stop(){
         timer.stop();
     }

}

analize メソッドの機能については報告していません。これは長くて関連性がないためです。すべて drawString によって実行されます。ここでは、jtabbed ペインの 2 番目のタブに設定されている空のパネルです。ご覧のとおり、まだ何も実行していません。

public class Informations extends JPanel{

/**
 * The constructor initializes the panel's appearence
 */
public Informations(){

    this.setBackground(Color.BLACK);
    this.setPreferredSize(new Dimension(400,211));
    this.setBounds(new Rectangle(16,44,400,211));
    this.setVisible(true);
}

}

JTabbedPane

public class MyPane extends JTabbedPane{

private Monitor monitor=new Monitor();
private Informations informations=new Informations();

public MyPane(){
    monitor.setBounds(new Rectangle(5, 5, 400, 211));
    this.setPreferredSize(new Dimension(420,250));
    this.setBounds(new Rectangle(16,44,420,250));
    this.setVisible(true);
    this.addTab("Performance", monitor);
    this.addTab("Informations", informations);
}

}

そして、これはタイマーのイベント ハンドラーです。

public class PerformanceEvent implements ActionListener{

private Monitor monitor=null;

public PerformanceEvent(Monitor monitor){

    this.monitor=monitor;
}

/**
 * Perform the drawing action
 */
public void actionPerformed(ActionEvent event) {

    monitor.analize(monitor.getGraphics());
}

}

アドバイスいただければ幸いです、thx

4

1 に答える 1

0

あなたの問題は確かに奇妙です。上記のコードから、すべてがうまくいっているように見えます。でも、何かテストしてもらえますか?描画は、JPanel を拡張する Monitor クラス内でのみ行われるため、Graphics オブジェクトをスローする代わりに、Graphics g = getGraphics(); を使用してみてください。「analize()」メソッド内で Graphics オブジェクトを取得します。それが何かを解決するかどうかはわかりませんが、あるクラスが別のクラスに何かを渡すのは奇妙に思えますが、それはすでに持っています。

于 2013-12-06T20:08:57.547 に答える