1

好的,我在执行 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 是一个布尔值,此时为真)。

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

        }
    }
}

我的问题是什么问题?为什么说无效。

好的,我看到了下面的一个答案,并认为我会添加更多信息:

好的,但我遇到的问题是我将它添加到 JScrollPane 并且它没有显示出来。

我有一个大型数据类,其中包含由我绘制的 JLabel 表示的“框”的所有重要信息。例如,我需要从绘制的 JLabel 转换为它所代表的工程图的缩放坐标。无论如何,我曾经让数据类成为 JLabel(或 MyAlphaForLabel)的扩展,它运行良好并出现在 JScrollPane 上。当我想将数据导出到 XML 时遇到了问题,因为无论我对 Accessor 为 NONE 做了什么,它都会获取父类。因此,我将“JLabel”移到了一段不是由 XML 导出的数据记录中。我现在只添加 JLabel,而不是将整个数据记录添加到 JScrollPane。在 pane.add() 语句中选择了类公共变量,而不是之前的整个类,只更改了完全相同的代码。但是我不再让 JLabel 显示在 JScrollPane 上。我不知道为什么。

有什么想法吗?

我意识到代码不在这里,但它确实相当于大量的代码。

4

1 回答 1

6

这只是意味着您的组件尚未由布局管理器验证

于 2012-04-23T16:50:58.563 回答