3 Replies - 661 Views - Last Post: 31 December 2011 - 01:15 AM Rate Topic: -----

#1 vl_dex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-December 11

non-static method cannot be referenced from a static context

Posted 30 December 2011 - 08:25 PM

Hello! I have a error : "non-static method cannot be referenced from a static context"


My main:
package game.snake;

public class SnakeApp { 
    public static void main(String[] args) {           
SnakeWindows.startGame();    
    }   
}


My SnakeWindows

package game.snake;

import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
 

/**
 *
 * @author Vlad
 */
public class SnakeWindows {

    public   int difficulty = 1;
    public   int map;
    public   int speedofsnake = 1;
    public   String dif;

    public void startGame() {


        JFrame myWindow = new JFrame("Snake menu");//Menu

        mywindow.setLayout(null);
        mywindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mywindow.setSize(300, 690);
        mywindow.setVisible(true);        
       
        final JButton Kids = new JButton("Kids");        
        Kids.setLocation(50, 50);
        Kids.setSize(200, 40);
        mywindow.add(Kids);
        
        final JButton Standart = new JButton("Standart");
        Standart.setLocation(50, 100);
        Standart.setSize(200, 40);
        mywindow.add(Standart);
        
        final JLabel diff = new javax.swing.JLabel();
        diff.setBounds(100, 20, 100, 30);
        diff.setText("Game difficulty");
        mywindow.add(diff);
        
        
        Kids.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(Kids, "Kids difficulty", "Difficulty", JOptionPane.WARNING_MESSAGE);//Popped Up a message
                difficulty = 0; 
            }
        });

        Standart.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(Kids, "Standart difficulty", "Difficulty", JOptionPane.WARNING_MESSAGE);
                difficulty = 1;
            }
        });


        JLabel maps = new javax.swing.JLabel();//New text
        maps.setBounds(130, 150, 160, 30);
        maps.setText("Map");//Set text to Map
        mywindow.add(maps);//Add text to myWindow
        
        final JButton standartmap = new JButton("Standart");
        standartmap.setLocation(50, 180);
        standartmap.setSize(200, 40);
        mywindow.add(standartmap);
        
        final JButton inceptionmap = new JButton("Inception");
        inceptionmap.setLocation(50, 230);
        inceptionmap.setSize(200, 40);
        mywindow.add(inceptionmap);
        mywindow.setResizable(false);

        standartmap.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(standartmap, "Standart map", "Maps", JOptionPane.WARNING_MESSAGE);
                map = 0;
            }
        });
        inceptionmap.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(inceptionmap, "Inception map", "Maps", JOptionPane.WARNING_MESSAGE);
                map = 1;
            }
        });
        

        JLabel speed = new javax.swing.JLabel();
        speed.setBounds(130, 280, 160, 30);
        speed.setText("Speed");
        mywindow.add(speed);
        
        final JButton slowspeed = new JButton("Slow");
        slowspeed.setLocation(50, 310);
        slowspeed.setSize(200, 40);
        mywindow.add(slowspeed);
        
        final JButton normalspeed = new JButton("Normal");
        normalspeed.setLocation(50, 360);
        normalspeed.setSize(200, 40);
        mywindow.add(normalspeed);
        mywindow.setResizable(false);
        
        final JButton fastspeed = new JButton("Fast");
        fastspeed.setLocation(50, 410);
        fastspeed.setSize(200, 40);
        mywindow.add(fastspeed);
        mywindow.setResizable(false);

        slowspeed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(slowspeed, "Slow speed", "Speed", JOptionPane.WARNING_MESSAGE);
                speedofsnake = 0;
            }
        });

        normalspeed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(normalspeed, "Normal speed", "Speed", JOptionPane.WARNING_MESSAGE);
                speedofsnake = 1;
            }
        });

        fastspeed.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(fastspeed, "Fast speed", "Speed", JOptionPane.WARNING_MESSAGE);
                speedofsnake = 2;
            }
        });

        final JButton highscore = new JButton("Highscore");
        highscore.setLocation(50, 540);
        highscore.setSize(200, 40);
        mywindow.add(highscore);
        mywindow.setResizable(false);

        highscore.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                game.highscores.HighscoreManager hm = new game.highscores.HighscoreManager();
                JOptionPane.showMessageDialog(highscore, hm.getHighscoreString(), "Highscores", JOptionPane.WARNING_MESSAGE);//Show highscores from HighscoreManager as Warning Message
}
        });
        final JButton GO = new JButton("Start game");
        GO.setLocation(50, 490);
        GO.setSize(200, 40);
        mywindow.add(GO);
        GO.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {//Start the game
              SnakeDialog.snakeGame();//Zavolame SnakeDialog - START GAME!
            }
        });



        final JButton exit = new JButton("Exit");//We HAVE Exit-button (if you gaming WorldOfWarcraft, WOW has you, and no exit from Matrix)
        exit.setLocation(50, 590);
        exit.setSize(200, 40);
        mywindow.add(exit);
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(exit, "Goodbye!", "OK", JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        });
    }    
     public   int getMap() {
        return map;
    }
     public   int getSpeed(){
         return speedofsnake;
     }
     public   int getDiff(){
       return difficulty;
     }
}




What wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: non-static method cannot be referenced from a static context

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,145
  • Joined: 06-March 08

Re: non-static method cannot be referenced from a static context

Posted 30 December 2011 - 08:36 PM

startGame() is a method that should be applied to an instance of SnakeWindows
You will have to create a SnakeWindows object first

so something like

SnakeWindows sw = new SnakeWindows();
sw.startGame);
Was This Post Helpful? 1
  • +
  • -

#3 vl_dex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-December 11

Re: non-static method cannot be referenced from a static context

Posted 30 December 2011 - 10:59 PM

View Postpbl, on 30 December 2011 - 08:36 PM, said:

startGame() is a method that should be applied to an instance of SnakeWindows
You will have to create a SnakeWindows object first

so something like

SnakeWindows sw = new SnakeWindows();
sw.startGame);

Thank you!!! Thanks!!!! :sorcerer:
Was This Post Helpful? 0
  • +
  • -

#4 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 83
  • View blog
  • Posts: 505
  • Joined: 14-January 09

Re: non-static method cannot be referenced from a static context

Posted 31 December 2011 - 01:15 AM

new SnakeWindows().startGame();

Would work as well, seeing as your main doesn't seem to need a reference to continue working on.

This post has been edited by Ghlavac: 31 December 2011 - 01:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1