下面为您详细讲解“Java实现图形化界面计算器”的完整攻略。
1. 准备工作
在开始之前,需要确保您已经正确安装了Java开发环境(JDK),以及Java集成开发工具(IDE),如Eclipse或IntelliJ IDEA。
2. 创建界面
使用Java Swing工具包,可以很容易地创建图形化用户界面。您可以通过创建一个JFrame实例作为主窗口,然后添加其他的Swing组件,如文本框、按钮、标签等来实现计算器的图形化界面。
参考以下示例代码,创建一个简单的计算器界面:
import java.awt.*;
import javax.swing.*;
public class Calculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 4;
JTextField textField = new JTextField(15);
panel.add(textField, constraints);
constraints.gridwidth = 1;
constraints.gridy = 1;
addButton(panel, constraints, "7");
addButton(panel, constraints, "8");
addButton(panel, constraints, "9");
addButton(panel, constraints, "/");
constraints.gridy = 2;
addButton(panel, constraints, "4");
addButton(panel, constraints, "5");
addButton(panel, constraints, "6");
addButton(panel, constraints, "*");
constraints.gridy = 3;
addButton(panel, constraints, "1");
addButton(panel, constraints, "2");
addButton(panel, constraints, "3");
addButton(panel, constraints, "-");
constraints.gridy = 4;
addButton(panel, constraints, "0");
addButton(panel, constraints, ".");
addButton(panel, constraints, "=");
addButton(panel, constraints, "+");
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void addButton(Container parent, GridBagConstraints constraints, String text) {
JButton button = new JButton(text);
constraints.gridx++;
parent.add(button, constraints);
}
}
3. 实现计算逻辑
计算器主要的功能是计算数字并显示结果。因此,我们需要为每个按钮添加适当的操作,以便我们可以进行数字运算。
在添加按钮之后,我们还需要定义一个ActionListener接口的实现类来以响应按钮的点击事件。
参考以下示例代码,实现加减乘除和等于操作的计算逻辑:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator implements ActionListener {
private static JTextField textField;
private static double num1 = 0;
private static double num2 = 0;
private static String operator = "";
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 4;
textField = new JTextField(15);
panel.add(textField, constraints);
constraints.gridwidth = 1;
constraints.gridy = 1;
addButton(panel, constraints, "7");
addButton(panel, constraints, "8");
addButton(panel, constraints, "9");
addButton(panel, constraints, "/");
constraints.gridy = 2;
addButton(panel, constraints, "4");
addButton(panel, constraints, "5");
addButton(panel, constraints, "6");
addButton(panel, constraints, "*");
constraints.gridy = 3;
addButton(panel, constraints, "1");
addButton(panel, constraints, "2");
addButton(panel, constraints, "3");
addButton(panel, constraints, "-");
constraints.gridy = 4;
addButton(panel, constraints, "0");
addButton(panel, constraints, ".");
addButton(panel, constraints, "=");
addButton(panel, constraints, "+");
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.matches("[0-9]")) {
textField.setText(textField.getText() + command);
} else if (command.equals(".")) {
if (!textField.getText().contains(".")) {
textField.setText(textField.getText() + ".");
}
} else if (command.equals("+")) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (command.equals("-")) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (command.equals("*")) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (command.equals("/")) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (command.equals("=")) {
num2 = Double.parseDouble(textField.getText());
double result = 0;
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
textField.setText(Double.toString(result));
}
}
private static void addButton(Container parent, GridBagConstraints constraints, String text) {
JButton button = new JButton(text);
button.addActionListener(new Calculator());
constraints.gridx++;
parent.add(button, constraints);
}
}
示例说明
示例一
如果要计算1 + 2,我们需要做如下步骤:
- 在计算器上按下“1”按钮;
- 在计算器上按下“+”按钮;
- 在计算器上按下“2”按钮;
- 在计算器上按下“=”按钮;
- 计算器将显示结果“3”。
在这个示例中,我们演示了如何使用计算器进行加法运算。
示例二
现在我们想计算10 / 5。我们可以执行以下操作:
- 在计算器上按下“1”按钮;
- 在计算器上按下“0”按钮;
- 在计算器上按下“/”按钮;
- 在计算器上按下“5”按钮;
- 在计算器上按下“=”按钮;
- 计算器将显示结果“2.0”。
在这个示例中,我们演示了如何使用计算器进行除法运算。由于我们使用了浮点数来表示结果,因此我们的结果将显示为“2.0”。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现图形化界面计算器 - Python技术站