如果和其他GUI。在其他所有的情况下只有一个输出

时间:2015-03-17 16:56:14

标签: java swing user-interface awt

这是我的问题....建立GUI

我还有另外两个名为DistanceConverter和VolumeConverter的类。 我将他们的动作命令设置为名为chkDistance和chkVolume的String,现在我可以使用if和else ...

但问题出在if和else输出只有一个..唯一输出的是chkVolume。每当我选择chkDistance时,输出仍然是chkVolume ..

这是我的代码....     import java.awt.Checkbox;     import javax.swing.JOptionPane;     import javax.swing.JRadioButton;

public class Converter extends javax.swing.JFrame {
String chkDistance;
String chkVolume;

public Converter() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    buttonGroup = new javax.swing.ButtonGroup();
    jLabel1 = new javax.swing.JLabel();
    myButton1 = new javax.swing.JRadioButton();
    MyButton2 = new javax.swing.JRadioButton();
    mainButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 0, 204));
    setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    jLabel1.setBackground(new java.awt.Color(153, 153, 255));
    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(0, 0, 102));
    jLabel1.setText("CONVERTER");

    buttonGroup.add(myButton1);
    myButton1.setText("Distance");
    myButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            myButton1ActionPerformed(evt);
        }
    });

    MyButton2.setBackground(new java.awt.Color(240, 240, 241));
    buttonGroup.add(MyButton2);
    MyButton2.setText("Volume");
    MyButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            MyButton2ActionPerformed(evt);
        }
    });

    mainButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    mainButton.setText("OK");
    mainButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            mainButtonActionPerformed(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(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addGroup(layout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(myButton1)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(mainButton))
                        .addComponent(MyButton2))))
            .addContainerGap(26, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(26, 26, 26)
            .addComponent(MyButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(myButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
            .addComponent(mainButton)
            .addGap(35, 35, 35))
    );

    pack();
}// </editor-fold>                        

private void myButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        myButton1= new JRadioButton();
    myButton1.setActionCommand(chkDistance);
}                                         

private void MyButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                          

    MyButton2= new JRadioButton();
    MyButton2.setActionCommand(chkVolume);

}                                         

private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String command = buttonGroup.getSelection().getActionCommand();

这是我的if和else ..........只有if语句输出中的变量,每当我选择chkDistance之类的其他选项时,输出也在卷中..

    if (command == chkVolume){
        VolumeConverter volume = new VolumeConverter();
        volume.setVisible(true);
    }
     else  if(command == chkDistance){
       DistanceConverter distance = new DistanceConverter();
       distance.setVisible(true);
    }
     else{
         return;
     }
}                                          

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<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(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Converter().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JRadioButton MyButton2;
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton mainButton;
private javax.swing.JRadioButton myButton1;
// End of variables declaration                   

}

但是当我更改他们的位置示例时... if语句具有chkDistance的值..即使我选择音量,将打开的输出也是距离

     if(command == chkDistance){
       DistanceConverter distance = new DistanceConverter();
       distance.setVisible(true);
    }
else if (command == chkVolume){
        VolumeConverter volume = new VolumeConverter();
        volume.setVisible(true);
    }
     else{
         return;
     }

1 个答案:

答案 0 :(得分:1)

您尚未初始化chkDistancechkVolume。所以他们都是null。这意味着您的actionCommands实际上都绑定到null。这意味着if (command == chkVolume){总是返回true。

将它们初始化为不同的值:

String chkDistance = "checkDistance";
String chkVolume = "checkVolume";