好吧,当我做一个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]
Run Code Online (Sandbox Code Playgroud)
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 ) );
}
}
Run Code Online (Sandbox Code Playgroud)
所有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 );
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题出了什么问题?为什么说这是无效的.
好的,我看到下面的一个答案,并认为我会添加更多信息:
好的,但我遇到的问题是我将它添加到JScrollPane并且它没有显示出来.
我有一个大型数据类,其中包含由我绘制的JLabel表示的"框"的所有重要信息.例如,我需要从绘制的JLabel转换为它所代表的工程图的缩放坐标.无论如何,我曾经把数据类作为JLabel(或MyAlphaForLabel)的扩展,它运行良好并出现在JScrollPane上.当我想将数据导出到XML时,我遇到了问题,因为无论我使用Accessor做什么都没有,它正在接收父类.因此,我将"JLabel"移动到一个未由XML导出的数据记录中.我现在只需添加JLabel,而不是将整个数据记录添加到JScrollPane.完全相同的代码只更改选择了类的公共变量,而不是在pane.add()语句之前的整个类.但我不再让JLabel在JScrollPane上显示了.我不知道为什么.
有什么想法吗?
我意识到代码不在这里,但它确实相当于大量的代码.