当前位置: 代码迷 >> Java相关 >> 求助!按钮监听事件~~
  详细解决方案

求助!按钮监听事件~~

热度:270   发布时间:2007-06-19 19:50:35.0
求助!按钮监听事件~~

如果我一个面板里有3个相同的按钮,为每个按钮都设立了一个监听器,按一个按钮就执行一个事件(用同一个方法实现),如何确定是按了哪个按钮呢?
比喻:public void mouseClicked(MouseEvent e)
{
if(这里应该怎么办,来确定是按了第一个按钮)
c.setBackground(Color.green);
else if(这里应该怎么办,来确定是按了第二个按钮)
c.setBackground(Color.yellow);
else if(这里应该怎么办,来确定是按了第三个按钮)
c.setBackground(Color.orange);
}

[此贴子已经被作者于2007-6-19 20:17:34编辑过]

搜索更多相关的解决方案: 监听  按钮  

----------------解决方案--------------------------------------------------------


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui implements MouseListener
{
/**
* @param args
*/
public int a;
public JFrame aFrame;
public JMenuBar aMenuBar;
public JToolBar aToolBar;
public Container c;
public JMenu fileMenu, viewMenu,saveMenu;
public JMenuItem newMenuItem,openMenuItem,saveMenuItem,saveAsMenuItem;
public JButton firstButton,secondButton,thirdButton;
public SimpleGui()
{
aFrame = new JFrame( "The SimpleGui");
Container c=aFrame.getContentPane();
aMenuBar=new JMenuBar( );
aToolBar=new JToolBar();
fileMenu=new JMenu( "File");
fileMenu.setMnemonic('F');
viewMenu=new JMenu("View");
viewMenu.setMnemonic('V');
saveMenu=new JMenu("Save");
saveMenu.setMnemonic('S');
newMenuItem=new JMenuItem("New" ,'N');
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N,InputEvent.CTRL_MASK,false));
openMenuItem =new JMenuItem("Open",'O' );
saveMenuItem =new JMenuItem("Save",'S' );
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S,InputEvent.CTRL_MASK,false));
saveAsMenuItem =new JMenuItem("SaveAs");
firstButton=new JButton("Green");
secondButton=new JButton("Yellow");
thirdButton=new JButton("Orange");

//
aMenuBar.add(fileMenu);
aMenuBar.add(viewMenu);
aMenuBar.add(saveMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.addSeparator( );
fileMenu.add(saveMenu);
saveMenu.add(saveMenuItem);
saveMenu.add(saveAsMenuItem);
aToolBar.add(firstButton);
aToolBar.add(secondButton);
aToolBar.add(thirdButton);
//

c.setLayout(new BorderLayout());
c.add(aToolBar,BorderLayout.NORTH);
firstButton.addMouseListener(this);
secondButton.addMouseListener(this);
thirdButton.addMouseListener(this);
aFrame.setJMenuBar(aMenuBar);
aFrame.setSize(400,250);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);

}
public void mouseClicked(MouseEvent e)
{
if(firstButton)
c.setBackground(Color.green);
else if(secondButton)
c.setBackground(Color.yellow);
else if(thirdButton)
c.setBackground(Color.orange);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args)
{
// TODO 自动生成方法存根
new SimpleGui();
}
}
红色的就是问题的地方


----------------解决方案--------------------------------------------------------

在创建按钮时,你为每一个按钮起一个名字。
比如:firstButton=new JButton("按钮1");
我继承的是implements ActionListener(把你的public class SimpleGui implements MouseListener 改成public class SimpleGui implements ActionListener )
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1){}

或者
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="按钮1"){}
如果你的源码没错,应该会有反应。


----------------解决方案--------------------------------------------------------
同意楼上的观点 ....我的看法是:
把实现的接口改为:ActionListener 然后实现方法actionPerformed() 如下:
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == firstButton)
c.setBackground(Color.green);
if(button == secondButton)
c.setBackground(Color.yellow);
else if(button == thirdButton)
c.setBackground(Color.orange);

}


还有就是这里    firstButton.addMouseListener(this);
      secondButton.addMouseListener(this);
      thirdButton.addMouseListener(this);
就要改为:

          firstButton.addActionListener(this);
       secondButton.addActionListener(this);
      thirdButton.addActionListener(this);


[此贴子已经被作者于2007-6-20 2:21:58编辑过]


----------------解决方案--------------------------------------------------------

但是还是有问题,未知源!


----------------解决方案--------------------------------------------------------
以下是引用xw2014在2007-6-20 10:00:24的发言:

但是还是有问题,未知源!

是框架的问题还是监听的问题。。。
----------------解决方案--------------------------------------------------------
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N,InputEvent.CTRL_MASK,false));


这里有问题是不...你还没实现接口ItemListener吧...(猜的)―_―
----------------解决方案--------------------------------------------------------

修改后的程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui implements ActionListener
{
/**
* @param args
*/
public int a;
public JFrame aFrame;
public JMenuBar aMenuBar;
public JToolBar aToolBar;
public Container c;
public JMenu fileMenu, viewMenu,saveMenu;
public JMenuItem newMenuItem,openMenuItem,saveMenuItem,saveAsMenuItem;
public JButton firstButton,secondButton,thirdButton;
public SimpleGui()
{
aFrame = new JFrame( "The SimpleGui");
Container c=aFrame.getContentPane();
aMenuBar=new JMenuBar( );
aToolBar=new JToolBar();
fileMenu=new JMenu( "File");
fileMenu.setMnemonic('F');
viewMenu=new JMenu("View");
viewMenu.setMnemonic('V');
saveMenu=new JMenu("Save");
saveMenu.setMnemonic('S');
newMenuItem=new JMenuItem("New" ,'N');
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N,InputEvent.CTRL_MASK,false));
openMenuItem =new JMenuItem("Open",'O' );
saveMenuItem =new JMenuItem("Save",'S' );
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S,InputEvent.CTRL_MASK,false));
saveAsMenuItem =new JMenuItem("SaveAs");
firstButton=new JButton("Green");
secondButton=new JButton("Yellow");
thirdButton=new JButton("Orange");

//
aMenuBar.add(fileMenu);
aMenuBar.add(viewMenu);
aMenuBar.add(saveMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.addSeparator( );
fileMenu.add(saveMenu);
saveMenu.add(saveMenuItem);
saveMenu.add(saveAsMenuItem);
aToolBar.add(firstButton);
aToolBar.add(secondButton);
aToolBar.add(thirdButton);
//

c.setLayout(new BorderLayout());
c.add(aToolBar,BorderLayout.NORTH);
firstButton.addActionListener(this);
secondButton.addActionListener(this);
thirdButton.addActionListener(this);

aFrame.setJMenuBar(aMenuBar);
aFrame.setSize(400,250);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == firstButton)
c.setBackground(Color.green);
if(button == secondButton)
c.setBackground(Color.yellow);
else if(button == thirdButton)
c.setBackground(Color.orange);
}

public static void main(String[] args)
{
// TODO 自动生成方法存根
new SimpleGui();
}
}

编译没有问题,这是运行时的错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SimpleGui.actionPerformed(SimpleGui.java:73)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

[此贴子已经被作者于2007-6-20 12:32:59编辑过]


----------------解决方案--------------------------------------------------------
回复:(xw2014)修改后的程序:import javax.swing.*...
是个异常。。我看看先....―_―
----------------解决方案--------------------------------------------------------
是不是不能把事件e强制转换成JButton型的?
----------------解决方案--------------------------------------------------------
  相关解决方案