是这样的,我自己从JButton中派生了一个类MyButton,想实现这样的功能,当按下这类按钮时,使按钮失效,同时作其他工作,但现在鼠标按下按钮的工作写在什么函数里呢,是写在mouseclick中还是什么函数中呢?程序如下。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.lang.Throwable;public class BattleShip extends JFrame
{
public static void main(String[] args)
{
BattleShip bs = new BattleShip("BS");
}
BattleShip(String str)
{
super(str);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(450, 450);                initPanelAndButtons();                setResizable(false);
                setVisible(true);
        }
        void initPanelAndButtons()
        {
            JPanel jPanel = new JPanel();
            BsButton myB[]=new BsButton[25];
            
            int i;/*
            for (i=0; i<myB.length; i++)
            {
                myB[i] = new BsButton("");
                myB[i].setFlag(0);
                myB[i].setPreferredSize(new java.awt.Dimension(70, 70));
                jPanel.add(myB[i]);
            }
                   */
            BsButton m = new BsButton("34");
            jPanel.add(m);
            getContentPane().add(jPanel, java.awt.BorderLayout.CENTER);
        }
//button 
        class BsButton extends JButton
        {
            int flag;   //0:no any 2:one of the group of 2 battleships; 3:one of the group of 3 battleships;
            BsButton(String str)
            {
                super(str);
                flag = 0;   //default value
            }
            
            //set flag
            public void setFlag(int iFlag)
            {
                flag = iFlag;
            }
            public int getFlag()
            {
                return flag;
            }
            public void BsButtonMouseClicked(java.awt.event.ActionEvent evt)
            {
                System.out.println("ffd");
                this.setEnabled(false);
                JOptionPane jOP = new JOptionPane();
                jOP.showMessageDialog(this, "11", "22", JOptionPane.INFORMATION_MESSAGE);
            }
        }
}
class RetryButton extends JButton
{
    RetryButton()
    {
        super();
    }
    private void RetryButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
                
    }
    
}

解决方案 »

  1.   

    打印出的东东是JButton重载了Object的toString方法,至于具体包含什么东西,最好去看源代码。
    要想监听一个事件,必须向监听器注册,并重栽其中相应的方法例如,想让一个button点击使之失效的代码如下:/*
     * Created on 2004-11-24
     *
     * To change the template for this generated file go to
     * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
     */
    package cn.zxm.xml.dom;import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;/**
     * To change the template for this generated type comment go to
     * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
     */
    public class FibonacciDOMClient
    {
    JButton button = new JButton("aaaa"); private void test()
    {

    JFrame frame = new JFrame("");
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();
    c.add(button,BorderLayout.CENTER);
    frame.setVisible(true);
    button.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    FibonacciDOMClient.this.button.setText("safsaf");
    }
    }
    );
    } public static void main(String[] args)
    {
    app.test();
    //System.out.println("button ======= " + button.toString());
    }
    }
      

  2.   

    更正test方法修改如下: private void test()
    {

    JFrame frame = new JFrame("");
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();
    c.add(button,BorderLayout.CENTER);
    frame.setVisible(true);
    button.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    FibonacciDOMClient.this.button.setEnabled(false);
                                        //在这里加入想处理的代码
    }
    }
    );
    }
      

  3.   

    那个值是JButton的大小设置呀.