标题:求助!按钮监听事件~~
只看楼主
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
 问题点数:0 回复次数:13 
求助!按钮监听事件~~

如果我一个面板里有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编辑过]

搜索更多相关主题的帖子: 监听 按钮 
2007-06-19 19:50
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
得分:0 


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();
}
}
红色的就是问题的地方


2007-06-19 20:28
josean
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-4-4
得分:0 

在创建按钮时,你为每一个按钮起一个名字。
比如: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"){}
如果你的源码没错,应该会有反应。


菩提本无树,明镜亦非台,本来无一物,何处惹尘埃!
2007-06-19 22:01
zeng702008
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-3-30
得分:0 
同意楼上的观点 ....我的看法是:
把实现的接口改为: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编辑过]


“向左走,向右走”我想,忽然有一天早晨他们改变了习惯,一转身就会相遇!
2007-06-20 00:58
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
得分:0 

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


2007-06-20 10:00
zeng702008
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-3-30
得分:0 
以下是引用xw2014在2007-6-20 10:00:24的发言:

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

是框架的问题还是监听的问题。。。


“向左走,向右走”我想,忽然有一天早晨他们改变了习惯,一转身就会相遇!
2007-06-20 12:08
zeng702008
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-3-30
得分:0 
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N,InputEvent.CTRL_MASK,false));


这里有问题是不...你还没实现接口ItemListener吧...(猜的)—_—

“向左走,向右走”我想,忽然有一天早晨他们改变了习惯,一转身就会相遇!
2007-06-20 12:13
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
得分:0 

修改后的程序:
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编辑过]


2007-06-20 12:30
zeng702008
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-3-30
得分:0 
回复:(xw2014)修改后的程序:import javax.swing.*...
是个异常。。我看看先....—_

“向左走,向右走”我想,忽然有一天早晨他们改变了习惯,一转身就会相遇!
2007-06-20 12:34
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
得分:0 
是不是不能把事件e强制转换成JButton型的?

2007-06-20 12:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-148790-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031290 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved