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);
}
}
}
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.