0 から 1 の間のオブジェクトのメンバー フィールドに基づいて、JTextArea を部分的に埋めようとしています。paintComponent 関数でパーセンテージをハード コードすると、うまく機能します。しかし、メンバー フィールドをパーセンテージ値として使用しようとすると、デバッガーでは常に 0.0 になり、テキストの背後に四角形が描画されません。
内でメンバ フィールドが初期化されていないように見えるのはなぜpaintComponent()
ですか? を呼び出した後setPercent()
、percentFilled
正しいです。BarGraphText
(オブジェクトが呼び出された後、オブジェクトのコンテナーを無効にしますsetPercent()
。)
EDIT: setPercent()
ボタンによって ActionListener がトリガーされた後に呼び出されます。別の gui スレッドは、この失敗と関係がありますか? 以下のクラスが単独で JFrame にある場合に機能します。更新:ボタンでパーセントを変更してコンポーネントを再描画しても、別のプロジェクトにある場合は違いはありません。
解決済み:プログラムの間違った場所で値をクリアしていました。この質問は公開したままにします。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JTextArea;
public class BarGraphText extends JTextArea {
double percentFilled;
Color fillColor;
BarGraphText( String s )
{
super(s);
setOpaque(false);
percentFilled = 0.0;
}
@Override
public void paintComponent( Graphics g )
{
int width, height;
Rectangle bounds = g.getClipBounds();
if( bounds != null )
{
height = bounds.height;
width = bounds.width;
}
else
{
System.err.println("Error [BarGraphText]: Clipping bounds unknown.");
height = width = 0;
}
g.setColor(fillColor);
g.fillRect(0, 0, (int) (width*percentFilled), height);
super.paintComponent(g);
}
public void setPercent( int myResp, int totResp )
{
percentFilled = (float)myResp / totResp;
}
public void setColor( Color c )
{
fillColor = c;
}
}