Problem med trafik mellan klasser (Java)
Detta är i princip första gången som jag använder mer än en klass i ett Java-projekt och jag har ingen aning om varför det blir fel. Felmeddelandet lyder som följande:
non-static variable difficulty cannot be referenced from a static context
non-static variable cont cannot be referenced from a static context
package Numerical;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menu extends JFrame implements ActionListener {
private JButton startGame;
private JButton options;
private JButton exitGame;
private JButton easy;
private JButton medium;
private JButton hard;
JPanel jP = new JPanel(new GridLayout(3,1));
JPanel jP2 = new JPanel(new GridLayout(3,1));
boolean optionsActive = false;
public int difficulty = 2;
public Container cont = getContentPane();
public Menu() {
super("Numerical");
createAndShowGUI();
}
public void addToPane(Container c) {
c.setLayout(null);
c.add(jP);
c.add(jP2);
startGame = addButtonToContainer("Start Game", jP);
options = addButtonToContainer("Options", jP);
exitGame = addButtonToContainer("Exit Game", jP);
easy = addButtonToContainer("Easy", jP2);
medium = addButtonToContainer("Medium", jP2);
hard = addButtonToContainer("Hard", jP2);
jP.setBounds(5,5,150,150);
jP2.setBounds(5,175,150,150);
}
public JButton addButtonToContainer (String text, JPanel p) {
JButton button = new JButton(text);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
p.add(button);
button.addActionListener(this);
return button;
}
public void createAndShowGUI() {
addToPane(cont);
setVisible(true);
easy.setVisible(false);
medium.setVisible(false);
hard.setVisible(false);
repaint();
setSize(175,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startGame) {
Numerical num = new Numerical();
}
if (e.getSource() == options) {
if (optionsActive==false) {
easy.setVisible(true);
medium.setVisible(true);
hard.setVisible(true);
optionsActive = true;
}
else {
easy.setVisible(false);
medium.setVisible(false);
hard.setVisible(false);
optionsActive = false;
}
}
if (e.getSource() == exitGame) {
System.exit(0);
}
if (e.getSource() == easy) {
difficulty = 1;
}
if (e.getSource() == medium) {
difficulty = 2;
}
if (e.getSource() == hard) {
difficulty = 3;
}
}
public static void main (String[] args) {
Menu m = new Menu();
}
}
package Numerical;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Numerical extends Menu {
private int difficulty;
private JButton[] buttonsX;
private JButton[] buttonsY;
JPanel jP3;
public Numerical() {
Menu menu1 = new Menu();
difficulty = Menu.difficulty;
createAndShowGUI();
}
public void addToPane(Container c) {
jP3 = new JPanel(new GridLayout(difficulty*5, difficulty*5));
c.add(jP3);
buttonsX = new JButton[difficulty*5];
buttonsY = new JButton[difficulty*5];
for (int i = 0; i < difficulty*5; i++) {
buttonsX[i] = addButtonToContainer("", jP3);
buttonsY[i] = addButtonToContainer("", jP3);
}
int[] wutt = {25,50,100};
for (int i = 0; i < 3; i++) {
if(i==difficulty) {
jP3.setBounds(wutt[i],wutt[i],wutt[i],wutt[i]);
}
}
}
public void createAndShowGUI() {
addToPane(Menu.cont);
}
}
EDIT: Hittade felet (menu1 istället för Menu *duh*) men det är något annat som gör så att inte programmet vill köras... Kompilatorn hittar inga fel men när jag kör (trycker på start i Menu) så blir det ungefär 1 sida med felmeddelanden i kommandofönstret...
Programmerande får mig att tänka friskt och klart! Mitt nya liv blir underbart!