我正在嘗試將JRadioButton鏈接到一個(gè)接口和一個(gè)實(shí)現(xiàn)該接口的方法。我有兩種模式,即攻擊和防御模式,我希望用戶能夠單擊攻擊切換按鈕并啟用它,當(dāng)它被解鎖時(shí),它會(huì)激活防御模式類中的代碼public class AttackMode implements GameBehaviour { public boolean behaviourMode(int numOfShips) { if(numOfShips < 3){ return true; } return false; }}Code From Java Swing GUIprivate JRadioButton getRdbtnDefensiveMode() { if (rdbtnDefensiveMode == null) { rdbtnDefensiveMode = new JRadioButton("Defensive Mode"); rdbtnDefensiveMode.setEnabled(); rdbtnDefensiveMode.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent arg0) { if (arg0.getStateChange() == ItemEvent.SELECTED) { theRules.theMasterShip.setBadAssBehaviour(new DefenceMode()); rdbtnAttackMode.setSelected(false); }else if (arg0.getStateChange() == ItemEvent.DESELECTED) { theRules.theMasterShip.setBadAssBehaviour(new AttackMode()); rdbtnAttackMode.setSelected(true); } return; } }); rdbtnDefensiveMode.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 11)); rdbtnDefensiveMode.setBounds(658, 355, 109, 23); } return rdbtnDefensiveMode; }我期望如果單擊攻擊按鈕,它應(yīng)該實(shí)現(xiàn)攻擊方法,如果未選中它們,則應(yīng)激活防御模式按鈕。因此,必須始終啟用其中一個(gè)按鈕
添加回答
舉報(bào)
0/150
提交
取消