我正在尝试将JRadioButton链接到接口以及实现该接口的方法。我有两种模式,分别是攻击和防御模式,我希望用户能够单击攻击切换按钮并启用它,而当未单击该按钮时,它将激活防御模式
班上的代码
public class AttackMode implements GameBehaviour {
public boolean behaviourMode(int numOfShips) {
if(numOfShips < 3){
return true;
}
return false;
}
}
Code From Java Swing GUI
private 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;
}
private JRadioButton getRdbtnAttackMode() {
if (rdbtnAttackMode == null) {
rdbtnAttackMode = new JRadioButton("Attack Mode");
rdbtnAttackMode.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
theRules.theMasterShip.setBadAssBehaviour(new AttackMode());
rdbtnDefensiveMode.setSelected(false);
}else if (arg0.getStateChange() == ItemEvent.DESELECTED) {
theRules.theMasterShip.setBadAssBehaviour(new DefenceMode());
rdbtnDefensiveMode.setSelected(true);
}
}
});
rdbtnAttackMode.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 11));
rdbtnAttackMode.setBounds(658, 265, 109, 23);
}
return rdbtnAttackMode;
}
我期望如果单击攻击按钮,则应实施攻击方法,如果未选中该按钮,则应激活防御模式按钮。因此必须始终启用按钮之一
答案 0 :(得分:0)
将按钮添加到button group
赞:
<form id="validationForm" #validationForm="ngForm" (ngSubmit)="onFormSubmit(validationForm)">
<div class="row">
<div id="u79" class="large-7 columns" data-label="businessNumber">
<label for="u79_input">
<abbr title="example">e.g.</abbr>{{'BUSINESS-NUMBER-PAGE.EXAMPLE' | translate}}
<input id="u79_input" type="text" value="" name="businessNumber" [(ngModel)] = "businessNumber" required minlength="9" maxlength="9" pattern="^\d{9}$" #uname="ngModel" (focus)="setErrors()" class="no-margin"/>
</label>
<!-- inline error -->
<div *ngIf="!hasErrors" id="error" data-label="inline error">
<div *ngIf="uname.errors?.required && validationForm.submitted && !isValidFormSubmitted">
<small class="error">{{'BUSINESS-NUMBER-PAGE.VAL-MESSAGE-REQ' | translate}}</small>
</div>
<div *ngIf="uname.errors?.pattern && !isValidFormSubmitted">
<small class="error">{{'BUSINESS-NUMBER-PAGE.VAL-MESSAGE-DIGIT' | translate}} </small>
</div>
</div>
</div>
</div>
为每个按钮创建一个ButtonGroup bgroup = new ButtonGroup();
bgroup.add(getRdbtnAttackMode());
bgroup.add(getRdbtnDefensiveMode());
,您会很好