| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3397 人关注过本帖
标题:求助!按钮监听事件~~
取消只看楼主 加入收藏
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
收藏
 问题点数:0 回复次数:5 
求助!按钮监听事件~~

如果我一个面板里有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
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
收藏
得分:0 

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


2007-06-20 10:00
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
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
收藏
得分:0 
是不是不能把事件e强制转换成JButton型的?

2007-06-20 12:47
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
收藏
得分:0 
谢了哦~确实~
这样的毛病~真难找
谢谢了哦

2007-06-20 13:30
快速回复:求助!按钮监听事件~~
数据加载中...
 
   



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

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