Java 摆动按钮
我有一个关于切换按钮的基本问题:下面的代码确实生成了所需的结果,但是,看起来布尔值应该是相反的。代码在编程上是否正确Java 摆动按钮,java,swing,Java,Swing,我有一个关于切换按钮的基本问题:下面的代码确实生成了所需的结果,但是,看起来布尔值应该是相反的。代码在编程上是否正确 public class ToggleButton extends javax.swing.JFrame { public ToggleButton() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="co
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
}
public类ToggleButton扩展了javax.swing.JFrame{
公共切换按钮(){
初始化组件();
}
@抑制警告(“未选中”)
//
私有组件(){
one=newjavax.swing.JRadioButton();
all=newjavax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
一个.setText(“一个频道”);
addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行一项行动(evt);
}
});
all.setText(“所有频道”);
all.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的所有操作(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33,33,33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(全部)
.addComponent(一)
.addContainerGap(122,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30,30,30)
.addComponent(一个)
.addGap(18,18,18)
.addComponent(全部)
.addContainerGap(29,简称最大值))
);
包装();
}//
private void allActionPerformed(java.awt.event.ActionEvent evt){
if(“all”.equals(evt.getActionCommand())){
全部。已选择(错误);
一、选择(正确);
}否则{
一、选举结果(假);
全部。选择(正确);
}
}
私有void oneActionPerformed(java.awt.event.ActionEvent evt){
if(“one”.equals(evt.getActionCommand())){
一、选举结果(假);
全部。选择(正确);
}否则{
全部。已选择(错误);
一、选择(正确);
}
}
公共静态void main(字符串参数[]){
//
/*
*如果Nimbus(在JavaSE6中引入)不可用,请使用
*默认外观。有关详细信息,请参阅
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
invokeLater(new Runnable()){
公开募捐{
新建ToggleButton().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JRadioButton all;
private javax.swing.JRadioButton one;
//变量结束声明
}
您的测试if(“all”.equals(evt.getActionCommand())
和if(“one”.equals(evt.getActionCommand()))
总是false
,因为您从未在jradiobuiton
上设置任何actionCommand
。现在,我真的不明白为什么要在UI已经设置单选按钮的时候设置它的值。您不必重新修改按钮的状态
您可能缺少的是一个(并将两个
JRadioButton
添加到该组),这样当选择一个JRadioButton时,另一个会自动取消选择。如果您使用的是GUI生成器,则必须是正确的。大多数代码是由IDE生成的,AllActionExecuted和OneActionExecuted事件处理程序令人困惑,如果单击一个频道,oneActionPerformed下的one.setSelected不应该为true吗?反之亦然?这取决于您想要实现什么。您想要输出什么?哪一个应该为false?为了更快地获得更好的帮助,发布SSCCE+1,我没有注意到这些按钮是否属于同一组。这就是为什么它们都可以被选择的原因。