Counting Toggle Buttons and

369 Views Asked by At

http://i415.photobucket.com/albums/pp235/wong93_photos/Untitled-3.png

yo guys I have few questions here, before I ask, what I want do with this program is to count number of toggles that have been pressed and set the total in the jTextField below "Quantity . For example, I pressed on A and B toggle buttons, so quantity will display 2, and jLabel7 will display 20

1) How can I count number of toggles that have been pressed?? I can't find this anywhere

2) How to input value into jTextField?? I know it is basic but I don't know how, I searched around and all of them are about retrieving information from textfield instead of inserting

3) How can I pass the value from jLabel7 to my other jFrame?? Cause I wanna use it as receipt, e.g: Total cost is 20

Thanks a lot !

1

There are 1 best solutions below

0
On
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Example extends JPanel {

    private static final long serialVersionUID = 1L;

    private int toggle_count = 0;
    private JTextField text_field;
    private JLabel label;

    public Example() {

        this.setLayout(new FlowLayout());

        JButton button_a = new JButton("Button A");
        this.add(button_a);

        JButton button_b = new JButton("Button B");
        this.add(button_b);

        text_field = new JTextField("0");
        text_field.setPreferredSize(new Dimension(50, 20));
        text_field.setEditable(false);
        this.add(text_field);

        label = new JLabel("x 10 = 0");
        this.add(label);

        button_a.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        button_b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        JButton receipt = new JButton("Receipt");
        this.add(receipt);

        receipt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                SomeClass.main(toggle_count*10);
            }
        });

    }

    public void updateToggle() {

        toggle_count++;
        text_field.setText("" +toggle_count);
        label.setText("x 10 = " + toggle_count * 10);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(new Example());

        frame.setVisible(true);
    }
}

The SomeClass is the other class with the main method that takes your toggle_count*10 as an argument