在java中将键盘属性设置为按钮
好了,伙计们,我有一个java内置的“小型计算器”,我现在希望能够从我的键盘访问它(访问按钮) 代码如下: 类Gui在java中将键盘属性设置为按钮,java,events,keyboard,listener,Java,Events,Keyboard,Listener,好了,伙计们,我有一个java内置的“小型计算器”,我现在希望能够从我的键盘访问它(访问按钮) 代码如下: 类Gui package mainProgram; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Gui extends JFrame { final private s
package mainProgram;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Gui extends JFrame {
final private static JButton numberButtons[] = new JButton[10];
private JButton sum = new JButton("+");
private JButton substract = new JButton("-");
private JButton divide = new JButton("/");
private JButton multiply = new JButton("*");
private JButton sqrt = new JButton("√");
private JButton square = new JButton("x^2");
private JButton cubic = new JButton("x^3");
private JButton percentage = new JButton("%");
private JButton divideByOne = new JButton("1/x");
private JButton C = new JButton("C");
private JButton OK = new JButton("=");
private JButton point = new JButton(".");
private JButton plusMinus = new JButton("+-");
private JTextArea output = new JTextArea();
private JTextField inputOne = new JTextField();
// WriteReadFunctions wrf = new WriteReadFunctions();
functions fn = new functions();
GuiUpdate gridBagConstr = new GuiUpdate();
HandlerClass handler = new HandlerClass();
public Gui() {
super("Calculator");
setLayout(new GridBagLayout());
output.setEditable(false);
output.setBackground(Color.RED);
for (int i = 0; i <= numberButtons.length - 1; i++) {
numberButtons[i] = new JButton(Integer.toString(i));
}
gridBagConstr.setConstrains("HORIZONTAL", 4, 1, 0, 0, 30);
add(output, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 4, 1, 0, 1, 15);
add(inputOne, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 2, 0);
add(divide, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 2, 0);
add(multiply, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 2, 0);
add(substract, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 2, 0);
add(sum, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 3, 0);
add(numberButtons[7], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 3, 0);
add(numberButtons[8], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 3, 0);
add(numberButtons[9], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 3, 0);
add(sqrt, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 4, 0);
add(numberButtons[4], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 4, 0);
add(numberButtons[5], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 4, 0);
add(numberButtons[6], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 4, 0);
add(square, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 5, 0);
add(numberButtons[1], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 5, 0);
add(numberButtons[2], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 5, 0);
add(numberButtons[3], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 5, 0);
add(cubic, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 8, 0);
add(numberButtons[0], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 8, 0);
add(point, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 8, 0);
add(C, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 8, 0);
add(OK, gridBagConstr.c);
inputOne.addActionListener(handler);
divide.addActionListener(handler);
multiply.addActionListener(handler);
substract.addActionListener(handler);
sum.addActionListener(handler);
for (int i = 0; i <= numberButtons.length - 1; i++) {
numberButtons[i].addActionListener(handler);
}
sqrt.addActionListener(handler);
square.addActionListener(handler);
cubic.addActionListener(handler);
point.addActionListener(handler);
C.addActionListener(handler);
OK.addActionListener(handler);
}
public void eraseData() {
fn.setA(0);
inputOne.setText("");
}
public void saveInput() {
if (fn.isNumeric(inputOne.getText())) {
fn.setResult(Double.parseDouble(inputOne.getText()));
output.setText(inputOne.getText());
}
}
类函数
package mainProgram;
import java.math.*;
import java.util.*;
public class functions {
private double a;
private double result = 0;
public boolean isNumeric (String x){
try {
Double.parseDouble(x);
return true;
} catch (NumberFormatException nfe) {}
return false;
}
public double getNumber ( String x){
if(isNumeric(x)){
this.a= Double.parseDouble(x);}
return a;
}
public double sum(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b + c ;
return result;
}else return b;
}
public double divide(String a , double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b / c ;
return result;
}else return b;
}
public double multiply(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b * c ;
return result;
}else return b;
}
public double substract(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b - c ;
return result;
}else return b;
}
public double sqrt(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = Math.sqrt(c) ;
return result;
}else return 0;
}
public double square(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c ;
return result;
}else return 0;
}
public double cubic(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c*c ;
return result;
}else return 0;
}
public double divideByOne(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = 1/c ;
return result;
}else return 0;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
package mainProgram;
import java.awt.GridBagConstraints;
public class GuiUpdate extends GridBagConstraints {
GridBagConstraints c = new GridBagConstraints();
public void setConstrains (String FILL, int gridwidth, int gridheight, int gridx, int gridy, int ipady ){
if(FILL.toUpperCase().equals("HORIZONTAL")){
this.c.fill = GridBagConstraints.HORIZONTAL;
}else if(FILL.toUpperCase().equals("VERTICAL")){
c.fill = GridBagConstraints.VERTICAL;
}else if (FILL.toUpperCase().equals("BOTH")){
c.fill = GridBagConstraints.BOTH;
}else c.fill = GridBagConstraints.NONE;
this.c.gridwidth = gridwidth;
this.c.gridheight = gridheight;
this.c.gridx = gridx;
this.c.gridy = gridy;
this.c.ipady = ipady;
}
}
package mainProgram;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(230,350);
go.setVisible(true);
}
}
类GUI更新
package mainProgram;
import java.math.*;
import java.util.*;
public class functions {
private double a;
private double result = 0;
public boolean isNumeric (String x){
try {
Double.parseDouble(x);
return true;
} catch (NumberFormatException nfe) {}
return false;
}
public double getNumber ( String x){
if(isNumeric(x)){
this.a= Double.parseDouble(x);}
return a;
}
public double sum(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b + c ;
return result;
}else return b;
}
public double divide(String a , double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b / c ;
return result;
}else return b;
}
public double multiply(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b * c ;
return result;
}else return b;
}
public double substract(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b - c ;
return result;
}else return b;
}
public double sqrt(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = Math.sqrt(c) ;
return result;
}else return 0;
}
public double square(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c ;
return result;
}else return 0;
}
public double cubic(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c*c ;
return result;
}else return 0;
}
public double divideByOne(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = 1/c ;
return result;
}else return 0;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
package mainProgram;
import java.awt.GridBagConstraints;
public class GuiUpdate extends GridBagConstraints {
GridBagConstraints c = new GridBagConstraints();
public void setConstrains (String FILL, int gridwidth, int gridheight, int gridx, int gridy, int ipady ){
if(FILL.toUpperCase().equals("HORIZONTAL")){
this.c.fill = GridBagConstraints.HORIZONTAL;
}else if(FILL.toUpperCase().equals("VERTICAL")){
c.fill = GridBagConstraints.VERTICAL;
}else if (FILL.toUpperCase().equals("BOTH")){
c.fill = GridBagConstraints.BOTH;
}else c.fill = GridBagConstraints.NONE;
this.c.gridwidth = gridwidth;
this.c.gridheight = gridheight;
this.c.gridx = gridx;
this.c.gridy = gridy;
this.c.ipady = ipady;
}
}
package mainProgram;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(230,350);
go.setVisible(true);
}
}
class主类
package mainProgram;
import java.math.*;
import java.util.*;
public class functions {
private double a;
private double result = 0;
public boolean isNumeric (String x){
try {
Double.parseDouble(x);
return true;
} catch (NumberFormatException nfe) {}
return false;
}
public double getNumber ( String x){
if(isNumeric(x)){
this.a= Double.parseDouble(x);}
return a;
}
public double sum(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b + c ;
return result;
}else return b;
}
public double divide(String a , double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b / c ;
return result;
}else return b;
}
public double multiply(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b * c ;
return result;
}else return b;
}
public double substract(String a, double b) {
if(isNumeric(a)){
double c = getNumber(a);
result = b - c ;
return result;
}else return b;
}
public double sqrt(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = Math.sqrt(c) ;
return result;
}else return 0;
}
public double square(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c ;
return result;
}else return 0;
}
public double cubic(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = c*c*c ;
return result;
}else return 0;
}
public double divideByOne(String a) {
if(isNumeric(a)){
double c = getNumber(a);
result = 1/c ;
return result;
}else return 0;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
package mainProgram;
import java.awt.GridBagConstraints;
public class GuiUpdate extends GridBagConstraints {
GridBagConstraints c = new GridBagConstraints();
public void setConstrains (String FILL, int gridwidth, int gridheight, int gridx, int gridy, int ipady ){
if(FILL.toUpperCase().equals("HORIZONTAL")){
this.c.fill = GridBagConstraints.HORIZONTAL;
}else if(FILL.toUpperCase().equals("VERTICAL")){
c.fill = GridBagConstraints.VERTICAL;
}else if (FILL.toUpperCase().equals("BOTH")){
c.fill = GridBagConstraints.BOTH;
}else c.fill = GridBagConstraints.NONE;
this.c.gridwidth = gridwidth;
this.c.gridheight = gridheight;
this.c.gridx = gridx;
this.c.gridy = gridy;
this.c.ipady = ipady;
}
}
package mainProgram;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(230,350);
go.setVisible(true);
}
}
现在我想添加的是一个按键监听器(或者其他什么),如果我按下键盘上的数字或者我按下(/*-+)计算器中的按钮来激活他的监听器。我是如何理解自己的。我对这个I/O类和方法不是很熟悉,有些建议会很好,也许我可以在1-2个地方阅读更多内容。您可以使用键绑定。阅读上的Swing教程中的部分 下面是一个简单的示例,让您开始学习:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ButtonCalculator extends JPanel
{
private JButton[] buttons;
private JTextField display;
public ButtonCalculator()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
display.replaceSelection(e.getActionCommand());
}
};
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
buttons = new JButton[10];
for (int i = 0; i < buttons.length; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
buttons[i] = button;
buttonPanel.add( button );
// Support Key Bindings
KeyStroke pressed = KeyStroke.getKeyStroke(text);
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(pressed, text);
button.getActionMap().put(text, numberAction);
}
setLayout( new BorderLayout() );
add(display, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
}
private static void createAndShowUI()
{
UIManager.put("Button.margin", new Insets(5, 10, 5, 10) );
JFrame frame = new JFrame("Button Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ButtonCalculator() );
frame.setResizable( false );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
公共类按钮计算器扩展JPanel
{
私有JButton[]按钮;
专用JTextField显示;
公共按钮计算器()
{
Action numberAction=new AbstractAction()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
display.replaceSelection(例如getActionCommand());
}
};
display=新的JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
JPanel buttonPanel=新的JPanel();
setLayout(新的GridLayout(0,5));
按钮=新的JButton[10];
对于(int i=0;i
该示例还显示了如何编写一个通用ActionListener,而不是多个按钮可以共享的ActionListener。这是一个计算器的大量代码。你怎么能做到^|~我是OOP方面的新手,一般来说在这个领域。。。还是个学生。这是我最好的主意…是的,这就是我感谢你的原因。你真的很有耐心所以我的代码相当好?我真的不太明白这个程序是如何工作的。。。但是看起来不错。