gpt4 book ai didi

java - 在java中将键盘归属于按钮

转载 作者:行者123 更新时间:2023-12-01 13:43:36 25 4
gpt4 key购买 nike

好的,我已经有了一个用 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 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());
}
}

HandlerClass 类

    private class HandlerClass implements ActionListener {
String op;

public void actionPerformed(ActionEvent e) {

String x = e.getActionCommand();
// if(fn.isNumeric(inputOne.getText()))
// fn.setResult(Double.parseDouble(inputOne.getText()));
switch (x) {
case "0":
inputOne.setText(inputOne.getText() + x);
break;
case "1":
inputOne.setText(inputOne.getText() + x);
break;
case "2":
inputOne.setText(inputOne.getText() + x);
break;
case "3":
inputOne.setText(inputOne.getText() + x);
break;
case "4":
inputOne.setText(inputOne.getText() + x);
break;
case "5":
inputOne.setText(inputOne.getText() + x);
break;
case "6":
inputOne.setText(inputOne.getText() + x);
break;
case "7":
inputOne.setText(inputOne.getText() + x);
break;
case "8":
inputOne.setText(inputOne.getText() + x);
break;
case "9":
inputOne.setText(inputOne.getText() + x);
break;
case ".":
inputOne.setText(inputOne.getText() + x);
break;
case "/":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "*":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "-":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "+":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "√":
fn.sqrt(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
break;
case "x^2":
fn.square(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
break;
case "x^3":
fn.cubic(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
break;
case "%":
break;
case "1/x":
fn.divideByOne(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
break;
case "C":
eraseData();
output.setText("");
fn.setResult(0);
break;
case "=":
switch (op) {
case "/":
fn.divide(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "*":
fn.multiply(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "+":
fn.sum(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "-":
fn.substract(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "+-":
break;
default:
break;
}


}

}

}

类函数

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;
}
}

类 GuiUpdate

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);
}
}

现在我想添加的是一个 KeyListener(或其他东西),这样如果我按键盘上的数字或按(/* - +)计算器中具有该属性的按钮来激活他的监听器。我如何让自己被理解。我对这个 I/O 类和方法不是很熟悉,一些建议会很好,也许 1-2 个地方我可以阅读更多内容。

最佳答案

您将使用按键绑定(bind)。阅读 Swing 教程中关于 How to Use Key Bindings 的部分.

这是一个帮助您入门的简单示例:

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();
}
});
}
}

该示例还展示了如何编写可由多个按钮共享的通用 ActionListener。

关于java - 在java中将键盘归属于按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20505372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com