无法从静态上下文引用JAVA-JFrame-非静态方法

时间:2014-01-04 22:12:44

标签: java swing methods static jframe

此问题似乎很常见,但我无法找到解决问题的方法。 好吧,也许如果我不是那么难以编程,我会理解一切并解决问题。

无论如何,这是代码:

package kalk;

import java.awt.Toolkit;
import javax.swing.JOptionPane;

/**
 *
 * @author BADASS BOSS
 */
public class Kalkulator extends javax.swing.JFrame {

    /**
     * Creates new form Kalkulator
     */
    private double liczba1, liczba2;
    private double wynik=0;
    private int nrdzialania=0;
    private boolean dopierwszej=true;
    private Toolkit glownytoolkit;
    //dzialania
    //1-dodawanie
    //2-odejmowanie
    //3-mnozenie
    //4-dzielenie
    private double pobierzliczbe(String s)
    {
        double temp;        
        temp = 0;
        try
        {
            temp = Double.valueOf(s);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, "FATAL ERROR!!" + e,"Coś się zepsuło!!!!", JOptionPane.ERROR_MESSAGE);
        }
        return temp;
    }
    private void robdzialanie()
    {
        String bufor;
        liczba1 = pobierzliczbe(jTextField1.getText());
        liczba2 = pobierzliczbe(jTextField3.getText());
        bufor = "";
        if (nrdzialania==1) wynik = liczba1 + liczba2;
        else if (nrdzialania==2) wynik = liczba1 - liczba2;
        else if (nrdzialania==3) wynik = liczba1 * liczba2;
        else if (nrdzialania==4)
        {
            if (liczba2==0) bufor="FATAL ERROR!! Nie można dzielić przez zero!!!";
            else wynik = liczba1 / liczba2;
        }
        if (nrdzialania!=0) bufor = String.valueOf(wynik);
        else bufor = "FATAL ERROR!! Argument jest pusty albo niepoprawny!!";
        jTextField4.setText(bufor); 
    }
    public Kalkulator() {
        initComponents();
        glownytoolkit = Toolkit.getDefaultToolkit();
    }
    private void dajnasrodek()
    {
        int x;
        int y;
        int szerokość_ekranu;
        int wysokość_ekranu;
        int wysokość_ramki;
        int szerokość_ramki;
        szerokość_ekranu = glownytoolkit.getScreenSize().width;
        wysokość_ekranu = glownytoolkit.getScreenSize().height;
        szerokość_ramki = this.getSize().width;
        wysokość_ramki = this.getSize().height;
        x = (szerokość_ekranu - szerokość_ramki)/2;
        y = (wysokość_ekranu - wysokość_ramki)/2;
        this.setLocation(x, y);
    }
    /**
     * 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() {


////some unnecessary stuff I guess



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

        /* Create and display the form */
        //Kalkulator gc = new Kalkulator();
        //gc.dajnasrodek();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
          }

        );
        //Kalkulator gc = new Kalkulator();
        //gc.dajnasrodek();
        //ActionEvent klik;
        //jButton18ActionPerformed();    
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
   ///some unnecessary stuff                 
}

对于波兰语言感到抱歉,但我希望变量的名称无论如何都不重要。

好吧,所以这是交易:当我尝试从main方法引用dajnasrodek();时,我得到错误,如标题 - non static method cannot be referenced from a static context所示。我尝试将dajnasrodek()方法更改为静态但由于出现了其他一些错误,因此效果不佳。

任何聪明的想法?任何形式的帮助将非常感谢!!非常感谢提前!!

1 个答案:

答案 0 :(得分:0)

不要将代码放在main方法中。相反,我将它放入类的类构造函数中,它包含main方法,在你的Kalkulator中。然后在main方法中实例化Kalkulator。 Kalkulator不会是静态的,因此您不会收到此消息。如果你想知道为什么你会收到这个错误,那是因为main方法被声明为static,而且因为错误暗示静态的东西不能访问非静态的东西。检查this