-1

I am trying to create a doodle god style game in java, I cant figure out how to make it so when two JToggleButtons are toggled it makes a third appear. Here is my attempt, but it is not working any suggestions. I am new at this and its confusing. I think I need an ActionListener somewhere, but im not to sure how that works. Im using Eclipse w/ windows builder. Thanks for taking on this challenge!

public class New {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                New window = new New();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public New() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JToggleButton waterButton = new JToggleButton("water");
    waterButton.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
           if(ev.getStateChange()==ItemEvent.SELECTED){
             System.out.println("waterButton is toggled");
           }else if(ev.getStateChange()==ItemEvent.DESELECTED){
             System.out.println("waterButton is untoggled");
           }
        }
    });

    waterButton.setBounds(6, 6, 161, 29);
    frame.getContentPane().add(waterButton);

    JToggleButton fireButton = new JToggleButton("fire");
    fireButton.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
           if(ev.getStateChange()==ItemEvent.SELECTED){
             System.out.println("fireButton is toggled");
           }else if(ev.getStateChange()==ItemEvent.DESELECTED){
             System.out.println("fireButton is untoggled");
           }
        }
    });

    fireButton.setBounds(6, 41, 161, 29);
    frame.getContentPane().add(fireButton);

    JToggleButton steamButton = new JToggleButton("steam");
    steamButton.setBounds(6, 82, 161, 29);
    frame.getContentPane().add(steamButton);
    //steamButton.setVisible(false);

    if (waterButton.isSelected() && fireButton.isSelected()){
        steamButton.setVisible(true);
    }
    else{
        steamButton.setVisible(false);
    }
}

}

1 Answers1

1
public class DoodleGodTestOrganized {

JFrame frame = new JFrame("DoodleGod");
public static JToggleButton waterButton = new JToggleButton("Water");
public static JToggleButton fireButton = new JToggleButton("Fire");
public static JToggleButton steamButton = new JToggleButton("Stream");


public DoodleGodTestOrganized(){
    frame();

}

public void frame(){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 340);
    frame.setTitle("Doogle God");
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    frame.setVisible(true);

    JPanel panel = new JPanel();
    panel.add(waterButton);
    panel.add(fireButton);
    panel.add(steamButton);
    steamButton.setVisible(false);
    panel.setVisible(true);
    frame.add(panel, BorderLayout.NORTH);
    frame.setVisible(true);

}


public static void main(String[] args) {
    new DoodleGodTestOrganized();

    //Created actionListener for the waterbutton
    waterButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(waterButton.isSelected() && fireButton.isSelected()){
                steamButton.setVisible(true);
            }else{
                steamButton.setVisible(false);
            }
        }
    });


//Actionlistener for the fireButton
    fireButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(waterButton.isSelected() && fireButton.isSelected()){
                steamButton.setVisible(true);
            }else{
                steamButton.setVisible(false);
            }
        }
    });

 }
}

Here is a example of a working solution. It is not the best written one but I made it out of your existing code setup and tried to really push on the important parts to give you an understanding of what a working solution to the problem CAN look like.

TheMirrox
  • 368
  • 2
  • 13
  • I'm not sure what you are trying to accomplish with the new code. I do think that you have gotten the concept a bit wrong. There are a lot of good tutorial out there on the subject =) – TheMirrox Mar 20 '16 at 03:34
  • Updated my answer with a, not to beautiful but working, solution based on the code that you provided. – TheMirrox Mar 20 '16 at 04:08
  • any suggestions to make it stay once it is visible the first time. I will look further into this tomorrow, but You may have some suggestions. Trying to make it so its not visible when opened. Then when you make the combo once, it stays throughout the rest of the program. Thanks so much for the effort your putting into this, very informative for me – Thomas Donnelly Mar 20 '16 at 04:32
  • Just remove the else statements in the action handlers ;) – TheMirrox Mar 20 '16 at 04:33
  • thats so easy, yet i would have never found it. Looking into the code one more time, what the difference between using an actionlistener and an itemlistener? – Thomas Donnelly Mar 20 '16 at 04:38
  • An ActionListener handles events that occur when you do something with a component, like pressing a button. An ItemListener handles events that occur when a component like a radio button or a checkbox changes it's state. There is a lot of material on the web for you to read up on this kind of programming. A lot to learn =) – TheMirrox Mar 20 '16 at 04:46