0

The question is poorly worded, but I'll try to explain. I've created a vending machine where a window pops up and asks user to enter money. They then move to the main machine window and it displays the number they entered as the amount of money they have. I have a button, 'Add Money' that is supposed to add money to that current amount, but i'm not sure how to do that.

For example, a user enters that they have 2 dollars, then hits the enter key which takes them to the main machine interface that states they have 2 dollars.. The user hits the 'add money' button and types 3, denoting that they have 3 more dollars. That should mean they have 5 dollars, and will be denoted on the main interface that they have 5 dollars.

Code for the money input...

public void actionPerformed(ActionEvent arg0) {
    double moneyInput;
    String text = mInput.getText();
    moneyInput = Double.parseDouble (text);

    VendingMachineInterface frame;
    try {
        frame = new VendingMachineInterface(vendingMachineName, moneyInput);
        frame.setVisible(true);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Ryan Dorman
  • 317
  • 1
  • 3
  • 10
  • 1
    Post an [MCVE](http://stackoverflow.com/help/mcve) (don't post all of your program, create a new small one that demonstrates the problem and that we can run ourselves). – user1803551 Apr 26 '15 at 22:32
  • 1
    I'm personally having trouble trying to visualize what the scenario is here. Could you include a screenshot or two as well as some additional context for your code segment? – snickers10m Apr 26 '15 at 22:32
  • 1
    You could use a CardLayout, especially of you want to reshow previous views again – MadProgrammer Apr 26 '15 at 22:32
  • @snickers10m Sorry about that, I updated it correctly now for the example to be more informative. – Ryan Dorman Apr 26 '15 at 22:52
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 27 '15 at 01:11
  • It looks to me like you want to refresh one label with the current value, and also to store the inputs in a file. did I understand you correctly? – Klemens Morbe Apr 27 '15 at 13:23

2 Answers2

1

This is one way to do what you want, but since you did not provide an MCVE this might not be what you were looking for.

public class VendingMachine extends JFrame {

    static int amount = 0;

    VendingMachine() {

        JButton add = new JButton("Add amount");
        JTextField moneyInput = new JTextField(8);
        JLabel currentAmount = new JLabel("Current amount:");
        JLabel amountLabel = new JLabel(String.valueOf(amount));

        setLayout(new FlowLayout());
        add(currentAmount);
        add(amountLabel);
        add(moneyInput);
        add(add);

        add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                String addAmountString = moneyInput.getText();
                int addAmount = 0;
                try {
                    addAmount = Integer.parseInt(addAmountString);
                } catch (NumberFormatException exp) {
                    System.out.println("Not a number, amount to add will be 0.");
                }
                amount += addAmount;
                moneyInput.setText("");
                amountLabel.setText(String.valueOf(amount));
            }
        });

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        String initialString = JOptionPane.showInputDialog("Enter initial $");
        try {
            amount = Integer.parseInt(initialString);
        } catch (NumberFormatException e) {
            System.out.println("Not a number, initial amount will be 0.");
        }
        new VendingMachine();
    }
}

Notes:

  • You can use an InputVerifier for the text fields instead of checking the value after it is entered.
  • You can use the input dialog that I used at the beginning each time the button is pressed instead of having the text field in the main window.
user1803551
  • 12,965
  • 5
  • 47
  • 74
-1

I don't know if this is the best way to accomplish your task, but I've used it before in a similar type of application.

Basically, inside the JFrame I had a JPanel which existed only to switch between other panels using the add() and remove() methods.

I created a ManagerPanel class, which has the following method:

public void switchPanel(JPanel removePanel, JPanel addPanel) {
    this.remove(removePanel);
    this.add(addPanel);
    validate();
    repaint();
}

To switch panels, I used the following inside an action event:

((ManagerPanel)this.getParent()).switchPanel(currentPanel.this, newPanel);

Like I said, there might be a fancier solution out there, but this was easy and worked for me.

Kyle556
  • 26
  • 4
  • Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Apr 27 '15 at 01:10