Results 1 to 6 of 6
  1. #1
    g6pd is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Help with JButtons into JFrame

    Hello every1,
    I am trying to make a Frame that will show up the string "Mode Of The Game"
    and beneath it will show 2 buttons..
    somethings in the code is wrong and causes the buttons the be all over the Frame..

    the frame should look like this :
    ----------Frame----------

    Mode of the game

    ------------------
    - Button1 -
    ------------------
    ------------------
    - Button2 -
    ------------------
    please find the mistake :D thanks
    Java Code:
     
    public class TicTacToe {
        
        public static void main(String[] args) {	
       	
       	createMode();
    }
    	public static void createMode()
    	{
    		Settings mode = new Settings();
    		mode.setSize(400,400);
    		mode.setTitle("Mode Of The Game");
    		JButton cmp=new JButton("Player vs Computer");
    		cmp.setSize(150,50);
    		cmp.setLocation(150,50);
    		JButton plyr=new JButton("Player vs Player");
    		plyr.setSize(150,50);
    		plyr.setLocation(120,150);
    		mode.add(cmp);
    		mode.add(plyr);
    		mode.setVisible(true);
    	}
    }
    
    class Settings extends JFrame
    {
    		public void paint (Graphics g)
    	{
    		super.paint(g);
    		g.setColor(Color.black);
    		String str="Choose The Mode Of The Game";	
    		Font font = new Font(str,3,25);
    		g.setFont(font);
    		g.drawString(str,20,90);
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default

    Your mistake is that you're using layouts incorrectly. It looks like you're trying to use a null layout, which is bad enough, but you never set the JFrame (or more accurately, its contentPane) to have a null layout.

    Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Your mistake is that you're using layouts incorrectly. It looks like you're trying to use a null layout, which is bad enough, but you never set the JFrame (or more accurately, its contentPane) to have a null layout.
    You mean the default layout, not a null layout, right? JFrame's (well, JRootPane's) content pane is a JPanel, with a default layout of BorderLayout. See Source for javax.swing.JRootPane (GNU Classpath 0.97.2 Documentation).

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default

    Quote Originally Posted by NRitH View Post
    You mean the default layout, not a null layout, right? JFrame's (well, JRootPane's) content pane is a JPanel, with a default layout of BorderLayout. See Source for javax.swing.JRootPane (GNU Classpath 0.97.2 Documentation).
    I am well aware of that. However, the OP is trying to set the bounds of the components directly, which imply that he's trying to use a null layout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    set the layout of the mode to null and make sure all component are a correct size and a correct position. for example, your font with 25 and position 20, 90 will touch the border of the jframe. other mistake: a button with the location 150,50 and the other with the location 120,150 will vertical never be in-line.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    set the layout of the mode to null
    Bad advice. Learn how to use the layout managers. Kevin's already provided a link in the first response.

    db

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2011, 12:43 AM
  2. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  3. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  4. Replies: 5
    Last Post: 03-09-2009, 03:09 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

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