Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.SwingConstants;
- import java.awt.Font;
- import java.awt.Color;
- import java.awt.SystemColor;
- public class CalculatorUKTC {
- private JFrame frame;
- private JTextField num1Field;
- private JTextField num2Field;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- CalculatorUKTC window = new CalculatorUKTC();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public CalculatorUKTC() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.getContentPane().setBackground(SystemColor.menu);
- frame.getContentPane().setForeground(Color.BLACK);
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- num1Field = new JTextField();
- num1Field.setBounds(169, 44, 86, 20);
- frame.getContentPane().add(num1Field);
- num1Field.setColumns(10);
- num2Field = new JTextField();
- num2Field.setBounds(169, 87, 86, 20);
- frame.getContentPane().add(num2Field);
- num2Field.setColumns(10);
- JLabel resultLbl = new JLabel("Result:");
- resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
- resultLbl.setHorizontalAlignment(SwingConstants.LEFT);
- resultLbl.setBounds(47, 210, 149, 27);
- frame.getContentPane().add(resultLbl);
- JLabel lblNewLabel = new JLabel("Number 1:");
- lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
- lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
- lblNewLabel.setBounds(29, 44, 108, 17);
- frame.getContentPane().add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("Number 2:");
- lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
- lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
- lblNewLabel_1.setBounds(29, 87, 108, 17);
- frame.getContentPane().add(lblNewLabel_1);
- JButton clearBtn = new JButton("C");
- clearBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- num1Field.setText("");
- num2Field.setText("");
- resultLbl.setText("Result:");
- }
- });
- clearBtn.setBounds(29, 140, 49, 44);
- frame.getContentPane().add(clearBtn);
- JButton multiplyBtn = new JButton("X");
- multiplyBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- double num1 = Double.parseDouble(num1Field.getText());
- double num2 = Double.parseDouble(num2Field.getText());
- double result = num1 * num2;
- resultLbl.setText("Result: " + result);
- } catch (NumberFormatException e1) {
- resultLbl.setText("Invalid input!");
- num1Field.setText("");
- num2Field.setText("");
- }
- }
- });
- multiplyBtn.setBounds(88, 140, 49, 44);
- frame.getContentPane().add(multiplyBtn);
- JButton devideBtn = new JButton("/");
- devideBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- double num1 = Double.parseDouble(num1Field.getText());
- double num2 = Double.parseDouble(num2Field.getText());
- double result = num1 / num2;
- resultLbl.setText("Result: " + result);
- } catch (NumberFormatException e1) {
- resultLbl.setText("Invalid input!");
- num1Field.setText("");
- num2Field.setText("");
- }
- }
- });
- devideBtn.setBounds(147, 140, 49, 44);
- frame.getContentPane().add(devideBtn);
- JButton sumBtn = new JButton("+");
- sumBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- double num1 = Double.parseDouble(num1Field.getText());
- double num2 = Double.parseDouble(num2Field.getText());
- double result = num1 + num2;
- resultLbl.setText("Result: " + result);
- } catch (NumberFormatException e1) {
- resultLbl.setText("Invalid input!");
- num1Field.setText("");
- num2Field.setText("");
- }
- }
- });
- sumBtn.setBounds(206, 140, 49, 44);
- frame.getContentPane().add(sumBtn);
- JButton button_2 = new JButton("-");
- button_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- double num1 = Double.parseDouble(num1Field.getText());
- double num2 = Double.parseDouble(num2Field.getText());
- double result = num1 - num2;
- resultLbl.setText("Result: " + result);
- } catch (NumberFormatException e1) {
- resultLbl.setText("Invalid input!");
- num1Field.setText("");
- num2Field.setText("");
- }
- }
- });
- button_2.setBounds(265, 140, 49, 44);
- frame.getContentPane().add(button_2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement