2012-04-23 25 views
1

好吧,我有當我做了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上。我不知道爲什麼。

有什麼想法?

我意識到代碼不在這裏,但它確實代表了大量的代碼。

+0

哎呀,我忘了,我改變了「MyLabel」到「MyAlphaForLabel」類,看看是否能固定任何東西,它沒有。我從第二行獲得相同的文本,但它只是說「MyAlphaForLabel」。 – 2012-04-23 16:52:14

回答

6

,僅表示您的組件沒有被佈局管理器驗證尚未

+0

好吧,那還不錯,那麼......對吧? – 2012-04-23 17:00:51

+0

@ user1351827對 – ControlAltDel 2012-04-23 17:06:12