关于swing里面的事件机制 actionPerformed是谁的?ActionListener?如果是,我倒是很奇怪,你怎么把一个ActionListener加给一个JComponent的?给代码看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是这样的,我想让所有的类对象共享一个actionmap,我把其中的一个action设为inner class,actionPerformed里面如果直接访问外面的类的成员的话,就直接指向了第一次创建actionmap的对象 给你一个例子,这个例子中有一个按钮,点击这个按钮会弹出一个菜单,上有一个菜单项为 Try,点击这个菜单项后,会在 actionPerformed 里通过 e.getSource() 等方法最终找到弹出菜单的按钮,并修改这个按钮的文本。/* * @(#) TestSwing.java * Created on 2004-10-16 * By James Fancy */package jamesfancy;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;/** * TestSwing * * @author James */public class TestSwing extends JFrame implements ActionListener { JButton button; JPopupMenu pMenu; public TestSwing() { button = new JButton("OK"); pMenu = new JPopupMenu(); JMenuItem item = new JMenuItem("Try"); item.addActionListener(this); pMenu.add(item); this.getContentPane().add(button); button.addActionListener(this); pack(); this.setLocation(400, 300); } public final static void main(String[] args) { TestSwing test = new TestSwing(); test.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { pMenu.show(button, 0, 0); } else { JMenuItem item = (JMenuItem) e.getSource(); JPopupMenu m = (JPopupMenu) item.getParent(); JButton b = (JButton) m.getInvoker(); b.setText("Good"); } }} 我明白你了程序要实现的目的,我想这样对你有帮助!import javax.swing.*;import java.awt.event.*;import java.awt.*;public class TestSwing extends JFrame{ JButton button; JPopupMenu pMenu; public TestSwing() { button = new JButton("OK"); pMenu = new JPopupMenu(); JMenuItem item = new JMenuItem("Try"); item.addActionListener(new myaction()); pMenu.add(item); this.getContentPane().add(button); this.getContentPane().addMouseListener(new mymouse()); button.addActionListener(new myaction()); pack(); this.setLocation(400, 300); this.getContentPane().setLayout(new FlowLayout()); } public final static void main(String[] args) { TestSwing test = new TestSwing(); test.setSize(300,200); test.setVisible(true); } class myaction implements ActionListener{ public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { System.out.println("Actionʼþ"); } } } class mymouse extends MouseAdapter { public void mouseReleased(MouseEvent me){ if(me.isPopupTrigger()){ System.out.println("Êó±êÓÒ¼ü"); } } } } java 新建文件的问题。 JAVA中怎样把字符串作为代码来执行?类似JS里EVAL()那样的作用 新手请教:UltraEdit怎么让Java关键字着色 高分求助在线等:java的application程序可以获得本机的机器名么? 初学JAVA,求助关于如何正确编译JAVA包的问题 java数码计算器?大家一起给我出出主意? 提出一个小问题供讨论! eclipse问题 百分求解,解决了立即给分!! jdk环境变量设置好了,Java可以在cmd中运行,Java不能,是哪里出问题了 关于win2000里的一些.class文件的问题 菜鸟的问题:关于I/O流,多多帮忙!
/*
* @(#) TestSwing.java
* Created on 2004-10-16
* By James Fancy
*/
package jamesfancy;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;/**
* TestSwing
*
* @author James
*/
public class TestSwing extends JFrame implements ActionListener { JButton button;
JPopupMenu pMenu; public TestSwing() {
button = new JButton("OK"); pMenu = new JPopupMenu();
JMenuItem item = new JMenuItem("Try");
item.addActionListener(this);
pMenu.add(item); this.getContentPane().add(button); button.addActionListener(this); pack();
this.setLocation(400, 300);
} public final static void main(String[] args) {
TestSwing test = new TestSwing();
test.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
pMenu.show(button, 0, 0);
} else {
JMenuItem item = (JMenuItem) e.getSource();
JPopupMenu m = (JPopupMenu) item.getParent();
JButton b = (JButton) m.getInvoker();
b.setText("Good");
}
}}
import java.awt.event.*;
import java.awt.*;public class TestSwing extends JFrame{ JButton button;
JPopupMenu pMenu; public TestSwing() {
button = new JButton("OK"); pMenu = new JPopupMenu();
JMenuItem item = new JMenuItem("Try");
item.addActionListener(new myaction());
pMenu.add(item); this.getContentPane().add(button);
this.getContentPane().addMouseListener(new mymouse()); button.addActionListener(new myaction()); pack();
this.setLocation(400, 300);
this.getContentPane().setLayout(new FlowLayout());
} public final static void main(String[] args) {
TestSwing test = new TestSwing();
test.setSize(300,200);
test.setVisible(true);
} class myaction implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
System.out.println("Actionʼþ");
}
}
}
class mymouse extends MouseAdapter {
public void mouseReleased(MouseEvent me){
if(me.isPopupTrigger()){
System.out.println("Êó±êÓÒ¼ü");
}
}
}
}