2016-04-10 31 views
0

我試圖做一個GUI,以十進制轉換爲二進制,反之亦然,但 上公共靜態無效的主要有表達非法啓動(字符串ARGS [])。我該如何糾正?表達非法的開始,如何解決這一

我的代碼:

public class NewJFrame extends javax.swing.JFrame { 

     /** 
     * Creates new form NewJFrame 
     */ 
     public NewJFrame() { 
      setTitle("Quiz"); 
     } 

     /** 
     * This method is called from within the constructor to initialize the form. 
     * WARNING: Do NOT modify this code. The content of this method is always 
     * regenerated by the Form Editor. 
     */ 
     @SuppressWarnings("unchecked") 
     // <editor-fold defaultstate="collapsed" desc="Generated Code">       
     private void initComponents() { 

      jLabel1 = new javax.swing.JLabel(); 
      jTextField1 = new javax.swing.JTextField(); 
      jButton1 = new javax.swing.JButton(); 
      jTextField2 = new javax.swing.JTextField(); 
      jTextField3 = new javax.swing.JTextField(); 
      jTextField4 = new javax.swing.JTextField(); 
      jLabel2 = new javax.swing.JLabel(); 
      jLabel3 = new javax.swing.JLabel(); 
      jLabel4 = new javax.swing.JLabel(); 
      jComboBox1 = new javax.swing.JComboBox<>(); 
      jTextField5 = new javax.swing.JTextField(); 
      jLabel5 = new javax.swing.JLabel(); 

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

      jLabel1.setText("Desimal"); 

      jTextField1.setText("jTextField1"); 

      jButton1.setText("Konversikan"); 
      jButton1.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent evt) { 
        jButton1ActionPerformed(evt); 
       } 
      }); 

      jTextField2.setText("jTextField2"); 

      jTextField3.setText("jTextField3"); 

      jTextField4.setText("jTextField4"); 

      jLabel2.setText("Biner"); 

      jLabel3.setText("Hexa"); 

      jLabel4.setText("Octal"); 

      jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Desimal", "Biner", "Hexa", "Octal" })); 

      jTextField5.setText("jTextField5"); 

      jLabel5.setText("Input"); 

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
      getContentPane().setLayout(layout); 
      layout.setHorizontalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jLabel5) 
        .addGap(11, 11, 11) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addGap(15, 15, 15) 
          .addComponent(jComboBox1, 0, 116, Short.MAX_VALUE) 
          .addGap(18, 18, 18) 
          .addComponent(jButton1)) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(22, 22, 22) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(jLabel4) 
           .addGroup(layout.createSequentialGroup() 
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
              .addComponent(jLabel1) 
              .addComponent(jLabel2)) 
             .addGroup(layout.createSequentialGroup() 
              .addComponent(jLabel3) 
              .addGap(12, 12, 12))) 
            .addGap(26, 26, 26) 
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
             .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
          .addGap(0, 0, Short.MAX_VALUE))) 
        .addContainerGap()) 
      ); 
      layout.setVerticalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(31, 31, 31) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jButton1) 
         .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel5)) 
        .addGap(53, 53, 53) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel1)) 
        .addGap(18, 18, 18) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel2)) 
        .addGap(24, 24, 24) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel3) 
         .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGap(18, 18, 18) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel4)) 
        .addContainerGap(53, Short.MAX_VALUE)) 
      ); 

      pack(); 
     }// </editor-fold>       
     private void error(){ 
      jLabel2.setText("Input invalid"); 
      jLabel1.setText("Input invalid"); 
      jLabel3.setText("Input invalid"); 
      jLabel4.setText("Input invalid"); 
     } 

     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
      // TODO add your handling code here: 
      if (jComboBox1.getSelectedItem() == "Desimal") { 
       try { 
       int decimal = Integer.parseInt(jTextField5.getText()); 

       String biner = Integer.toBinaryString(decimal); 
       String hex = Integer.toHexString(decimal); 
       String octal = Integer.toOctalString(decimal); 

       jTextField1.setText(Integer.toString(decimal)); 
       jTextField2.setText(biner); 
       jTextField3.setText(hex); 
       jTextField4.setText(octal); 
       } catch (Exception e) { 
        error(); 
       } 
      }else if(jComboBox1.getSelectedItem() == "Biner") 
      try { 
        int decimal = Integer.parseInt(jTextField5.getText(), 2); 
       String biner = jTextField5.getText(); 
       String hex = Integer.toHexString(decimal); 
       String octal = Integer.toOctalString(decimal); 

       jTextField1.setText(Integer.toString(decimal)); 
       jTextField2.setText(biner); 
       jTextField3.setText(hex); 
       jTextField4.setText(octal); 

       } catch (Exception e){ 

       }else if(jComboBox1.getSelectedItem() == "Biner") 
        try { 
       int decimal = Integer.parseInt(jTextField5.getText(), 16); 
       String biner = Integer.toBinaryString(decimal); 
       String octal = Integer.toOctalString(decimal); 

       jTextField1.setText(Integer.toString(decimal)); 
       jTextField2.setText(biner); 
       jTextField3.setText(jTextField5.getText()); 
       jTextField4.setText(octal); 

       } catch (Exception e) { 

       }else if(jComboBox1.getSelectedItem() == "Octal"){ 
       try { 
       int decimal = Integer.parseInt(jTextField5.getText(),8); 

       String hexa = Integer.toHexString(decimal); 
       String biner = Integer.toBinaryString(decimal); 

       jTextField1.setText(Integer.toString(decimal)); 
       jTextField2.setText(biner); 
       jTextField3.setText(hexa); 
       jTextField4.setText(jTextField5.getText()); 

       } catch (Exception e) { 
     }           

     /** 
     * @param args the command line arguments 
     */ 
     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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
      } catch (InstantiationException ex) { 
       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
      } catch (IllegalAccessException ex) { 
       java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
      } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
       java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true); 
       } 
      }); 
     } 

     // Variables declaration - do not modify      
     private javax.swing.JButton jButton1; 
     private javax.swing.JComboBox<String> jComboBox1; 
     private javax.swing.JLabel jLabel1; 
     private javax.swing.JLabel jLabel2; 
     private javax.swing.JLabel jLabel3; 
     private javax.swing.JLabel jLabel4; 
     private javax.swing.JLabel jLabel5; 
     private javax.swing.JTextField jTextField1; 
     private javax.swing.JTextField jTextField2; 
     private javax.swing.JTextField jTextField3; 
     private javax.swing.JTextField jTextField4; 
     private javax.swing.JTextField jTextField5; 
     // End of variables declaration     
    } 
+0

你的代碼是一團糟,但很可能它缺少一個右括號'}'。 –

+0

現在很好,但是當我嘗試運行它時,程序顯示爲空白,爲什麼?我已經設計了gui。 –

+0

現在使用調試器對你來說將非常寶貴。你可以瀏覽代碼並找到它正在破壞的確切位置。 –

回答

2

修正你的語法錯誤。你忘記了catch塊的右括號,以及else之上的右括號(如果在上面)。

+1

這應該是一條評論。 –

+0

爲什麼?這是答案 – Vampire

+0

@TimBiegeleisen是的,我不同意。它回答了這個問題。如果有一個StackOverflow政策說明爲什麼這應該是一個評論,我想看看它爲我自己的信息。與此同時,我提高了這一點。 – nhouser9