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

时间:2016-03-20 02:01:13

标签: java jtogglebutton

我正在尝试用java创建一个涂鸦神风格游戏,我无法弄清楚如何制作它,所以当两个JToggleButtons切换时它会出现第三个。这是我的尝试,但它没有任何建议。我是新来的,令人困惑。我想我需要一个ActionListener,但我不确定它是如何工作的。我正在使用Eclipse w / windows builder。感谢您接受这一挑战!

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 个答案:

答案 0 :(得分: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);
            }
        }
    });

 }
}

以下是工作解决方案的示例。它不是最好的书面文件,但我是从现有的代码设置中完成的,并试图真正推动重要部分,让您了解问题的解决方案是什么样的。