[java] Java Buttons

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, I am working on a small game in Java and would like to know how I can add a quick and dirty button into my app. Right now I have a class that sets up the main window and I am using a Canvas to draw everything on. My question is can I use the JButton class to add buttons and how do I go about doing this? Thanks for any pointers.

Share on other sites
You must create, then add a JButton to a Container using that Container's add method. Since a Canvas is not a Container, you cannot add a JButton directly to a Canvas. You can, however, add a JButton to the Container which contains your Canvas. It would look something like this.

JFrame f = new JFrame("Game");Container c = f.getContentPane(); //this grabs the default Container used by the JFrame, so that we can add stuff to itJButton b = new JButton("Click me!");Canvas canvas = new Canvas();c.add(canvas); //here we are adding stuff to the Containerc.add(b); //you add buttons the same wayf.pack();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);

Share on other sites
Ok, I tried that and it sorta worked, except I got an exception and my window was only my button. Here is my code that initializes everything:

 public MainWindow()    {        super( "Game" );        // Exit the program when we close the window for god's sake!        setDefaultCloseOperation( EXIT_ON_CLOSE );        // We don't want to repaint when the OS says we need to        setIgnoreRepaint( true );                        GraphicsEnvironment ge =                             GraphicsEnvironment.getLocalGraphicsEnvironment();        GraphicsDevice gd = ge.getDefaultScreenDevice();                // Setup the Canvas        renderCanvas = new Canvas()        {            public Dimension getPreferredSize()            {                return new Dimension( 1024, 768 );            }        };                add( "Center", renderCanvas );        btn = new JButton( "CLICK!" );        btn.setLocation( 10, 10 );        add( btn );        pack();                // Put the window in the center of the screen        Point cp = ge.getCenterPoint();        cp.translate( -getWidth()/2, -getHeight()/2 );        setLocation( cp.x, cp.y );                setResizable( false );        setVisible( true );        renderCanvas.createBufferStrategy( 2 );    }

The exception I get is: Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

Thanks for any help, I can't seem to see what this is talking about.

Share on other sites
Hi.

Which line does your exception occur on?

Anyways, a shot in the dark:

If you are not using a layout manager, you should use setBounds to setup the buttons size and location. This sets up absolute positioning. On another note, it is not always trivial to add lightweight components (JButton) in combination with heavy weight components (Canvas). If you get layout problems you cannot explain, try replacing your Canvas with a JPanel (example), and see if it solves the problem. (I know you will have to comment out things like BufferStrategy, - I only mean this as a means for you to test your component combination works).
You can also try substituting your JButton with a awt Button.

Good luck ;)

• 9
• 23
• 10
• 19
• 17