简体   繁体   English

Java 中的快捷键(助记符)

[英]Shortcut keys in Java (Mnemonic)

I need help with my homework.我的家庭作业需要帮助。 I can't seem to make this program work.我似乎无法让这个程序工作。 I just need to put shortcut keys for the compute, reset, and exit buttons.我只需要为计算、重置和退出按钮设置快捷键。

Compute - Ctrl C Reset - Ctrl R Exit - Ctrl E计算 - Ctrl C 重置 - Ctrl R 退出 - Ctrl E

If you guys need my professor's instructions, I can provide that as well.如果你们需要我教授的指示,我也可以提供。 here这里

Here's my code:这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener{
    JLabel fval,sval;
    JTextField tf1,tf2;
    JButton add,subtract,multiply,divide,compute,reset; 
    String s="";
    JPanel jp = new JPanel();
    Calculator(){
        jp.setLayout(new GridLayout(5,2));
        fval=new JLabel("First Value: ");
        sval=new JLabel("Second Value: ");
        tf1=new JTextField();
        tf2=new JTextField();
        add=new JButton("ADD");
        subtract=new JButton("SUBTRACT");
        multiply=new JButton("MULTIPLY");
        divide=new JButton("DIVIDE");
        compute=new JButton("COMPUTE");
        reset=new JButton("RESET");
        jp.add(fval);
        jp.add(tf1);
        jp.add(sval);
        jp.add(tf2);
        jp.add(add);
        jp.add(subtract);
        jp.add(multiply);
        jp.add(divide);
        jp.add(compute);
        jp.add(reset);
        add.addActionListener(this);
        subtract.addActionListener(this);
        multiply.addActionListener(this);
        divide.addActionListener(this);
        compute.addActionListener(this);
        reset.addActionListener(this);
        this.setTitle("Calculator");
        this.setBounds(10,10,300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        add(jp);
        
        //ADD
       Action addAction = new AbstractAction("ADD") {
 
            @Override
            public void actionPerformed(ActionEvent evt) {
                s="ADD";
            }
        };

        String key1 = "ADD";
        add.setAction(addAction);
        addAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
        add.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_DOWN_MASK), key1);
        add.getActionMap().put(key1, addAction);
        
        //SUBTRACT
        Action subAction = new AbstractAction("SUBTRACT") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                s="SUBTRACT";
            }
        };
        
        String key2 = "SUBTRACT";
        subtract.setAction(subAction);
        subAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
        subtract.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK), key2);     
        subtract.getActionMap().put(key2, subAction);
        
        //MULTIPLY
         Action mulAction = new AbstractAction("MULTIPLY") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                s="MULTIPLY";
            }
        };
        
        String key3 = "MULTIPLY";
        multiply.setAction(mulAction);
        mulAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_M);
        multiply.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK), key3);     
        multiply.getActionMap().put(key3, mulAction);
        
        //DIVIDE
        Action divAction = new AbstractAction("DIVIDE") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                s="DIVIDE";
            }
        };
        
        String key4 = "DIVIDE";
        divide.setAction(divAction);
        divAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
        divide.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK), key4);     
        divide.getActionMap().put(key4, divAction);
        
    }
    
    public void Add(double n1,double n2){
        JOptionPane.showMessageDialog(this, +(n1+n2), "Answer", JOptionPane.INFORMATION_MESSAGE);
    }
    public void Subtract(double n1,double n2){
        JOptionPane.showMessageDialog(this, +(n1-n2), "Answer", JOptionPane.INFORMATION_MESSAGE);
    }
    public void Multiply(double n1,double n2){
        JOptionPane.showMessageDialog(this, +(n1*n2), "Answer", JOptionPane.INFORMATION_MESSAGE);
    }
    public void Divide(double n1,double n2){
        JOptionPane.showMessageDialog(this, +(n1/n2), "Answer", JOptionPane.INFORMATION_MESSAGE);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String in=e.getActionCommand();
        if(in.equals("COMPUTE")){
        try {
            double n1=Double.parseDouble(tf1.getText());
            double n2=Double.parseDouble(tf2.getText());
        
            if(s.equals("ADD"))
                Add(n1,n2);
            else if(s.equals("SUBTRACT"))
                Subtract(n1,n2);
            else if(s.equals("MULTIPLY"))
                Multiply(n1,n2);
            else if(s.equals("DIVIDE"))
                Divide(n1,n2);
            else if(in.equals("RESET")){
                tf1.setText("");
                tf2.setText("");
                s="";
            }else
            {
                s=in;
         }
        }
        catch (NumberFormatException e1) {
            compute.setSelected(true);
        JOptionPane.showMessageDialog(this, "Math Error.", "Warning", JOptionPane.INFORMATION_MESSAGE);
    }
    }       
};
    
    public static void main(String[] args) {
        new Calculator();
    }  
}

Thank you in advance!先感谢您!

I just need to put shortcut keys for the compute, reset, and exit buttons.我只需要为计算、重置和退出按钮设置快捷键。

I think you are confusing a "mnemonic" with an "accelerator".我认为您将“助记符”与“加速器”混淆了。

The "mnemonic" is invoked when the component is focused.当组件获得焦点时调用“助记符”。 You invoke the Action by using the "Alt" key plus the underlined character of the text on the button.您可以通过使用“Alt”键加上按钮上文本的下划线字符来调用操作。

The "accelerator" can be invoked even when the component doesn't have focus by using the specified KeyStroke.即使组件没有使用指定的 KeyStroke 获得焦点,也可以调用“加速器”。

So instead of attempting use Key Bindings directly, you can set the "accelerator" of the Action .因此,您可以设置Action的“加速器”,而不是尝试直接使用 Key Bindings。 When you add the Action to the button the key bindings will be set automatically for you.当您将操作添加到按钮时,键绑定将自动为您设置。

Read the section from the Swing tutorial on How to Use Actions for more information阅读 Swing 教程中有关如何使用操作的部分以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM