Friday, May 6, 2011

using buttons in netbeans gui frame

Ok, so I'm pretty noob at JAVA, and programming in general really. I'm trying to make a rock, paper, scissors app that with run with a GUI interface. What I'm trying is the simplest thing I thought of, press a button for your choice (r, p, or s) and that will make a variable used in the rest of the program to compare to the computer's choice, finding the winner, etc. I already have the program worked out, the problem is the GUI stuff. I'm using netbeans and a JFrame, which is completely new to me. I have a lot of questions, and maybe there is a nice tutorial out there for me someone could link to, but using the official netbeans site hasn't helped much so far. Anyways:

  1. I'm not sure where to put my main method code that would be in the text program, make a new class, or put it in the main method of the frame I use.

  2. Because of that, the reason I can't even compile is that the main function is trying to use the variable I want to make in the button. The whole order of operation is unknown to me I guess. I looked at the netbeans examples and I think I could make this work by doing something a little more complex, but it seems unnecessary?

From stackoverflow
  • You want to find some articles and tutorials on Swing.

    Here's a little bit of code to help you:

    // RockPaperScissors is the JFrame as well as the KeyListener.
    // You could make another class to handle the key presses instead by doing:
    // addKeyListener(new myOtherClass());
    // So long as myOtherClass implements KeyListener
    class RockPaperScissors extends JFrame implements KeyListener {
      public RockPaperScissors() {
        // Listen for any key presses.
        addKeyListener(this);
      }
    
      public void keyPressed(KeyEvent e) {
        // Figure out which key was pressed based on it's code.
        // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html
        switch (e.getKeyCode()) {
          case e.VK_R:
            // Rock
          break;
          case e.VK_P:
            // Paper
          break;
          case e.VK_S:
            // Scissors
          break;
        }
      }
    
      // You can use these if you want, but we don't care about them.
      public void keyReleased(KeyEvent e) { }
      public void keyTyped(KeyEvent e) { }
    }
    
    void main() {
      // Set up the GUI (which is just a JFrame!)
      new RockPaperScissors();
      // The main program can exit because the JFrame runs on it's own thread.
    }
    
  • Here's a good place to start learning about Swing. I would caution you that using NetBeans is great if you are trying to crank out a big GUI quickly, but may cause more problems than it solves if you are not already familiar with how Swing works.

    A few tips from what I have learned on my projects:

    • Think through what the underlying model of the program will be before starting to write the GUI. For example, perhaps you would use a game class like this pseudocode:
    Start a new game
       Get the player's choice
       Randomly generate a choice for the computer
       Compare the choices
          if the player's choice is stronger
             player wins
          else computer wins
       return the winner
    • Using the Model-View-Controller pattern is a good place to start when you have a GUI to build. This ensures that there is separation between the components of your program; if you ever need to change the view or the model, you don't break the other components.

    • What this generally means in practice for Java is that you wrap the view and controller together, generally extending a JFrame as qpingu pointed out. The JFrame will need a reference to your model class and the panels that contain the various controls.

0 comments:

Post a Comment