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

JAVA-JFrame-无法从静态上下文引用非静态方法,java,swing,methods,static,jframe,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 ne

这个问题似乎很常见,但我找不到任何解决问题的方法。 嗯,也许如果我不那么擅长编程,我会理解一切并解决问题

不管怎样,代码如下:

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                 
}
很抱歉使用波兰语,但我希望变量的名称无论如何都不那么重要

好吧,交易是这样的:当我试图引用dajnasrodek时;从main方法中,我得到了标题中所示的错误-无法从静态上下文引用非静态方法。我试图将dajnasrodek方法更改为static,但由于出现了一些其他错误,因此没有太大帮助


有什么好主意吗??任何形式的帮助都将不胜感激!!提前多谢

不要将代码放在main方法中。相反,我将它放入类的类构造函数中,该类包含主方法,在您的例子中是Kalkulator。然后在main方法中实例化Kalkulator。Kalkulator不会是静态的,因此您不会收到此消息。如果您想知道为什么会出现此错误,这是因为main方法被声明为static,并且错误表明静态对象无法访问非静态的对象。签出。

那么错误在哪一行?我会尝试自己编译它,但我不适合你使用的任何字符集,我也不会为了回答这个问题而乱弄我的设置。在我键入答案之前,它可能会关闭。这正是我所期望的。好吧,我想引用主方法中的dajnasrodek方法,但我甚至不知道该放在哪里。。。你至少知道我的意思吗??因为我是一个编程新手,所以这一点我看起来很不清楚。或者,简单地说,dajnasrodek方法意味着完全居中屏幕,我想做的就是让程序在启动后自动居中屏幕。你知道怎么做吗??dajnasrodek方法是我想要运行的算法,但目前为止运气不佳。不,您似乎在描述一个编译错误。但我看到的只是应该编译的代码。如果您想要帮助解决编译错误,您需要向我们展示有错误的代码,而不是其他没有错误的代码。