监听与事件源 我做的一个代码界面上有两个按钮,分别用于向不同的jList列表框添加数据(没有用到数据库,不牵涉到数据库的绑定),我想让两个按钮点击时都是弹出一个对话框。通过在对话框的文本框内输入要添加的数据,点击对话框的确定按钮就能够添加到对应的jList列表框内。即此时能监听到对话框是由哪个添加按钮激发的?本人对什么action、Listener不是很了解,希望能够给一些解释,如果有代码示范更好。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 添加相同的ActionListener。然后在listener里判断数据源来自哪个按钮,就添加到哪个列表。你先自己写试试。 普通点就是event e.getsource()或者为他们注册不同的监听器 import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * @version 1.33 2007-06-12 * @author Cay Horstmann */public class ButtonTest{ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}/** * A frame with a button panel */class ButtonFrame extends JFrame{ public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // create buttons JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); buttonPanel = new JPanel(); // add buttons to panel buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); // add panel to frame add(buttonPanel); // create button actions ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } /** * An action listener that sets the panel's background color. */ private class ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(backgroundColor); } private Color backgroundColor; } private JPanel buttonPanel; public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200;}这是《JAVA核心技术》里面对源代码,这个是一个Action的典型用法,自己运行一下看看,应该能理解不少。 对Action的理解我已经通过查看相应书籍明白了,发现昨天的问题牵涉到多层监听,要获取监听源比较麻烦。后来用定义了一个全局类型来解决问题的。不过非常谢谢大家的回答,感谢! 为什么循环不能停止 java中有类似visual assist的工具吗? 大家好,新手在这里提个问题,关于clone类的问题 Call to 'super' must be the first statement in the constructor body..这句话是什么意思.... JAVA字符串连接问题... 谁有java版的打字游戏的例子 executeBatch()的返回值问题 转换匿名 innerclass 简单的问题,有人给我解答吗? JAVA基础问题 几个很模糊的知识点请高手指点下 类和对象的内存结构
或者为他们注册不同的监听器
import java.awt.event.*;
import javax.swing.*;/**
* @version 1.33 2007-06-12
* @author Cay Horstmann
*/
public class ButtonTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}/**
* A frame with a button panel
*/
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // create buttons
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red"); buttonPanel = new JPanel(); // add buttons to panel
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton); // add panel to frame
add(buttonPanel); // create button actions
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
} /**
* An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
} public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
} private Color backgroundColor;
} private JPanel buttonPanel; public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}这是《JAVA核心技术》里面对源代码,这个是一个Action的典型用法,自己运行一下看看,应该能理解不少。