gpt4 book ai didi

java - JMenuItem 没有被键盘回车键激活

转载 作者:行者123 更新时间:2023-11-29 03:45:23 28 4
gpt4 key购买 nike

我自动生成一组 JMenuItem 并将它们添加到每个右键单击事件的 JPopUpMenu 中。问题是当我在所选菜单上按回车键时,我为菜单项设置的操作没有运行。我可以使用向上/向下键在菜单上导航,但是,该操作只能通过鼠标单击来运行。

下面是创建菜单项的代码:

private JMenuItem createMenuItem(TaskFactory tf, String title, boolean useCheckBoxMenuItem, String toolTipText) {

enter code hereJMenuItem item;
PopupAction action = new PopupAction(tf, title);
if ( useCheckBoxMenuItem )
item = new JCheckBoxMenuItem(action);
else
item = new JMenuItem(action);
item.setEnabled(tf.isReady());
item.setToolTipText(toolTipText);
return item;
}

private class PopupAction extends AbstractAction {

TaskFactory tf;
PopupAction(TaskFactory tf, String title) {
super( title );
this.tf = tf;
}
public void actionPerformed(ActionEvent ae) {
m_view.manager.execute(tf.createTaskIterator());
}
}

我试过将 focusable 设置为 true,但没有用。我已经在 Mac 10.7 Java 版本 1.6.0_33 和 Ubuntu Java 版本 1.6.0_26 上尝试过。我使用了标准的 Swing 外观和感觉。在 Mac 上运行时:外观是 [Aqua Look and Feel for Mac OS X - com.apple.laf.AquaLookAndFeel]

最佳答案

Windows XP,JDK6_025,使用 Substance、Metal、Windows、Nimbus

编辑1

关于 按下键或鼠标被触发 isEnabled

编辑2

添加了MenuKeyListener,

注意!!!!

不要使用 MenuKeyListener 作为 捕捉另一个 KeyEvents (空格)和 是,否则有时会生成虚假结果,例如Unknown keyCode: 0x0 on ......

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.*;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.BusinessBlueSteelSkin;

public class TableCheckBox extends JFrame {

private static final long serialVersionUID = 1L;
private JTable table;

public TableCheckBox() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {

private static final long serialVersionUID = 1L;

@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
createPopupMenu();
}

private void createPopupMenu() {
MyMenuKeyListener myMenuKeyListener = new MyMenuKeyListener();
JPopupMenu popup = new JPopupMenu();
JMenuItem myMenuItem1 = new JMenuItem("cccccccccccccccccccccc");
myMenuItem1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
//System.out.println("Done myMenuItem1");
}
});
myMenuItem1.getModel().addChangeListener(new ChangeListener() {

@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isArmed()) {
//System.out.println("isArmed");
} else if (model.isEnabled()) {
//System.out.println("isEnabled");
} else if (model.isPressed()) {
//System.out.println("isPressed");
} else if (model.isRollover()) {
//System.out.println("isRollover");
} else if (model.isSelected()) {
//System.out.println("isSelected");
}
}
});
myMenuItem1.addMenuKeyListener(myMenuKeyListener);

JMenuItem myMenuItem2 = new JMenuItem("bbbbbbbbbbbbbbbbbbbbbb");
myMenuItem2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
//System.out.println("Done myMenuItem2");
}
});
myMenuItem2.getModel().addChangeListener(new ChangeListener() {

@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isArmed()) {
//System.out.println("isArmed");
} else if (model.isEnabled()) {
//System.out.println("isEnabled");
} else if (model.isPressed()) {
//System.out.println("isPressed");
} else if (model.isRollover()) {
//System.out.println("isRollover");
} else if (model.isSelected()) {
//System.out.println("isSelected");
}
}
});
myMenuItem2.addMenuKeyListener(myMenuKeyListener);

popup.add(myMenuItem1);
popup.add(myMenuItem2);
MouseListener popupListener = new PopupListener(popup);
table.addMouseListener(popupListener);
popup.setLightWeightPopupEnabled(false);
PopupMenuListener popupMenuListener = new MyPopupMenuListener();
popup.addPopupMenuListener(popupMenuListener);
}

private class PopupListener extends MouseAdapter {

private JPopupMenu popup;

PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}

@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

@Override
public void mouseReleased(MouseEvent e) {
if (table.getSelectedRow() != -1) {
maybeShowPopup(e);
}
}

private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}

private class MyMenuKeyListener implements MenuKeyListener {

public void menuKeyTyped(MenuKeyEvent e) {
MenuElement[] path = e.getPath();
JMenuItem item = (JMenuItem) path[path.length - 1];
System.out.println("Key typed: " + e.getKeyChar()
+ ", " + MenuKeyEvent.getKeyText(e.getKeyCode())
+ " on " + item.getText());
}

public void menuKeyPressed(MenuKeyEvent e) {
MenuElement[] path = e.getPath();
JMenuItem item = (JMenuItem) path[path.length - 1];
System.out.println("Key typed: " + e.getKeyChar()
+ ", " + MenuKeyEvent.getKeyText(e.getKeyCode())
+ " on " + item.getText());
}

public void menuKeyReleased(MenuKeyEvent e) {
MenuElement[] path = e.getPath();
JMenuItem item = (JMenuItem) path[path.length - 1];
System.out.println("Key typed: " + e.getKeyChar()
+ ", " + MenuKeyEvent.getKeyText(e.getKeyCode())
+ " on " + item.getText());
}
}

private class MyPopupMenuListener implements PopupMenuListener {

public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
//System.out.println("Canceled");
}

public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
//System.out.println("Becoming Invisible");
}

public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
//System.out.println("Becoming Visible");
}
}

public static void main(String[] args) {
/*SwingUtilities.invokeLater(new Runnable() {

public void run() {
SubstanceLookAndFeel.setSkin(new BusinessBlueSteelSkin());
}
});*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
/*UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());*/
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
}
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
TableCheckBox frame = new TableCheckBox();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}

关于java - JMenuItem 没有被键盘回车键激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162666/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com