- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不知道标题是否可以理解。不管怎样,我有一些 JMenuItems,并且我已经为这些设置了 mouseListener。
mntmExtractPaleographyFeature.addMouseListener(this);
其中mntmExtractPaleographyFeature是一个JMenuitem,this是实现MouseListener的类。
所以我为 mouseListener 添加了方法,例如
@Override
public void mouseReleased(MouseEvent arg0) {
if(arg0.getSource()==mntmExtractPaleographyFeature) {
//Code Here
}
现在我有了这个 JButton extractPaleographyB,它的功能与 JMenuItem 完全相同。我不想复制/粘贴代码两次(也因为它不是唯一的按钮/jmenuitem)。我尝试过
extractPaleographyB.addMouseListener(mntmExtractPaleographyFeature.getMouseListeners()[1]);
但它不起作用。有什么想法吗?
最佳答案
创建 Action并用于菜单项和按钮
教程中的一段代码
Action leftAction = new LeftAction(); //LeftAction code is shown later
...
button = new JButton(leftAction)
...
menuItem = new JMenuItem(leftAction);
要创建 Action 对象,通常创建 AbstractAction 的子类,然后实例化它。在您的子类中,您必须实现 actionPerformed 方法,以便在操作事件发生时做出适当的 react 。下面是创建和实例化 AbstractAction 子类的示例:
leftAction = new LeftAction("Go left", anIcon,
"This is the left button.",
new Integer(KeyEvent.VK_L));
...
class LeftAction extends AbstractAction {
public LeftAction(String text, ImageIcon icon,
String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
displayResult("Action for first button/menu item", e);
}
}
关于java - 在 JButton 上模拟 JMenuItem MouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31036360/
我有一个程序,我必须在其中更改对象的状态,我想知道是否有人能够在外部创建 MouseListener 类或监听器类,该类仍然具有与对象交互的代码,允许他们更改字段从原始类中取出,然后将它们交换出来,如
我有一个 JPanel parent 和一个 JPanel child。我在父级和子级中添加了一个 MouseListener。这是代码: public void init(MouseListener
我需要先说明一下,我的导师不允许我们使用 IDE。我们使用文本板。我想单击此标签,然后它从“H”更改为“T”。目前,当我单击标签时什么也不做。我忘记了什么? import javax.swing.*;
我试图在屏幕上出现一个圆圈并跟随鼠标移动。 (最终我将把它变成一个带有光线转换的游戏)我正在使用 MouseMotionListener 并尝试使用 mouseMoved 方法在我的 JPanel 中
我有一个类,它实现 MouseListner。 如果我将此 class 实例设置为 null ,它会从 JPanel 中删除 MouseListener 吗?或者我是否必须使用 removeMouse
所以我尝试使用 Java 图形创建一个视频游戏,并且我需要使用 MouseListener 作为菜单屏幕上的“播放”按钮。当我按下按钮时,只要我按住按钮,它就会重复打印测试文本。我认为这就像编写只允许
我是 Java 新手,希望实现以下目标。 我有各种组合框。对于每个组合框,我都有一个按钮可以清除组合框的内容。清除按钮是从我创建的通用 ClearComboBoxButton 类创建的。作为此 Cle
我有 4 JLabel s。 首次点击时: 我将背景颜色更改为红色并删除了 JLabel的MouseListener我点击过的。 第二次点击时: 我将背景颜色更改为绿色,但是JLabel我之前单击的不
我的游戏中处理射击的 MouseListener 在使用单独的鼠标时没有响应,但在使用笔记本电脑上的内置鼠标时却没有响应。在我的播放器类中,mouseClicked 方法如下所示: public vo
在我目前制作的游戏中,我有三种不同的 mousePressed 方法,一种用于单次射击,一种用于自动射击,一种用于近战攻击。因为自动开火的方法使用了 Swing Timer,所以我可以在其他 mous
我有堆叠条形图,我希望能够在其中选择堆栈中的单个条形图。但是 ChartMouseListener 不会将 ChartMouseEvent 解析为对应的 ChartEntity。这是监听器片段:
对于我正在制作的这款游戏,我真的很想得到一些帮助。我已经尝试了最长的时间,但我却找不到解决方案。目前它的作用不大。基本上,我需要鼠标监听器寻找左键单击并以一种方式改变蛇的方向,另一种方式用于右键单击。
(注意:我知道 MouseAdapter 类的存在,但由于我稍后可能会覆盖所有方法,所以它的优势就消失了吗? ) 我有一个 MainProgram.java 类,我在其中添加了几个组件。他们中的大多数
我有一个 JPanel(网格),嵌套在主 JFrame 中。我正在尝试在网格上设置一个 Mouselistener 并将事件报告给主框架。 App.java (入口点) public class Ap
我有一个 JInternalFrame,我想在双击 JTable 时在其中显示 JOptionPane。我在互联网上查了一下,发现唯一的方法是重写 mousePressed() 方法,这就是我的做法:
我在 java 中使用 swing 创建了一个圆形 strip ,现在我想在鼠标单击 strip 的特定区域(例如 45 度到 135 度中心角之间的区域)时显示一些文本,有人可以帮我吗? impor
有谁知道以下是否是触发 MouseEvent 方面的预期行为?其中一颗似乎在其他地方丢失或被消耗掉。 右键单击JPanel并显示JPopupMenu: MousePressed java.awt.ev
我正在使用 Java 开发一款游戏,并面临以下挑战。 我有 2 个 JPanel,需要以可视方式将形状从一个 JPanel 拖动到另一个。我已经使用 JFrame 中的 GlassPane 完成了这项
我有一个类,它扩展了JPanel并包含一个带有MouseListener的JLabel。我有一个扩展它的子类,我希望它具有相同的 JLabel,但我希望 MouseListener 执行不同的操作。代
我知道在类(class)的开头您可以编写 implements ActionListener 然后您必须创建 public void actionPerformed(ActionEvent e) 才能
我是一名优秀的程序员,十分优秀!