Results 1 to 6 of 6
  1. #1
    earnest is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default how do i use jframe button to send input selection

    i am trying to send selection from jframe button and get the selection for the program. What am i doing wrong?

    Public class Main {
    private static enum Command {
    F, // fight
    R, // rest
    B, // buy weapon
    C, // status
    Q, // quit
    UNDEFINED;
    }

    public static void main(String... args) throws IOException {


    // create the opponents
    final Opponent goodman = new Opponent("Goodman", 50, false);
    final Opponent monster = new Opponent("Monster", 50, true);

    // initiate the fight
    final Fight fight = new Fight(goodman, monster);

    // input switch statement
    // loop while both are alive
    while (fight.isOngoing()) {


    JFrame frame = new JFrame("Goodman Vs. Monster - Round 1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    frame.setLayout(new GridLayout(1,5));
    frame.add(new JButton("Go out and fight"));
    frame.add(new JButton("Buy Weapon"));
    frame.add(new JButton("R: Rest"));
    frame.add(new JButton("C: Check Stats"));
    frame.add(new JButton("Q: Quit"));

    frame.pack();
    frame.setVisible(true);

    switch (readCommand()) {
    case F:
    System.out.println("f: Continue the fight");
    fight.fight();
    if (!fight.isOngoing()) {
    fight.end();
    }
    break;
    case R:
    fight.rest();

    break;

    case C:
    System.out.println("c: Fight-Status:");
    fight.getStats();
    break;

    case B:
    System.out.println("b: Buy Weapon:");
    System.out.println(Weapon.getBuyableWeapons());
    Weapon weapon = readWeapon();
    fight.buy(goodman, weapon);
    break;

    case Q:
    System.out.println("q: You quit: Game over");
    System.exit(0);
    break;

    default:
    System.out.println("--> Invalid selection");
    break;
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I suggest you read up on how to use action listeners, especially for buttons. It's not too challenging. Something like this is the general form of adding with an anonymous inner class.

    Java Code:
    public JButton button = new JButton("HI");
    button.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        System.out.println("Hello");
      }
    });
    When this button is clicked it will print "Hello" to the terminal/console.

    Here is a link to the sun tutorials
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  3. #3
    earnest is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    i read this, but i am not seeing how you will pass the input to the rest of the script. i do not want to print, I want to send the selection as input. how do i do that?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    While my example shows HOW to make a button do something, actually doing it the way you want is the challenge you will benefit from learning.

    What do you mean by pass input? Do you want to use a text field and capture input? Or call a method?

  5. #5
    earnest is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    if one click the R: Rest button, then the fighter rest; if the c button is clicked, then a check is made. This must be passed on to the switch statement for this to occur. How do i send the input / selection / button clicked to the switch statement

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I think a better approach may be to have methods which do one thing and then when you click the correct button you will call the correct method based on the button called. Since you already have the methods defined you can just call the method when clicked. You can do this in two ways, using e.getSource() to figure out which button is clicked, or create an action listener for each button and attach them to the correct buttons.

    Java Code:
    class XButton implements ActionListener{
      public void actionPerformed(ActionEvent e){
        System.out.println("X button pressed");
      }
    }
    
    class YButton implements ActionListener{
      public void actionPerformed(ActionEvent e){
        System.out.println("Y Button pressed");
      }
    }
    
    public class MyFrame implements JFrame{
      public MyFrame(){
        setLayout(new BorderLayout());
        JButton x = new JButton("X");
        x.addActionListener(new XButton());
        JButton y = new JButton("Y");
        y.addActionListener(new YButton());
        add(x, BorderLayout.SOUTH);
        add(y, BorderLayout.NORTH);
        setVisible(true);
        pack();
      }
    }

Similar Threads

  1. Disable Maximize button in JFrame?
    By Manish87 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 04:39 PM
  2. How to open new JFrame with clicking on button?
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-14-2011, 11:36 AM
  3. How to send a list as a input for a oracle procedure
    By anilkumar_vist in forum Advanced Java
    Replies: 4
    Last Post: 11-24-2010, 09:51 AM
  4. Replies: 10
    Last Post: 06-06-2010, 07:49 PM
  5. Replies: 0
    Last Post: 09-21-2009, 12:33 PM

Tags for this Thread

Posting Permissions

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