键盘按和Jbutton [英] Keyboard Press and Jbutton

查看:110
本文介绍了键盘按和Jbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算器应用程序,我在使用setMnemonic时遇到问题。我试图让键盘上的按键与计算器上的按钮链接。它一直告诉我<标识符>是预期的。这是我从 http:/获取有关函数的信息的页面/docs.oracle.com/javase/tutorial/uiswing/components/button.html

I have an calculator app and I am having trouble using setMnemonic. I am trying to get the keys on the keyboard to link with the buttons on the calculator. It keeps telling me < Identifier> is expected. This is the page I have been getting the info from about the function http://docs.oracle.com/javase/tutorial/uiswing/components/button.html.

我有什么想法让它起作用?

Any ideas how I can get it to work?

推荐答案

您的问题与Swing或助记符无关,而与尝试在外部进行方法调用有关方法或构造函数。你不能这样做:

Your problem has nothing to do with Swing or mnemonics and all to do with trying to make method calls outside of a method or constructor. You can't do this:

public class calculator_ui implements ActionListener {
  /**Creates a new instance of the window "Buttons"*/
    JFrame frame = new JFrame("Buttons");

    // .... etc...
    JButton buteq = new JButton("=");
    JButton butclear = new JButton("C");
    butclear.setMnemonic(KeyEvent.VK_B); // .... **** this is misplaced
    JButton back = new JButton("<");

而是将该行代码移动到合法的类的构造函数中。

Instead move that line of code into your class's constructor where it is legal.

除此之外,设置助记符将设置按钮将重新分配的alt键组合。如果你想获得更高级的按钮并让按钮响应按下非alt数字键,那么你将需要使用键绑定。

As an aside, setting mnemonics will set the alt-key combination that the button will respod to. If you want to get fancier and have the button respond to press of a non-alt numeric key, then you'll want to use Key Bindings.

这篇关于键盘按和Jbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆