Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JButtons not showing

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JButtons not showing

    I have googled this issue and apparently it is common, but I'm not sure how to fix it in my case... I think it has something to do with mixing Swing and Awt libraries... Anyway, I am trying to create a menu for a game and at the moment its just a black screen with a title and I am trying to show a 'start game' button. The button shows up only when I resize the window and hover over the area where it should be. Does anyone know how I can fix this???? Here is my code...

        public void menu(final MazeTeeter mazeteeter){
    		Graphics g = canvas.getOffscreenGraphics();
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, xcanvas, ycanvas);
    		g.setColor(Color.WHITE);
    		g.setFont(new Font("SANS_SERIF", Font.ITALIC,100));
    		g.drawString("MazeTeeter", xcanvas/2 - 300, 200);
    		canvas.drawOffscreen();
     
    		class AddStartButtonListener implements ActionListener
    		{ public void actionPerformed(ActionEvent event){mazeteeter.runGame();}}
     
    		button = new JButton("Start Game");
    		ActionListener buttonListen = new AddStartButtonListener();
    		button.addActionListener(buttonListen);
     
    		panel = new JPanel();
    		panel.add(button);
    		panel.setBounds(xcanvas/2, ycanvas/2, 250, 100);
    		jframe.add(panel);
     
    	}

    Not only does the button not show up until hovering over it, but the 'setBounds' method does nothing to the panel. It remains in its original position.... Any ideas?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButtons not showing

    What is canvas? What is that painting code supposed to be doing?
    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And your setBounds isn't doing anything because you're not using a null layout. More recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM
  2. Adding JButtons to Frame
    By iKlaush in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 12th, 2011, 10:52 AM
  3. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM
  4. help with JFrame, drawing, and JButtons
    By Khoatic in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 12:34 AM
  5. JTables with JButtons, I'm overlooking something
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2010, 11:30 AM