How to develop a calculator using Java GUI

The following is an example code for a simple Java GUI calculator: ```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(); } }); } } ``` You can instantiate the calculator GUI by creating a CalculatorGUI object and use the 'SwingUtilities. invokeLater()' method to launch the application in the event distribution thread.

How to develop a clock using Java GUI

The following is an example code for developing a clock using a 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); } } ``` This example code creates a 'Clock' class that inherits from 'JFrame'. In the constructor, some basic properties of the window were set and a 'JLabel' was created to display the time. Then, use the 'Timer' class to update the time every second` The updateTime() method uses' SimpleDateFormat 'to format the current time as a string of' HH: mm: ss', and then sets it to the text of 'timeLabel'. Finally, add 'timeLabel' to the content panel of the window. To compile and run this code, you need to install the Java Development Kit (JDK) and save the code as a file called 'Clock. java'. Then, compile the code using 'Javac Clock. java' in the command line window, and then run the program using 'Java Clock'.