好吧,我有当我做了MyBox.toString()即打印出来以下此JLabel上的内容无效吗?
com.swa.fin.esaa.sgb.myclasses.mydatatypes.MyAlphaForLabel[,0,0,0x0,invalid,hidden,alignmentX=0.0,alignmentY=0.0,[email protected],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()语句之前,完全相同的代码只改变了类public变量,而不是整个类。但是我没有让JLabel再显示在JScrollPane上。我不知道为什么。
有什么想法?
我意识到代码不在这里,但它确实代表了大量的代码。
哎呀,我忘了,我改变了“MyLabel”到“MyAlphaForLabel”类,看看是否能固定任何东西,它没有。我从第二行获得相同的文本,但它只是说“MyAlphaForLabel”。 – 2012-04-23 16:52:14