Results 1 to 5 of 5
- 11-18-2011, 12:18 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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?
Java 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); } } }
- 11-18-2011, 12:24 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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.
- 11-18-2011, 01:23 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
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.
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?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
Similar Threads
-
Trouble with Object.equals()
By summersab in forum New To JavaReplies: 3Last Post: 06-14-2011, 01:07 AM -
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
Trouble creating object that includes array
By Desdenova in forum New To JavaReplies: 7Last Post: 05-18-2010, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks