好像影音播放器的开始/暂停按钮一样,点击一下就切换成另外一个按钮了。

解决方案 »

  1.   

    用setLabel方法设置显示 开始还是暂停,响应点击事件的监听方法中 根据getLabel方法得到的不同结果设置不同的实现代码
      

  2.   

    boolean start = false;
    final JButton b = new JButton("start");
    b.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    b.setText(start ? "start" : "pause");
    start = ~start;
    }
    })
      

  3.   

    java没写过这样的程序用一个变量绑定这个按钮的状态,根据这个变量,觉得按下这个按钮要调用的处理函数..不知道这样行不行,以前在VC++中就这样做
      

  4.   

    SE好久没用了···不用大概思路可以告诉你参考一下
    加入开始是个button按钮
    写button的点击事件
    当你点击button的时候我先去得到button里面的值
    if(值是开始的话){
    我将你的button的值设置成 结束
    }else{
    我将你button里面的值设置成  开始
    }
      

  5.   

    要是做页面的效果其实还是换背景比较简单,可是你得用ajax查看当前状态。要是用两个按钮用隐藏做效果倒是不用管后台的执行状态。我倒是觉得你想让什么东西中断再执行可能和线程关系比较紧密。
      

  6.   

    你用个JToggleButton按钮有setPressedIcon(pressedIcon)方法,这样平时setIcon是不按的Icon,按下去就是另外一个图标 
      

  7.   

    最终的解决方式是:使用了布局管理器,采用flowlayout的流布局方式把这两个空间放置在一个group中,然后把其中一个setvisible(false),由于布局是默认把不可见的控件的空间分配给可见的控件,这样一来就好像按钮重叠了一样~~
    不管怎么说,谢谢大家给了我其他的方法和创意!
      

  8.   

    导哪些包就不写了.public class Music {public Music{
    JLabel down;
    JLabel playandstop;
    JLabel up;playandstop = new JButton();
    playandstop.addActionListener(this);
    playandstop.setText("||>");
    playandstop.setBounds(50, 30, 60, 30);
    getContentPane().add(playandstop);down = new JButton();
    down.addActionListener(this);
    down.setText("< <");
    down.setBounds(145, 30, 60, 30);
    getContentPane().add(down);up = new JButton();
    up.addActionListener(this);
    up.setText("> >");
    up.setBounds(240, 30, 60, 30);
    getContentPane().add(up);public void actionPerformed(final ActionEvent e) {
    String flag = e.getActionCommand();
                    if (flag.equals("||>")) {
    playandstop.setText("| |");
    } else if (flag.equals("| |")) {
    playandstop.setText("||>");
    }
                }
            }