我正在開發一個為多種不同目的維護資料的程式,例如負載、車輛服務、經紀人、客戶、燃料購買以及其他型別的購買。
為此,我使用了一個JMenu包含用于創建這些新資料項的選單項。因此,用戶可以按下,例如,ALT N, L打開新的加載向導。這只是簡單的選單導航。
還有一種方法可以創建鍵盤快捷鍵,例如CTLR N, L基本上可以做同樣的事情,但不打開選單?
我的意思是,用戶按下CTLR N并且系統等待另一個鍵,這將是L新的負載,B新的經紀人進入等。我正在使用一些具有功能的程式(我不記得哪個程式副手)像這樣。我不知道它是用什么語言撰寫的,但我記得當時認為開發人員根據標準鍵盤快捷鍵創建多個熱鍵很酷,只需等待第三個鍵與CTRL ${KEY}組合鍵分開按下即可。
我一直在網上搜索如何完成這一壯舉的想法,但沒有看到任何接近我正在尋找的東西。為了清楚我要做什么,請查看以下內容:
- 我有一個用于處理負載跟蹤資料的模塊,它帶有一個用于輸入新負載的向導。要啟動此向導,我希望在 New...Load Wizard 選單項上顯示鍵盤快捷鍵為
CTRL N, L. 當用戶按下時CTRL N,應用程式會等待查看下一個按下的鍵。如果按下的下一個鍵是N,則啟動新加載向導。 - 我有一個用于處理代理資料的模塊,它有一個用于輸入新代理的資料輸入對話框。要打開此對話框,我希望在 New...Broker 選單項上顯示鍵盤快捷鍵為
CTRL N, B. 當用戶按下時CTRL N,應用程式會等待查看下一個按下的鍵。如果按下的下一個鍵是B,則顯示 New Broker 對話框。 - 我有一個用于處理車輛服務、維護和維修的模塊,它有一個對話框用于輸入有關這些購買的資訊。我希望鍵盤快捷方式在 New...Service/Repair 選單項上顯示為
CTRL N, S. 當用戶按下時CTRL N,應用程式會等待查看下一個按下的鍵。如果按下的下一個鍵是S,則打開新服務/修復對話框。 - 應用程式中其他模塊的“新建”選單下將有一個類似的選單項,其功能應該相同。
- If the key pressed after the
CTRL NKeyStroke is not one of the available single shortcut keys displayed on the various New menu items, then it cancels the wait for the next key press. In other words, if the user pressesCTRL N, then presses, for example,Z(which has no menu item associated with it) theCTRL Nis simply discarded.
I am not sure if I have explained what I am trying to do clearly enough, so am more than willing to answer questions to allow for better understanding of my thoughts. Even so, any ideas or thoughts are greatly appreciated!
-SC
uj5u.com熱心網友回復:
我就是這樣做的。
我在用戶按下Ctrl 后設定了一個標志N,然后如果用戶按下b,如果設定了標志,則執行適當的操作。
下面的代碼只是一個概念證明(POC)。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class BindKeys {
private boolean flag;
private JTextField textField;
private JPanel createButton() {
JPanel panel = new JPanel();
JButton button = new JButton("Exit");
button.addActionListener(e -> System.exit(0));
panel.add(button);
return panel;
}
private JPanel createTextField() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
textField = new JTextField(12);
InputMap im = textField.getInputMap();
KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK);
im.put(ctrlN, "SET_FLAG");
KeyStroke b = KeyStroke.getKeyStroke('b');
im.put(b, "NEW_BROKER");
ActionMap am = textField.getActionMap();
am.put("SET_FLAG", new CtrlNAction());
am.put("NEW_BROKER", new BAction());
panel.add(textField);
return panel;
}
private void showGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTextField(), BorderLayout.PAGE_START);
frame.add(createButton(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class BAction extends AbstractAction {
public void actionPerformed(ActionEvent event) {
if (flag) {
textField.setText("New broker");
}
else {
textField.setText(textField.getText() "b");
}
}
}
private class CtrlNAction extends AbstractAction {
public void actionPerformed(ActionEvent event) {
flag = true;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new BindKeys().showGui());
}
}
請參閱如何使用鍵系結
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/456050.html
標籤:java swing desktop-application
