很抱歉它是重复的——我写这篇文章的时候很着急,没有时间检查。虽然我认为它会更快,但现在我想到它......
我一直在写一个应用程序,并正在尝试制作热键。我决定使用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();
}
}
}