vendredi 19 février 2016

Checkbox to change button actionlistener

I'm trying to make in a small program I'm creating that when one checkbox is selected the action performed for a specific button to be changed to another action performed, let's say the button action performed gives the result of 1+2, I want when I check the checkbox to make the button action performed be 1-3 just for example.

Here is my code

import java.awt.*;

public class frame extends JFrame { // Declaring the frame class

public frame() { // crating a constructor 
    getContentPane().setLayout(new GridLayout(5,2)); // Creating 5 lines and 2 rows
    // first label for adding the first number
    JLabel label = new JLabel("First Number");
    getContentPane().add(label);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    // first text field
    final JTextField text = new JTextField(8);
    getContentPane().add(text);
    text.setHorizontalAlignment(SwingConstants.CENTER);
    // second label for adding second number
    JLabel label1 = new JLabel("Second Number");
    getContentPane().add(label1);
    label1.setHorizontalAlignment(SwingConstants.CENTER);
    // second text field
    final JTextField text1 = new JTextField();
    getContentPane().add(text1);
    text1.setHorizontalAlignment(SwingConstants.CENTER);
    // third label for the result
    JLabel label2 = new JLabel("Result is");
    getContentPane().add(label2);
    label2.setHorizontalAlignment(SwingConstants.CENTER);
    // third text field to show the result
    final JTextField text2 = new JTextField();
    getContentPane().add(text2);
    text2.setHorizontalAlignment(SwingConstants.CENTER);
    text2.setEditable(false);
    // Calculation button
    JButton btn = new JButton("Calculate");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String number1, number2, result, ans;
            try {
                /*number1 = Double.parseDouble(text.getText());
                number2 = Double.parseDouble(text1.getText());
                result = number1*number2;
                text2.setText(Double.toString(result));*/
                if (text.getText().isEmpty() || text1.getText().isEmpty()) {
                    throw new Exception();
                }

                number1 = text.getText();
                number2 = text1.getText();
                result = number1.concat(number2);
                text2.toString();
                text2.setText(result);

            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null, "Please add a letter");
            }
        }
    });     
    getContentPane().add(btn);
    // Clear button
    JButton clear = new JButton("Clear");
    getContentPane().add(clear);

    final JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
    getContentPane().add(chckbxNewCheckBox);

    final JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New check box");
    getContentPane().add(chckbxNewCheckBox_1);

    chckbxNewCheckBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (chckbxNewCheckBox.isSelected()) {
                chckbxNewCheckBox_1.setSelected(false);
            }
        }
    });

    chckbxNewCheckBox_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (chckbxNewCheckBox_1.isSelected()) {
                chckbxNewCheckBox.setSelected(false);
            }
        }
    });

    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                text.setText("");
                text1.setText("");
                text2.setText("");
            } catch(Exception e1) {
            }
        }
    });

    //setVisible(true);
    //setSize(400,200);
    //setResizable(false);
    //setLocationRelativeTo(null);
    //setTitle("Calculator");
}

public static void main (String args[]) { // Creating the main method
    frame frame = new frame(); // creating an object of the ShowGridLayout class, object is frame.
    frame.setVisible(true);
    frame.setSize(400, 200);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setTitle("Calculator");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);;
}

}




Aucun commentaire:

Enregistrer un commentaire