Results 1 to 5 of 5
  1. #1
    jrdarkness is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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);
    
        }
      }
    }

  2. #2
    jrdarkness is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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."

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  4. #4
    jrdarkness is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Having Trouble with a GUI Object class

    Quote Originally Posted by jrdarkness View Post
    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.

    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?

Similar Threads

  1. Trouble with Object.equals()
    By summersab in forum New To Java
    Replies: 3
    Last Post: 06-14-2011, 01:07 AM
  2. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  3. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. Trouble creating object that includes array
    By Desdenova in forum New To Java
    Replies: 7
    Last Post: 05-18-2010, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •