Ho eseguito pari pari questo esempio:
http://download.oracle.com/javase/tu...settingup.html
ecco il codice del CelsiusConverterGUI.java generato da NetBeans 6.9.1
codice:/* * CelsiusConverterGUI.java * * Created on May 17, 2011, 1:03:29 PM */ package learn; /** * * @author neorf */ 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("Celsius Converter's neorf"); 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"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(layout.createSequentialGroup() .add(tempTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(celsiusLabel)) .add(layout.createSequentialGroup() .add(convertButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(fahrenheitLabel))) .addContainerGap(49, Short.MAX_VALUE)) ); layout.linkSize(new java.awt.Component[] {convertButton, tempTextField}, org.jdesktop.layout.GroupLayout.HORIZONTAL); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(tempTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(celsiusLabel)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(convertButton) .add(fahrenheitLabel)) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); 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) { // TODO add your handling code here: 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[]) { java.awt.EventQueue.invokeLater(new Runnable() { 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 }

Rispondi quotando