1

OK、 MyBox.toString() を実行すると次のように出力されます

com.swa.fin.esaa.sgb.myclasses.mydatatypes.MyAlphaForLabel[,0,0,0x0,invalid,hidden,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.MatteBorder@1082823,flags=25165824,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=0,height=0],defaultIcon=,disabledIcon=,horizontalAlignment=LEFT,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=TOP,verticalTextPosition=CENTER]

MyBox は次の方法でセットアップされます。

public void MySetMyBox( Rectangle bounds, Color color, Color bcolor, int bwidth ) {
    MyBox = new MyAlphaForLabel();
    MyBox.setComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT );
    MyBox.setVerticalAlignment( JLabel.TOP );
    MyBox.setHorizontalAlignment( JLabel.LEFT );
    MyBox.setFont( new Font(Font.MONOSPACED, Font.PLAIN, 12 ) );
    MyBox.setBackground( color );
    MyBox.setEnabled( true );
    MyBox.setVisible( MyVisible );
    MyBox.setFocusable( true );
    MyBox.setOpaque( MyOpaque );
    MyBox.setBounds( bounds );
    MyBox.setSize( new Dimension( bounds.width, bounds.height ) );
    MyBox.setPreferredSize( new Dimension( bounds.width, bounds.height ) );
    if ( ( MyDashed ) || ( MySelected ) && ( MyCellType != MYCELLS.ZOOM ) ) {
        MyBox.setBorder( new MyDashedBorder( bcolor, bwidth, bwidth ) );
    } else {
        MyBox.setBorder( new MatteBorder( bwidth, bwidth, bwidth, bwidth, bcolor ) );
    }
}

ここで、すべての MyXxxxx 変数が必要に応じて宣言されます (IE MyVisible は、この時点で true であるブール値です)。

MyBox は MyAlphaForLabel です。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.Serializable;
import javax.swing.JLabel;
import com.swa.fin.esaa.sgb.MyData;
import com.swa.fin.esaa.sgb.myclasses.myutils.MyDebug;

public class MyAlphaForLabel extends JLabel implements MyData, Serializable {

    private static final long serialVersionUID = 5522598090700895821L;

    private MyDebug           dbug                    = new MyDebug( MyData.MYDEBUGCHECK.MYALPHAFORLABEL.getOn() );

    public String MyTextString = "";

    public MyAlphaForLabel() {
        super();
        this.setOpaque( false );
    }

    /**
     *  Paint the background using the background Color of the
     *  contained component
     */
    @Override
    public void paintComponent( Graphics graphics ) {

        // Set color for the text label
        int red   = ( this.getBackground().getRed()   >= 50 ) ? ( this.getBackground().getRed()   - 50 ) : ( this.getBackground().getRed()   + 50 ); 
        int green = ( this.getBackground().getGreen() >= 50 ) ? ( this.getBackground().getGreen() - 50 ) : ( this.getBackground().getGreen() + 50 ); 
        int blue  = ( this.getBackground().getBlue()  >= 50 ) ? ( this.getBackground().getBlue()  - 50 ) : ( this.getBackground().getBlue()  + 50 ); 
        int alpha = 128; 

        // Make sure we are displaying something
        if ( this.getBackground().getAlpha() != 0 ) {
            // First draw rectangle
            graphics.setColor( this.getBackground() );
            graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
            // Then draw text label
            graphics.setColor( new Color( red, green, blue, alpha ) );
            graphics.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10 ) );
            graphics.drawString(MyTextString, 5, 15);
            dbug.Message( "MYALPHACONTAINER", "paintComponent set text to %s ", MyTextString );

        }
    }
}

私の質問は何が悪いのですか? なぜそれが無効だと言うのですか。

わかりました、私は以下の1つの答えを見て、さらに情報を追加すると思いました:

わかりましたが、私が抱えている問題は、それを JScrollPane に追加しても表示されないことです。

描画した JLabel で表される「ボックス」のすべての重要な情報を含む大きなデータ クラスがあります。たとえば、描画された JLabel から、それが表すエンジニアリング図面に変換するスケーリングされた座標が必要です。とにかく、以前はデータ クラスを JLabel (または MyAlphaForLabel) の拡張として使用していましたが、問題なく動作し、JScrollPane に表示されました。データを XML にエクスポートしようとしたときに問題が発生しました。これは、Accessor を NONE にして何をしても、親クラスが取得されたためです。そこで、「JLabel」を XML でエクスポートされていないデータ レコードの一部に移動しました。データ レコード全体を JScrollPane に追加する代わりに、JLabel を追加するだけです。まったく同じコードのみの変更が、pane.add() ステートメントの前のクラス全体ではなく、クラスのパブリック変数が選択されました。しかし、JScrollPane に JLabel が表示されなくなりました。理由はわかりません。

何かご意見は?

コードがここにないことは認識していますが、大量のコードになります。

4

1 に答える 1

6

これは、コンポーネントがまだレイアウトマネージャーによって検証されていないことを意味します

于 2012-04-23T16:50:58.563 に答える