【发布时间】:2012-12-22 16:30:44
【问题描述】:
很抱歉它是重复的 - 我写这篇文章时很着急,没有时间检查。虽然我认为它会更快,但现在我想到了......
我一直在编写一个应用程序并尝试制作热键。我决定使用KeyListener,因为这是我目前所知道的。但是,班级对按键没有反应。我将如何修复这个错误?如果有替代KeyListener 的方法可以做同样的事情,请告诉我,最好提供一个例子来说明它是如何工作的。
主类
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import panels.TabBar;
public class __mn implements KeyListener {
static JFrame disp = new JFrame("dat app");
static TabBar tabs = new TabBar();
public static void main(String[] args) {
disp.setLayout(new BorderLayout());
disp.add(tabs, BorderLayout.PAGE_START);
disp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
disp.setSize(TabBar.PREF_WIDTH, 500);
disp.setResizable(false);
disp.setLocationRelativeTo(null);
disp.addKeyListener(new __mn());
disp.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.paramString());
}
//Unused
@Override public void keyReleased(KeyEvent e) {
System.out.println(e.paramString());
}
@Override public void keyTyped(KeyEvent e) {
System.out.println(e.paramString());
}
}
TabBar 类
package panels;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
//http://www.dreamincode.net/forums/topic/245148-java-key-binding-tutorial-and-demo-program/
public class TabBar extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
public static final int NONE = -1;
public static final int INBOX = 0;
public static final int SEND_MSG = 1;
public static final int PRIVATE_CHAT = 2;
public static final int FEEDBACK = 3;
public static final int PREF_WIDTH = 425;
private static final String[] tabNames = {"Inbox", "Send a message", "Private chat", "Feedback"};
private static final JButton btnInbox = new JButton(tabNames[INBOX]);
private static final JButton btnSendMSG = new JButton(tabNames[SEND_MSG]);
private static final JButton btnPrivChat = new JButton(tabNames[PRIVATE_CHAT]);
private static final JButton btnFeedback = new JButton(tabNames[FEEDBACK]);
public int currentTab = -1;
public TabBar() {
this(new FlowLayout());
}
public TabBar(LayoutManager layout) {
super(layout);
add(btnInbox);
add(btnSendMSG);
add(btnPrivChat);
add(btnFeedback);
btnInbox.addActionListener(this);
btnSendMSG.addActionListener(this);
btnPrivChat.addActionListener(this);
btnFeedback.addActionListener(this);
setBackground(Color.BLACK);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.paramString());
if (e.getSource() == btnInbox)
currentTab = INBOX;
else if (e.getSource() == btnSendMSG)
currentTab = SEND_MSG;
else if (e.getSource() == btnPrivChat)
currentTab = PRIVATE_CHAT;
else if (e.getSource() == btnFeedback)
currentTab = FEEDBACK;
else currentTab = NONE;
}
public void hotkeyPressed(char pressed) {
pressed = Character.toLowerCase(pressed);
System.out.println("Hotkey pressed: " + pressed);
switch (pressed) {
case 'i':
setTab(INBOX);
break;
case 's':
setTab(SEND_MSG);
break;
case 'p':
setTab(PRIVATE_CHAT);
break;
case 'f':
setTab(FEEDBACK);
break;
default:
break;
}
}
private void setTab(int tab) {
System.out.println("Somthing pressed! tab=" + tab);
currentTab = tab;
switch (tab) {
case INBOX:
btnInbox.requestFocusInWindow();
}
}
}
【问题讨论】:
-
"如果您觉得没有必要,请随时编辑代码。" - 你应该这样做。
-
您在控制台中看到任何错误吗?
hotkeyPressed方法到底在哪里调用?您要测试哪些键? -
快速回答,可以肯定.. 谢谢大家。
-
@LeeMeador 介意将其作为答案,以便我接受吗? :)
-
您可以使用KeyBinding,如图here。从 Swing 的角度来看,KeyListener 的级别太低了。
标签: java swing jframe keylistener setfocus