在 java 中使用 JToggleButton 使其他按钮可见

Using JToggleButton in java to make other buttons visible

我正在尝试在 java 中创建涂鸦之神风格的游戏,但我不知道如何制作它,所以当切换两个 JToggleButton 时,它会出现第三个。这是我的尝试,但没有任何建议。我对此很陌生,而且很困惑。我想我在某处需要一个 ActionListener,但我不确定它是如何工作的。我正在使用带 windows 生成器的 Eclipse。感谢您接受这个挑战!

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

 }
}

这是一个可行的解决方案示例。它不是写得最好的,但我是根据您现有的代码设置制作的,并试图真正推动重要部分,让您了解问题的有效解决方案可能是什么样子。