如何用Java GUI开发一个计算器

下面是一个简单的Java GUI计算器的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculatorGUI extends JFrame implements ActionListener { private JPanel panel; private JTextField textField; private JButton[] buttons; private String[] buttonLabels = { \t"7", "8", "9", "/", \t"4", "5", "6", "*", \t"1", "2", "3", "-", \t"0", ".", "=", "+" }; private String currentInput = ""; private double result = 0; private String operator = ""; public CalculatorGUI() { panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); textField = new JTextField(); textField.setEditable(false); buttons = new JButton[buttonLabels.length]; for (int i = 0; i < buttonLabels.length; i++) { buttons[i] = new JButton(buttonLabels[i]); buttons[i].addActionListener(this); panel.add(buttons[i]); } add(textField, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Calculator"); setSize(300, 400); setLocationRelativeTo(null); setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": currentInput += command; textField.setText(currentInput); break; case ".": if (!currentInput.contains(".")) { currentInput += "."; } textField.setText(currentInput); break; case "/": case "*": case "-": case "+": operator = command; result = Double.parseDouble(currentInput); currentInput = ""; textField.setText(""); break; case "=": double secondOperand = Double.parseDouble(currentInput); switch (operator) { case "/": result /= secondOperand; break; case "*": result *= secondOperand; break; case "-": result -= secondOperand; break; case "+": result += secondOperand; break; } currentInput = String.valueOf(result); textField.setText(currentInput); break; default: break; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new CalculatorGUI(); } }); } } ``` 可以通过创建CalculatorGUI对象来实例化计算器GUI,并使用`SwingUtilities.invokeLater()`方法在事件分发线程中启动应用程序。

如何用Java GUI开发一个时钟

以下是一个用Java GUI开发一个时钟的示例代码: ```java import javax.swing.*; import java.awt.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Clock extends JFrame { private JLabel timeLabel; public static void main(String[] args) { SwingUtilities.invokeLater(() -> { Clock clock = new Clock(); clock.setVisible(true); }); } public Clock() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Clock"); setSize(300, 200); setLocationRelativeTo(null); timeLabel = new JLabel(); timeLabel.setFont(new Font("Arial", Font.BOLD, 40)); timeLabel.setHorizontalAlignment(SwingConstants.CENTER); updateTime(); Timer timer = new Timer(1000, e -> updateTime()); timer.start(); getContentPane().add(timeLabel); } private void updateTime() { Date currentTime = Calendar.getInstance().getTime(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String timeText = dateFormat.format(currentTime); timeLabel.setText(timeText); } } ``` 这个示例代码创建了一个继承自`JFrame`的`Clock`类。在构造函数中,设置了窗口的一些基本属性,并创建了一个`JLabel`来显示时间。然后,使用`Timer`类每隔一秒更新时间。`updateTime()`方法使用`SimpleDateFormat`将当前时间格式化为"HH:mm:ss"的字符串,然后将其设置为`timeLabel`的文本。最后,将`timeLabel`添加到窗口的内容面板中。 要编译和运行这个代码,你需要安装Java开发工具包(Java Development Kit,JDK),并将代码保存为一个名为`Clock.java`的文件。然后,在命令行窗口中使用`javac Clock.java`编译代码,再使用`java Clock`运行程序。