Marcus
Marcus

Reputation:

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?

Upvotes: 1

Views: 5485

Answers (3)

Jared
Jared

Reputation: 109

The place to start putting in code like you are used to is in the source package directory usually named what you called you new project: YourFileName.java

The key is to look for the name you added in the set up in the names of files in the new project.

Once in, there are some methods that start the application for you. And at the bottom is where the main() resides which you and I are most familiar with :) .

    ...

    @Override 
    protected void configureWindow(java.awt.Window root) {
    }

    /** 
    * A convenient static getter for the application instance.
    * @return the instance of DesktopApplication1
    */


    public static DesktopApplication1 getApplication() {
        return Application.getInstance(DesktopApplication1.class);
    }

    /**
    * Main method launching the application.
    */

    public static void main(String[] args) {
    launch(DesktopApplication1.class, args);

    }
}

Personally, I prefer desktop application because it starts out with a lot of pre-generated code for you.

Upvotes: 1

Feanor
Feanor

Reputation: 2725

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.

Upvotes: 1

qpingu
qpingu

Reputation: 960

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

Upvotes: 0

Related Questions