-2

I am trying to make this code so that it adds or subtracts from the initial value presented each time you click submit. I have searched multiple threads on how to complete this and I haven't come across any that are really clear. A push in the right direction would really help.

package TwoPanelDesign;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;

public class TwoPanelDesign extends JFrame {

    private static final long serialVersionUID = 1L;

    public TwoPanelDesign() {

        getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 151, 262);
        getContentPane().add(panel);
        panel.setLayout(null);

        final JLabel lblYourChoice = new JLabel("Your choice...");
        lblYourChoice.setBounds(10, 11, 172, 14);
        panel.add(lblYourChoice);

        final JRadioButton rdbtnAdd = new JRadioButton("Add 2");
        rdbtnAdd.setBounds(10, 32, 109, 23);
        panel.add(rdbtnAdd);


        final JRadioButton rdbtnMinus = new JRadioButton("Minus 2");
        rdbtnMinus.setBounds(10, 58, 109, 23);
        panel.add(rdbtnMinus);

        ButtonGroup group = new ButtonGroup();
        group.add(rdbtnAdd);
        group.add(rdbtnMinus);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(97, 0, 349, 262);
        getContentPane().add(panel_1);
        panel_1.setLayout(null);
        panel_1.setBackground(Color.BLUE);

        final JLabel label = new JLabel("0");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(67, 0, 260, 262);
        label.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 45));
        panel_1.add(label);

        JButton btnSubmit = new JButton("Submit");
        btnSubmit.setBounds(10, 88, 88, 23);
        panel.add(btnSubmit);
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if (rdbtnAdd.isSelected())
                    label.setText("2");
                if (rdbtnMinus.isSelected())
                    label.setText("-2");
            }
        }
            );



    }

    public static void main(String[] args) {
        TwoPanelDesign A = new TwoPanelDesign();
        A.setSize(500,400);
        A.setVisible(true);
        A.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Does this work? Do you get an error? – Tom Jun 26 '14 at 01:35
  • For [example](http://stackoverflow.com/a/8703807/230513). – trashgod Jun 26 '14 at 01:36
  • It completely works, But it starts with a value of 0 and then once you click one of the radio buttons; either add 2 or minus 2 and submit it you can't keep updating the values because it is only replacing the 0 with a 2 or -2. I wan't it to add/subtract 2 and then be able to do it again and have it update each time – user3761914 Jun 26 '14 at 01:38
  • 2
    Welcome to Stack Overflow! You should refrain from using `null` layouts. You do not control the factors that affect the size requirements/decisions for components that will change their sizes on different platforms/os's – MadProgrammer Jun 26 '14 at 01:41

1 Answers1

3

You must retain the previous result:

public void actionPerformed(ActionEvent e){
  int v = Integer.parseInt(label.getText());
  if (rdbtnAdd.isSelected()){
    v += 2;
  } else {
    v -= 2;
  }
  label.setText(Integer.toString(v));
}