Java Swing是一个强大的GUI工具包,用于编写基于Java的图形界面。下面是编写一个简单的计算器软件的完整攻略:
1.设计图形界面
首先,需要设计图形界面,包括按钮、文本框和标签等控件。可以使用Eclipse或NetBeans等IDE工具来快速创建Swing应用程序。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 330);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.BOLD, 20));
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10, 10, 218, 50);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton_1 = new JButton("1");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_1.setBounds(10, 70, 50, 50);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("2");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
btnNewButton_2.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_2.setBounds(70, 70, 50, 50);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("3");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
btnNewButton_3.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_3.setBounds(130, 70, 50, 50);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("4");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
btnNewButton_4.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_4.setBounds(10, 130, 50, 50);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("5");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
btnNewButton_5.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_5.setBounds(70, 130, 50, 50);
contentPane.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("6");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
btnNewButton_6.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_6.setBounds(130, 130, 50, 50);
contentPane.add(btnNewButton_6);
JButton btnNewButton_7 = new JButton("7");
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
btnNewButton_7.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_7.setBounds(10, 190, 50, 50);
contentPane.add(btnNewButton_7);
JButton btnNewButton_8 = new JButton("8");
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
btnNewButton_8.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_8.setBounds(70, 190, 50, 50);
contentPane.add(btnNewButton_8);
JButton btnNewButton_9 = new JButton("9");
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
btnNewButton_9.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_9.setBounds(130, 190, 50, 50);
contentPane.add(btnNewButton_9);
JButton btnNewButton_10 = new JButton("0");
btnNewButton_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
btnNewButton_10.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_10.setBounds(70, 250, 50, 50);
contentPane.add(btnNewButton_10);
JButton btnNewButton_11 = new JButton("+");
btnNewButton_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "+");
}
});
btnNewButton_11.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_11.setBounds(190, 70, 50, 50);
contentPane.add(btnNewButton_11);
JButton btnNewButton_12 = new JButton("-");
btnNewButton_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "-");
}
});
btnNewButton_12.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_12.setBounds(190, 130, 50, 50);
contentPane.add(btnNewButton_12);
JButton btnNewButton_13 = new JButton("*");
btnNewButton_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "*");
}
});
btnNewButton_13.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_13.setBounds(190, 190, 50, 50);
contentPane.add(btnNewButton_13);
JButton btnNewButton_14 = new JButton("/");
btnNewButton_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "/");
}
});
btnNewButton_14.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_14.setBounds(190, 250, 50, 50);
contentPane.add(btnNewButton_14);
JButton btnNewButton_15 = new JButton("C");
btnNewButton_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
btnNewButton_15.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_15.setBounds(130, 250, 50, 50);
contentPane.add(btnNewButton_15);
JButton btnNewButton_16 = new JButton("=");
btnNewButton_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = 0;
String[] str = textField.getText().split("\\+|\\-|\\*|\\/");
if (textField.getText().contains("+")) {
result = Integer.parseInt(str[0]) + Integer.parseInt(str[1]);
} else if (textField.getText().contains("-")) {
result = Integer.parseInt(str[0]) - Integer.parseInt(str[1]);
} else if (textField.getText().contains("*")) {
result = Integer.parseInt(str[0]) * Integer.parseInt(str[1]);
} else if (textField.getText().contains("/")) {
result = Integer.parseInt(str[0]) / Integer.parseInt(str[1]);
}
textField.setText(String.valueOf(result));
}
});
btnNewButton_16.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton_16.setBounds(10, 250, 50, 50);
contentPane.add(btnNewButton_16);
}
}
2.实现计算器核心逻辑
在上述代码中,添加了数字、运算符和功能按钮,并为这些按钮添加了监听器。当按钮被单击时,将在文本框中显示相应的字符。而“=”按钮的监听器则实现了计算器的核心逻辑:将输入的字符串分离为数字和运算符,并进行相应的计算,最后将结果显示在文本框中。
JButton btnNewButton_16 = new JButton("=");
btnNewButton_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = 0;
String[] str = textField.getText().split("\\+|\\-|\\*|\\/");
if (textField.getText().contains("+")) {
result = Integer.parseInt(str[0]) + Integer.parseInt(str[1]);
} else if (textField.getText().contains("-")) {
result = Integer.parseInt(str[0]) - Integer.parseInt(str[1]);
} else if (textField.getText().contains("*")) {
result = Integer.parseInt(str[0]) * Integer.parseInt(str[1]);
} else if (textField.getText().contains("/")) {
result = Integer.parseInt(str[0]) / Integer.parseInt(str[1]);
}
textField.setText(String.valueOf(result));
}
});
3.示例说明
示例1
当用户输入“5+3”并单击“=”按钮时,程序将计算5+3的值,并将结果8显示在文本框中。
示例2
当用户输入“64”并单击“=”按钮时,程序将计算64的值,并将结果24显示在文本框中。
运行程序之后,用户可以使用鼠标或键盘来输入数字和操作符,计算器将根据用户的输入计算结果。总之,这是一个简单实用的计算器软件。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java Swing编写一个简单的计算器软件 - Python技术站