是这样的,我自己从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)
{
}
}
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)
{
}
}
解决方案 »
- sql求助
- JSF用的还多吗?
- 在servlet中使用request.getRequestDispatcher(path).forward(request, response)进行页面跳转,跳转后的页面div标签失效
- 一直没解决的问题,急
- 找工作
- 请大家推荐几款支持socket的手机!另外想了解,如何判断一个手机是否支持socket?
- 菜鸟承蒙您照顾!我用的服务器是tomcat4,webapps下自己建立的文件夹为什么服务器访问不到呢?
- JSP 对 EJB 应用的问题?
- 关于Struts2里使用struts2-dojo-plugin包来实现ajax的问题?
- 面试题:Transactional同时加在类和类中的一个方法,类的其他方法没加,如何只让这个方法的事务生效?
- 用java哪方面的技术可以调用Crystal report?
- 求vrml+java的实例,谢谢!
要想监听一个事件,必须向监听器注册,并重栽其中相应的方法例如,想让一个button点击使之失效的代码如下:/*
* Created on 2004-11-24
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>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>Preferences>Java>Code Generation>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());
}
}
{
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);
//在这里加入想处理的代码
}
}
);
}