Results 1 to 6 of 6
Thread: Help with JButtons into JFrame
- 03-09-2011, 01:20 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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); } }
- 03-09-2011, 02:01 PM #2
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!
- 03-09-2011, 06:18 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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).
- 03-09-2011, 06:40 PM #4
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-09-2011, 09:51 PM #5
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.
- 03-10-2011, 04:49 AM #6
Similar Threads
-
How can I add JLabels, JButtons, JTextFields to a JPanel then JFrame?
By crazysketch in forum New To JavaReplies: 5Last Post: 03-05-2011, 12:43 AM -
JFrame: JButtons in a grid
By jackal in forum New To JavaReplies: 2Last Post: 06-09-2010, 07:56 PM -
A Simple way to use JButtons and place objects in a JFrame
By Singing Boyo in forum Java TipReplies: 0Last Post: 03-10-2009, 09:55 AM -
[SOLVED] Problems with JButtons and JFrame.EXIT_ON_CLOSE. Please Help!!!
By Singing Boyo in forum New To JavaReplies: 5Last Post: 03-09-2009, 03:09 AM -
JButtons
By fgasimzade in forum SWT / JFaceReplies: 1Last Post: 12-25-2007, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks