2012-02-14 97 views
0

我有一个奇怪的问题......按钮不会显示

我有一个JFrame,并增加了两个JInternalFrame的

我加入一个JPanel到内部框架中的一个。

我用jpanel的鼠标监听器写了一个代码,就像我放开鼠标时放置一个设备对象(一个类extendng jpanel)。

设备图像显示,但只有在我移动d内部框架或最大化它..为什么是达?

下面的代码为JPanel中被添加到内部框架中的一个......

public class Board extends JPanel implements MouseListener{ 
Device[] devices=new Device[10]; 

int X,Y,i=0; 
int j=10; 

Point p1,p2; 

ImageIcon icon; 
public Board() 
{ 
    setBackground(Color.WHITE); 
    this.addMouseListener(this); 
    setLayout(null); 


} 

public void mouseClicked(MouseEvent me){ 

} 
public void mouseExited(MouseEvent me){ 
} 
public void mouseEntered(MouseEvent me){ 

} 
public void mousePressed(MouseEvent me){ 
} 

public void mouseReleased(MouseEvent me){ 

    X=me.getX(); 
    Y=me.getY(); 

    icon=new ImageIcon("E:\\java\\ecadpb\\src\\ecadpb\\device"+Layout1.clicked+".png"); 

    devices[i].setBounds(X,Y,icon.getIconWidth(),icon.getIconHeight()+20); 
    devices[i].setVisible(true); 
    this.add(devices[i]); 
    this.repaint(X,Y,icon.getIconWidth(),icon.getIconHeight()+20); 
    i++; 
    } 

} 

} 

以下是设备类..

public Device(Point pos,JLabel label,Rectangle r,int input,int output){ 
    setVisible(true); 
    setBackground(Color.BLACK); 
    deviceNo=dno; 
    position=pos; 
    inputs=input; 
    outputs=output; 
    this.r=r; 

    label.setLayout(null); 
    setOpaque(false); 
    this.add(label); 
    label.addMouseListener(this); 
    dno++; 


} 

} 

回答

0

Swing也奠定了新组件层次结构。看看JComponent.revalidate()及其相关方法。

+0

奇妙!我叫revalidate()方法,它解决了这个问题..非常感谢! – 2012-02-14 19:14:37

+0

优秀 - 请将此答案标记为接受:) – 2012-02-14 19:15:36

0

打电话给你添加了一个组件后:

this.add(devices[i]); 
revalidate();