Having Trouble with a GUI Object class
Hey all, I'm new to Java and am attempting to create a connect four game. Cliché I know, but it had a few interesting problems that I wanted to learn to solve dealing with the swing class and also work through multidimensional arrays. Right now though i can't get my gui object class to work the proper way... was wondering if you could help?
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;
public class PlayConnect4 {
public static void main(String[] args) {
intializeGame();
gameTypeGUI a = new gameTypeGUI();
}
private static void intializeGame() {
String rows = JOptionPane.showInputDialog(null, "Input the "
+ "number of rows");
String columns = JOptionPane.showInputDialog(null, "Input the "
+ "number of columns");
Scanner row = new Scanner(rows);
Scanner column = new Scanner(columns);
int r = row.nextInt();
int c = column.nextInt();
Connect4Board board = new Connect4Board(r, c);
Connect4Player p1 = new Connect4Player(0, "Player 1");
Connect4Player p2 = new Connect4Player(0, "Player 2");
}
public class gameTypeGUI {
JButton player, computer, remotePlayer;
JFrame buttons;
public gameTypeGUI() {
player = new JButton("Play vs. another human");
computer = new JButton("Play vs. Computer");
remotePlayer = new JButton("Play vs. a reomte player");
buttons = new JFrame();
buttons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons.setLayout(
new BorderLayout(25, 10));
buttons.setLocation(
250, 250);
buttons.add(player, BorderLayout.WEST);
buttons.add(remotePlayer, BorderLayout.CENTER);
buttons.add(computer, BorderLayout.EAST);
buttons.add(new JLabel("What type of game do you want to "
+ "play?"), BorderLayout.NORTH);
buttons.pack();
buttons.setVisible(true);
}
}
}
Re: Having Trouble with a GUI Object class
Oh, and the problem i'm getting is in line 12, I get the error, "non-static variable this cannot be referenced from a static context."
Re: Having Trouble with a GUI Object class
Why is the class gameTypeGUI nested inside of the PlayConnect4 class? Sure, I sometimes nest classes, but only for specific purposes, and rarely is the inner class a public class. I suggest you don't add the additional complexity and separate the two classes into their own files.
Also, when posting error messages, post the entire message, not a small snip of it or a paraphrased bit of it.
Re: Having Trouble with a GUI Object class
The reason I have the class nested is because I need the returns from the actionListener's that I want to implement on each button. I need those returns to keep running in the main code, and i couldn't get it to return properly from a separate java class, so i nested it instead.
here is the whole error.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Connect4Board.<init>(Connect4Board.java:67)
at PlayConnect4.intializeGame(PlayConnect4.java:34)
at PlayConnect4.main(PlayConnect4.java:20)
Java Result: 1
Re: Having Trouble with a GUI Object class
Quote:
Originally Posted by
jrdarkness
The reason I have the class nested is because I need the returns from the actionListener's that I want to implement on each button. I need those returns to keep running in the main code, and i couldn't get it to return properly from a separate java class, so i nested it instead.
That's not a reason to nest classes, not at all. Perhaps it would be best for you to show your non-nested class and the specific problem you were having with it, since you should be using separate classes and files here.
Quote:
here is the whole error.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Connect4Board.<init>(Connect4Board.java:67)
at PlayConnect4.intializeGame(PlayConnect4.java:34)
at PlayConnect4.main(PlayConnect4.java:20)
Java Result: 1
Huh?? This is nothing at all like the "non-static variable this cannot be referenced from a static context." that you mentioned above. Please clarify things -- just what is your real error?