Results 1 to 3 of 3
  1. #1
    GettinGwap's Avatar
    GettinGwap is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default Trying to make a GUI program

    Hey, I'm trying to make a game but this is my first time dealing with GUI components. I want a main menu on the game with four buttons. The buttons are :Infantry Battle", "Tank Battle", "Mixed Battle", and "Exit Wars". However, I am having trouble making the buttons show up on the frame and they are not working. The book I am looking at isn't making much sense to me. Can anyone help me? Here's my code:

    import java.awt.*;
    import java.awt.event.*;
    import jpb.*;

    public class War
    {
    public static void main(String[] args)
    {
    Frame f = new Frame("Main Menu");
    f.setSize(400, 400);
    f.setVisible(true);
    f.setLocation(300, 300);
    }
    }

    // Frame class
    class ButtonTestFrame extends Frame
    {
    public ButtonTestFrame(String title)
    {
    super(title);
    setLayout(new FlowLayout());
    Button exit = new Button("Exit Wars");
    add(exit);
    exit.addActionListener(new ButtonListener());

    // Attach window listener
    addWindowListener(new ButtonListener());
    }

    // Listener for button
    class ButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {
    System.exit(0);
    }
    }
    }
    :confused:

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I think you need Button exit = new Button(f,"Exit Wars");
    Generally when you define a GUI object you need to tell what it's being attached to, in this case the Frame f.
    also what method is add? I think you need f.add(exit).

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    class ButtonTestFrameRx extends Frame
    {
        public ButtonTestFrameRx(String title)
        {
            super(title);
            setLayout(new FlowLayout());
            Button exit = new Button("Exit Wars");
            add(exit);
            exit.addActionListener(new ButtonListener());
    
            // Attach window listener
            // An ActionListener won't do as an argument here.
            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e) {
                    quit();
                }
            });
        }
    
        // Listener for button
        class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent evt)
            {
                quit();
            }
        }
    
        private void quit()
        {
            System.exit(0);
        }
    
        public static void main(String[] args)
        {
            Frame f = new ButtonTestFrameRx("Main Menu");
            f.setSize(400, 400);
            f.setLocation(300, 300);
            f.setVisible(true);
        }
    }

Similar Threads

  1. Is it possible to make a Phone call program using java?
    By fireball2008 in forum New To Java
    Replies: 2
    Last Post: 05-08-2008, 06:20 PM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. i need help to make this program
    By masaka in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 01:26 PM
  4. Attempting to make a Law of Sines program
    By taco89 in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 04:10 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09: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
  •