2014-01-09 126 views
-1

我想学习Java中的Swing。现在,我在第一个教程,这是一个摄氏/华氏温度转换器。现在,我编写的第一个教程程序运行不正常,我似乎无法找到问题。Swing教程问题

下面是代码:

package learn; 

public class CelsiusConverterGUI extends javax.swing.JFrame { 

    /** 
    * Creates new form CelsiusConverterGUI 
    */ 
    public CelsiusConverterGUI() { 
     initComponents(); 
    } 

    /** 
    * 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() { 

     tempTextField = new javax.swing.JTextField(); 
     celsiusLabel = new javax.swing.JLabel(); 
     convertButton = new javax.swing.JButton(); 
     fahrenheitLabel = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("CelsiusConverter"); 

     tempTextField.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       tempTextFieldActionPerformed(evt); 
      } 
     }); 

     celsiusLabel.setText("Celsius"); 

     convertButton.setText("Convert"); 
     convertButton.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       convertButtonActionPerformed(evt); 
      } 
     }); 

     fahrenheitLabel.setText("Fahrenheit"); 

     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() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(celsiusLabel)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(convertButton) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(fahrenheitLabel))) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]{convertButton, tempTextField}); 

     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(celsiusLabel)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(convertButton) 
        .addComponent(fahrenheitLabel)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]{convertButton, tempTextField}); 

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

    private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    } 

    private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) { 
     //Parse degrees Celsius as a double and convert to Farhenheit. 
     int tempFahr = (int) ((Double.parseDouble(tempTextField.getText())) * 1.8 + 32); 
     fahrenheitLabel.setText(tempFahr + " Fahrenheit"); 
    } 

    /** 
    * @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(CelsiusConverterGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(CelsiusConverterGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(CelsiusConverterGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(CelsiusConverterGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
    //</editor-fold> 

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

    } 

// Variables declaration - do not modify      
    private javax.swing.JLabel celsiusLabel; 
    private javax.swing.JButton convertButton; 
    private javax.swing.JLabel fahrenheitLabel; 
    private javax.swing.JTextField tempTextField; 
// End of variables declaration     
} 

这是输出:

enter image description here

我不明白什么可能出错;我完成了教程告诉我要做的事情!作为参考,这里是教程。

http://docs.oracle.com/javase/tutorial/uiswing/learn/creatinggui.html

谢谢!

+0

这里什么都没有。这是问题吗?你想添加什么? –

+2

而且_“没有正确运行”,_是什么意思? –

+0

该应用应该看起来像这样(向下滚动,它是启动按钮):http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html 相反,它看起来像这样: http://imgur.com/VemmvE5 – newalchemy

回答

2

我已经运行你的代码,它运行良好。 你安装了哪些Java? CelciusGUI

我不知道NetBeans,但会自动生成吗?有些IDE没有,所以你可能不得不明确地构建你的应用程序。

+0

Netbeans IDE 7.4。我将进入运行选项卡并单击“运行项目”。如果它运行良好,我的编辑器设置方式肯定有问题。你有什么建议可能是什么? – newalchemy

+0

在NetBeans 7.4中正确运行。 – trashgod