• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to creat JFrame with buttons

 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a JFrame called frame in a method TestGrid()and add a grid with buttons on it, here's the code so far. I was hoping someone could tell me why this doesn't show the button(s). thanks again.

public TestGrid(){
Jframe frame = new JFrame ("HelloWorldSwing");
this.setDefaultCloseOperation (JFrame.EXIT_ON_ClOSE);
JPanel content=new JPanel();
content.setLayout (new GridLayout(2,2));
content.add(new JButton ("Button1"));
this.setSize(300,300);
this.setVisible(true);
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't introducted the frame to your content pane.
 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, I've added the frame.setContentPane line but the window still doesn't show any buttons. I'll keep working on this. Thanks again.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you gave (once I removed syntax errors) show a button:
 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, yes it works fine. I guess the reason my try didn't work is that the code I sent you was a "snippet". Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic