1

I am trying to work on a little program just for fun, and I want it so when I click the pineapple button it adds a pineapple to a string of text that I want under the buttons, same for rock. So basically I need a counter for the rock and pineapple that goes under the buttons that just counts up each time the rock button is hit, and the pineapple button is hit.

This is what I have so far.

public class BGUI extends JFrame{



private JButton pine;
    private JButton rock;

    public BGUI(){
        super("Bikini Bottom Builder Game");
        setLayout(new FlowLayout());

        Icon p = new ImageIcon(getClass().getResource("b.png"));
        pine = new JButton("Add Pineapple", p);
        add(pine);

        Icon r = new ImageIcon(getClass().getResource("b.png"));
        rock = new JButton("Add Rock", r);
        rock.setRolloverIcon(r);
        add(rock);

        Add handler = new Add();
        pine.addActionListener(handler);
        rock.addActionListener(handler);

    }

    private class Add implements ActionListener{
        int pine = 0;
        int rock = 0;
        public void actionPerformed(ActionEvent event){
              pine++;

              rock++;

        }
    }
}

Any help will be greatly appreciated!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2

You might use a single GridBagLayout or GroupLayout for this, but I would approach it by combining layouts.

To do that here, you might have an 'outer' panel that is BorderLayout. Put the existing panel with FlowLayout into the CENTER of the border layout. Create another new panel with flow layout to put the 'bottom part' in. Add that panel to the PAGE_END of the border layout.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class BGUI extends JFrame {
private JButton pine;
private JButton rock;
private int pineappleCount = 0,
        rockCount = 0;

public BGUI(){
    super("Bikini Bottom Builder Game");
    setLayout(new FlowLayout());
    setSize(800, 800);

    Icon p = new ImageIcon(getClass().getResource("b.png"));
    pine = new JButton("Add Pineapple: " + pineappleCount, p);
    pine.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            pine.setText("Add Pineapple: " + ++pineappleCount);
        }

    });
    add(pine);

    Icon r = new ImageIcon(getClass().getResource("b.png"));
    rock = new JButton("Add Rock: " + rockCount, r);
    rock.setRolloverIcon(r);
    rock.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            rock.setText("Add Rock: " + ++rockCount);
        }

    });
    add(rock);

    super.setVisible(true);

}


public static void main(String[] a) {
    new BGUI();
}
    }

You had the right idea. You were just implementing your listeners wrong. I went ahead and added an anonymous inner class to your buttons in order to make that counting effect that you wanted. If you want the counter to be under the buttons, you would need to change the layout of the JFrame component to something else and use JLabel.