Search code examples
javaswingjbutton

Make button only visible when variable reaches certain value is not working


Im trying to make a cookie cliker like "game" and my current obstacle is to make it so a button only turns visible when you have enough gold. this is all my code: (without start class)

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class GUI extends JFrame implements ActionListener{
    JFrame JFrame = new JFrame();
    private JButton klick, upgrade1, miner1;
    private JTextField punkte;
 static JLabel test;
    private JLabel Pickaxe;
    private JLabel Goldbarren;
    private JLabel minerlbl1;
    public static int minerkosten=200, pickaxekosten=50;
    public static int klicks=0; //Dies ist Ihre Punkzahl.
    int k = 1; //Diese Variable gibt an, wieviel Punkte Sie pro Klick erhalten.

{

}



public static class Miner extends Thread{
    public void run() {
        while (klicks<1000000000){
            try{
                Thread.sleep(1000);
            } catch (Exception e){
                System.err.println(e.getMessage());
            }
            klicks = klicks + 1;
            test.setText("Gold:" + " " + klicks);
        }
    }
}

public GUI(){
    this.setSize(1650, 1080);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    this.setUndecorated(false);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    klick = new JButton(new ImageIcon("Goldbarren.png")); //Dies ist der Button den Sie drücken müssen um Punkte zu erhalten.
    klick.setIcon(new javax.swing.ImageIcon(getClass().getResource("Goldbarren.png")));
    klick.setBorderPainted(false);
    klick.setFocusPainted(false);
    klick.setContentAreaFilled(false);
    klick.setBounds(50, 50, 150, 150);



    upgrade1 = new JButton(new ImageIcon("Pickaxe.png")); //Dies ist der erste Upgrade Button
    upgrade1.setIcon(new javax.swing.ImageIcon(getClass().getResource("Pickaxe.png")));
    upgrade1.setBorderPainted(false);
    upgrade1.setFocusPainted(false);
    upgrade1.setContentAreaFilled(false);
    upgrade1.setBounds(200, 200, 150, 150);
    upgrade1.setVisible(true);


    miner1 = new JButton(new ImageIcon("images.jpg")); //Dies ist das zweite Upgrade, es sollte pro Sekunde einen Punkt geben.
    miner1.setIcon(new javax.swing.ImageIcon(getClass().getResource("images.jpg")));
    miner1.setBorderPainted(false);
    miner1.setFocusPainted(false);
    miner1.setContentAreaFilled(false);
    miner1.setBounds(500, 200, 150, 150);



    minerlbl1= new JLabel("miner1"); //Dies ist die Beschriftung des zweiten Upgrades.
    minerlbl1= new JLabel("Miner!");
    minerlbl1.setBounds(10,10,10,10);
    minerlbl1.setFont(new Font("Courier new", Font.BOLD, 24));
    minerlbl1.setForeground(Color.BLUE);



    Pickaxe= new JLabel("Pickaxe"); //Dies ist die Beschriftung des zweiten Upgrades.
    Pickaxe = new JLabel("Pickaxe");
    Pickaxe.setBounds(220, 250, 150, 150);
    Pickaxe.setFont(new Font("Courier new", Font.BOLD, 24));
    Pickaxe.setForeground(Color.BLUE);



    test= new JLabel("klicks"); //Dieses Label gibt Ihre Anzahl Punkte an.
    test = new JLabel("Points: "+klicks,
            SwingConstants.CENTER);
    test.setBounds(400, 10, 400, 50);
    test.setFont(new Font("Courier new", Font.BOLD, 24));
    test.setForeground(Color.BLUE);

    Goldbarren= new JLabel("Goldbarren"); //Dies ist die Beschreibung des Goldbarrens
    Goldbarren= new JLabel("Mine!");
    Goldbarren.setBounds(90, 90, 150, 150);
    Goldbarren.setFont(new Font("Courier new", Font.BOLD, 24));
    Goldbarren.setForeground(Color.BLUE);


            punkte = new JTextField(""); //Dies ist das Textfield der Punkte.
    punkte.setBounds(1, 1, 1, 1);
    punkte.setFont(new Font("Courier new", Font.BOLD, 24));
    punkte.setForeground(Color.green);
    punkte.setBackground(Color.white);

    //JFrame.add(HoverButton("HoverButton"));




    this.add(punkte); //Dies sind die eingefügten Dinge.
    this.add(klick);
    this.add(test);
    this.add(miner1);
    this.add(upgrade1);
    this.add(Pickaxe);
    this.add(Goldbarren);
    this.add(minerlbl1);
    this.setVisible(true);




//Dies sind die ActionListener zu alllen Buttons.
    klick.addActionListener(new ActionListener() {


         public void actionPerformed(ActionEvent ae){

              klicks += k;
                test.setText("Gold:" + " " + klicks);


         }
    });



    upgrade1.addActionListener(new ActionListener() {


         public void actionPerformed(ActionEvent ae){

                if (klicks >= 50) {
                    upgrade1.setVisible(true);
                }





             klicks -= pickaxekosten;
             test.setText("Gold:" + " " + klicks);
              k += 1;
             pickaxekosten = pickaxekosten+4;
         }
         });

    miner1.addActionListener(new ActionListener() {


         public void actionPerformed(ActionEvent ae){

                if (klicks == 200) {
                    miner1.setVisible(true);

                }



             klicks -= minerkosten;
             test.setText("Gold:" + " " + klicks);
             new Miner().start();
             minerkosten = minerkosten+5;
         }
         });




}

private Component HoverButton(String string) {
    // TODO Auto-generated method stub
    return null;
}

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

}

}

So i was trying to make the button invisible and add this:

    if (klicks >= 50) {
                    upgrade1.setVisible(true);
                }

to make the button visible as soon as the player hits 50 "klicks" But by doing that the button never turns visible and its like it isnt even there.

So guys, what can i do here? Help would be much appreciated


Solution

  • Just add the component when the condition is reached. Any component added to a visible frame is visible (the property controls all the hierarchy).

    First don't add it to the UI, so remove line, so the component will not be visible:

    this.add(upgrade1);
    

    Modify the callback for any button that may increase the gold, for example:

    klick.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae){
           klicks += k;
           test.setText("Gold:" + " " + klicks);
           if (klicks >= 10) {
             GUI.this.add(upgrade1);
           }
         }
       });
    

    Be careful not to make the frame visible until every component have been added at start, so remove the first call to this.setVisible(true) at the beginning of the constructor.

    An alternative would be to let the component being visible but make it enable/disable when needed.