토글 버튼에 대한 기본적인 질문이 있습니다. 다음 코드는 원하는 결과를 생성하지만 불리언 값이 반대로 보입니다. 코드가 프로그래밍 방식으로 올바 릅니까? 당신이 당신의 JRadioBuitton
년대에 어떤 actionCommand
을 설정하지도토글 버튼 전환
public class ToggleButton extends javax.swing.JFrame {
public ToggleButton() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
one = new javax.swing.JRadioButton();
all = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
one.setText("one channel");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});
all.setText("all channel");
all.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
allActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(all)
.addComponent(one))
.addContainerGap(122, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(one)
.addGap(18, 18, 18)
.addComponent(all)
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void allActionPerformed(java.awt.event.ActionEvent evt) {
if("all".equals(evt.getActionCommand())){
all.setSelected(false);
one.setSelected(true);
}else {
one.setSelected(false);
all.setSelected(true);
}
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
if("one".equals(evt.getActionCommand())){
one.setSelected(false);
all.setSelected(true);
}else{
all.setSelected(false);
one.setSelected(true);
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ToggleButton().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JRadioButton all;
private javax.swing.JRadioButton one;
// End of variables declaration
}
GUI 빌더를 사용하는 경우 올바른지 확인하십시오. –
대부분의 코드가 IDE에서 생성되었으므로 하나의 채널을 클릭하면 allActionPerformed 및 oneActionPerformed 이벤트 처리기가 혼란 스럽습니다. oneActionPerformed에서 one.setSelected를 true로 설정하면 안됩니까? 그 반대의 경우도 마찬가지입니까? – user1955475
당신이 원하는 바에 달려 있습니다. 결과물은 무엇입니까? 틀릴 것입니다. 더 빨리 도움을 받으려면 SSCCE –