NetBeans 6.1 GUI Builder

di il
3 risposte

NetBeans 6.1 GUI Builder

Salve a tutti, mi sto addentrando nella programmazione in java da poco tempo. Siccome provengo da alcune esperienze con il c#. ho ritenuto molto interessante utilizzare il GUI Builder di NetBeans 6.1 per progettare le mia applicazioni desktop in java. Ora però ho un problema: ho creato alcuni bottoni (sempre con il GUI BUILDER) e ho la necessità di renderli invisibili in determinate condizioni. Però quando faccio nomebottone.setVisible(false) questo mi genera un errore, ossià "non-static variable nomebottone cannot be referenced from a static context". Purtroppo non riesco andare avanti!
Sperò che qualcuno possa darmi una mano e grazie in anticipo!

3 Risposte

  • Re: NetBeans 6.1 GUI Builder

    Ciao,
    posta il codice completo....
  • Re: NetBeans 6.1 GUI Builder

    Package interesse;

    /**
    *
    * @author NetRider
    */
    public class NewJFrame extends javax.swing.JFrame {

    double Capitale;
    double Tasso;
    double Interesse;
    int Tempo;

    public NewJFrame() {
    initComponents();
    }

    // QUESTO E' IL CODICE RELATIVO ALLA GRAFICA GENERATO AUTOMATICAMENTE DAL GUI BUILDER

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

    buttonGroup1 = new javax.swing.ButtonGroup();
    buttonGroup2 = new javax.swing.ButtonGroup();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLayeredPane1 = new javax.swing.JLayeredPane();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jRadioButton3 = new javax.swing.JRadioButton();
    jLayeredPane2 = new javax.swing.JLayeredPane();
    jRadioButton5 = new javax.swing.JRadioButton();
    jRadioButton4 = new javax.swing.JRadioButton();
    jCheckBox1 = new javax.swing.JCheckBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Calcolatore Interessi");
    setResizable(false);

    jButton1.setText("Interesse");

    jButton2.setText("Capitale");

    jButton3.setText("Tasso");

    jButton4.setText("Tempo");

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel1.setText("Cosa vuoi calcolare?");

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel2.setText("Capitale");

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel3.setText("Tasso");

    jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel4.setText("Tempo");

    jLabel5.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    jLabel5.setText("(");

    jLabel6.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    jLabel6.setText(")");

    jLabel7.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    jLabel7.setText("/");

    jLabel8.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    jLabel8.setText("Tempo");

    jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Scegli come è espresso il tempo"));

    buttonGroup1.add(jRadioButton1);
    jRadioButton1.setText("Giorni");
    jRadioButton1.setBounds(10, 20, 60, 23);
    jLayeredPane1.add(jRadioButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setText("Mesi");
    jRadioButton2.setBounds(10, 50, 60, 23);
    jLayeredPane1.add(jRadioButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    buttonGroup1.add(jRadioButton3);
    jRadioButton3.setText("Anni");
    jRadioButton3.setBounds(10, 80, 60, 23);
    jLayeredPane1.add(jRadioButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Scegli il calendario"));

    buttonGroup2.add(jRadioButton5);
    jRadioButton5.setText("Commerciale");
    jRadioButton5.setBounds(10, 60, 110, 23);
    jLayeredPane2.add(jRadioButton5, javax.swing.JLayeredPane.DEFAULT_LAYER);

    buttonGroup2.add(jRadioButton4);
    jRadioButton4.setText("Civile");
    jRadioButton4.setBounds(10, 30, 60, 23);
    jLayeredPane2.add(jRadioButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jCheckBox1.setText("Rileva anno bisestile");
    jCheckBox1.setBounds(10, 90, 150, 23);
    jLayeredPane2.add(jCheckBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    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)
    .addComponent(jLabel1)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel5)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel2))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jLabel6)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jLabel7)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jLabel8))
    .addGroup(layout.createSequentialGroup()
    .addGap(54, 54, 54)
    .addComponent(jLabel4))))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
    .addGap(18, 18, 18)
    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
    .addGap(18, 18, 18)
    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
    .addGap(14, 14, 14))
    .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(16, 16, 16)
    .addComponent(jLabel1)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton1)
    .addComponent(jButton3)
    .addComponent(jButton2)
    .addComponent(jButton4))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLayeredPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel4)
    .addComponent(jLabel3)
    .addComponent(jLabel2))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel5)
    .addComponent(jLabel6)
    .addComponent(jLabel7)
    .addComponent(jLabel8))
    .addGap(37, 37, 37))
    );

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

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new NewJFrame().setVisible(true);

    }
    });
    jButton1.setVisible(false); //QUI MI DA L'ERRORE CHE NON RIESCO A RISOLVERE
    }


    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    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.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration
    }
  • Re: NetBeans 6.1 GUI Builder

    Probabilmente hai già risolto perchè il post non è recentissimo...
    Il codice con quella identazione è una porcata: come speri che qualcuno possa perdere tempo ad aiutarti?
    Ad ogni modo...
    L'errore che ti viene segnalato dal compilatore non mi pare così astruso. Non puoi invocare il metodo setVisible(), che non è statico, da un contesto statico (la classe main()).
    Puoi provare a fare almeno due cose:
    1. invochi il metodo da un'altra parte anzichè nel main (per es. lo ficchi nel costruttore anche se fa schifo come cosa).
    2. togli il modificatore static alla classe main.
    Funzionerà? Prova...
    Ciao
Devi accedere o registrarti per scrivere nel forum
3 risposte