首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

这个功能如何实现

2012-01-13 
这个功能怎么实现?一个程序要求:26个显示内容为英文单词的名字的按钮(按钮变量button1名为a,按钮变量butoo

这个功能怎么实现?
一个程序要求:
26个显示内容为英文单词的名字的按钮(按钮变量button1名为a,按钮变量butoon2为b...到z)
我用了actionlistener,
我这这样写的
public   void   actionPerformed(ActionEvent   al)
{
    for(i=0;i <=25;i++)
    {
    if(al.getActionCommand().equals( " "+(char)(97+i)))
    {
       ...这里怎么实现哦 我的错误的
实现功能是:单击某个按钮使这个按钮,本来为显示的为不显示,本来不显示的为显示                                      
        }
    }
 }
改下要解决的问题


[解决办法]
取得button
JButton button = (JButton) al.getSource();
设置可见性
...button.setVisible(false);
[解决办法]
JButton[] buttons=new JButton[26];
for(int i=0;i <buttons.length;i++)
buttons[i]=new JButton( " "+(char)( 'A '+i));

actionPerformed(ActionEvent e)
{
JButton o=(JButton)e.getSource();
if(o.getLabel()!=null && o.getLabel().equals( " ")==false)
{
o.setLabel( " ");
return;//如果原来有标签,则设置标签为空,即隐藏按钮上的字
}
for(int i=0;i <buttons.length;i++)
if(buttons[i]==o)( " ")//如果原来标签为空,则设置标签
if(o.getLable()==null || o.getLabel().equals( " "))
o.setLabel( " "+(char)( 'A '+i));
}

热点排行