当前位置: 代码迷 >> Java相关 >> [求助]JPanel上怎么移除JLabe。l
  详细解决方案

[求助]JPanel上怎么移除JLabe。l

热度:258   发布时间:2007-06-15 22:15:22.0
[求助]JPanel上怎么移除JLabe。l
我在JPanle上加了几个JLabel,想移除几个JLabel,保留几个,这怎么办到呢?请高手帮帮忙,谢谢了!!
搜索更多相关的解决方案: JLabe  JPanel  

----------------解决方案--------------------------------------------------------
晕,你不会自己删代码吗?
----------------解决方案--------------------------------------------------------
哈哈,我是说,我点击一个按狃,然后在事件处理时移除,你理解错了。。哈哈哈
----------------解决方案--------------------------------------------------------
贴代码上来看看,大家才好帮忙嘛
----------------解决方案--------------------------------------------------------

package paint;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* @author Administrator
*
*/
public class Tools extends JPanel implements ActionListener{



static Color color;
FlowLayout layout=new FlowLayout( );

//private JButton[] jButtons= new JButton[8];
private JButton lineLabel ;
private JButton rectLabel ;
private JButton polyLabel ;
private JButton ovalLabel ;
private JButton rrectLabel;
private JButton textLabel ;
private JButton penLabel;
private JButton brushLabel;

//private Icon[] iCons=new Icon[8];

Icon lineBug = new ImageIcon( getClass().getResource( "image/bug1.JPG" ) );
Icon rectBug = new ImageIcon( getClass().getResource( "image/bug2.JPG" ) );
Icon polyBug = new ImageIcon( getClass().getResource( "image/bug3.JPG" ) );
Icon ovalBug = new ImageIcon( getClass().getResource( "image/bug4.JPG" ) );
Icon rrectBug = new ImageIcon( getClass().getResource( "image/bug5.JPG") );
Icon textBug = new ImageIcon( getClass().getResource( "image/bug6.JPG" ) );
Icon brushBug = new ImageIcon(getClass().getResource( "image/bug7.JPG" ));
Icon penBug = new ImageIcon(getClass().getResource( "image/bug8.JPG" ));
/*for(int i=0;i<iCons.length;i++){
iCons[i]=new ImageIcon( getClass().getResource( "image/bug"+(i+1)+".JPG" ) );
}*/

private Dimension dem = new Dimension(lineBug.getIconWidth(),lineBug.getIconHeight());

Icon emBug = new ImageIcon(getClass().getResource( "image/embug.JPG" ));
Icon emrBug = new ImageIcon(getClass().getResource( "image/emrbug.JPG" ));
Icon fullBug = new ImageIcon(getClass().getResource( "image/fullbug.JPG" ));
private JButton emLabel = new JButton(emBug);
private JButton emrLabel= new JButton(emrBug);
private JButton fullLabel = new JButton(fullBug);




public Tools(){

setLayout( layout );


setBackground( Color.WHITE);
setBounds(0,4,61,500);


penLabel = new JButton(penBug);
penLabel.setPreferredSize(dem);
add(penLabel);
penLabel.setVisible(true);

brushLabel = new JButton(brushBug);
brushLabel.setPreferredSize(dem);
add(brushLabel);
brushLabel.setVisible(true);

lineLabel= new JButton(lineBug);
lineLabel.setPreferredSize(dem);
add(lineLabel);
lineLabel.setVisible(true);

rectLabel= new JButton(rectBug);
rectLabel.setPreferredSize(dem);
add(rectLabel);
rectLabel.addActionListener(this);
rectLabel.setVisible(true);

polyLabel= new JButton(polyBug);
polyLabel.setPreferredSize(dem);
add(polyLabel);
polyLabel.addActionListener(this);
polyLabel.setVisible(true);

ovalLabel = new JButton();
ovalLabel.setIcon(ovalBug);
ovalLabel.setPreferredSize(dem);
add(ovalLabel);
ovalLabel.addActionListener(this);
ovalLabel.setVisible(true);

rrectLabel= new JButton(rrectBug);
rrectLabel.setPreferredSize(dem);
add(rrectLabel);
rrectLabel.addActionListener(this);
rrectLabel.setVisible(true);

textLabel= new JButton(textBug);
textLabel.setPreferredSize(dem);
add(textLabel);
textLabel.setVisible(true);
/*for(int i=0;i<jButtons.length;i++){
jButtons[i]=new JButton(iCons[i]);
jButtons[i].setPreferredSize(dem);
add(jButtons[i]);
jButtons[i].addActionListener(this);

}*/

emrLabel.addActionListener(this);
emLabel.addActionListener(this);
fullLabel.addActionListener(this);


}


public void actionPerformed(ActionEvent e){
Object o = e.getSource();
if(o == lineLabel){
我想在这里把添加的Label去掉。。
remove(emLabel);
remove(emrLabel);
remove(fullLabel);//这个移除不了。。。
validate();

}
else if(o == rectLabel){
add(emLabel);
add(emrLabel);
add(fullLabel);
validate();


}
else if(o ==polyLabel){

add(emLabel);
add(emrLabel);
add(fullLabel);
validate();

}
else if(o == ovalLabel){

//handle.getDrawJpanel().setLineTag(true);

add(emLabel);
add(emrLabel);
add(fullLabel);

validate();

}
else if(o == rrectLabel){

add(emLabel);
add(emrLabel);
add(fullLabel);
validate();


}
else if(o == textLabel){

}
else if(o==penLabel){
//handle.getremove()
}
else if(o==emLabel){


}
else if(o==emrLabel){


}
else if(o== fullLabel){



}






}
}

[此贴子已经被作者于2007-6-16 11:28:31编辑过]


----------------解决方案--------------------------------------------------------
请各位帮忙啊,我先谢谢了。。
----------------解决方案--------------------------------------------------------

jpanel.remove(jlabel);


----------------解决方案--------------------------------------------------------
谢谢冰封大哥了。。。。。
----------------解决方案--------------------------------------------------------

遇到一些问题要学会自己查看API,其实里面的说明很详细的


----------------解决方案--------------------------------------------------------
可以移除,为什么有一个按钮不能移除呢,只能移除两个?千里大哥麻烦你帮个忙。。
----------------解决方案--------------------------------------------------------