gpt4 book ai didi

java - 让计算器上的按钮具有交互性(以便它们实际上可以加、减等)的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-01 23:53:04 25 4
gpt4 key购买 nike

我想制作一个小型计算器项目来有趣地使用 Java Swing API 测试我的新 GUI 知识。我创建了 GUI,但缺少一个关键部分:数学!我的问题是,如何为每个按钮添加功能?简单地说:当用户单击按钮时,如何使按钮向文本框(2+2)添加一些内容,然后让系统实际将数字添加在一起并向用户显示它们?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class basicCalculatorDesign {

public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
basicCalculatorDesign calc = new basicCalculatorDesign();
calc.start();
}

public void start() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton sqrt = new JButton("SqRt");
JButton percentage = new JButton("%");
JButton equals = new JButton("=");

JTextField input = new JTextField();

JLabel label = new JLabel("Basic Calculator");
frame.getContentPane().add(BorderLayout.NORTH, label);

frame.getContentPane().add(BorderLayout.SOUTH, input);

panel.setBackground(Color.BLUE);
panel.setLayout(new GridBagLayout());

JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBackground(Color.RED);
frame.getContentPane().add(BorderLayout.EAST, panel2);

frame.getContentPane().add(BorderLayout.CENTER, panel);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(210, 260);


GridBagConstraints right = new GridBagConstraints();
right.anchor = GridBagConstraints.WEST;
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
GridBagConstraints middle = new GridBagConstraints();
middle.anchor = GridBagConstraints.CENTER;
right.gridwidth = GridBagConstraints.REMAINDER;
right.fill = GridBagConstraints.HORIZONTAL;

// add buttons
panel.add(one, left);
panel.add(two, middle);
panel.add(three, right);
panel.add(four, left);
panel.add(five, middle);
panel.add(six, right);
panel.add(seven, left);
panel.add(eight, middle);
panel.add(nine, right);
panel.add(zero, right);

panel2.add(equals);
panel2.add(plus);
panel2.add(minus);
panel2.add(divide);
panel2.add(multiply);
panel2.add(sqrt);
panel2.add(percentage);

}
}

最佳答案

is it possible to implement this into a single inner class, and then use that inner class multiple times for different buttons.

您可以对数字 (0-9) 按钮使用相同的操作 (ActionListener)。像这样的东西:

public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
display.replaceSelection( source.getActionCommand() );
}

其中“display”是显示数字和结果的文本字段。此代码只会将单击的按钮的数字附加到显示中。

对于其他按钮,您应该创建单独的操作,因为它们具有不同的功能。

关于java - 让计算器上的按钮具有交互性(以便它们实际上可以加、减等)的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073385/

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