Results 1 to 17 of 17
Thread: JButton issue
- 11-13-2010, 09:47 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
JButton issue
I need to create 3 buttons like this picture

This is my code
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Layout1 { public static void main(String [] args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(100, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyFrame extends JFrame { JPanel pan = new JPanel(); JRadioButton Rbtn = new JRadioButton("One", true); JTextField txt1 = new JTextField("one"); JTextField txt2 = new JTextField("two"); JTextField txt3 = new JTextField("three"); JTextField txt4 = new JTextField("four"); JButton btn1 = new JButton("btn1"); JButton btn2 = new JButton("btn2"); JButton btn3 = new JButton("btn2"); JButton btn4 = new JButton("btn3"); JPanel btnpanel = new JPanel(); ButtonGroup group = new ButtonGroup(); public MyFrame(String s) { super(s); setLayout ( new GridLayout (2, 3)); add(Rbtn, BorderLayout.NORTH); add(txt1); add(txt2); add(txt3); add(txt4); btnpanel.add(btn1); add(btn1); } }
-
You're going to have to nest a JPanel inthe lower right corner and give it its own layout.
- 11-13-2010, 11:35 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Could you give an example?
- 11-13-2010, 11:52 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
I did this but did not work
JPanel btnpan = new JPanel();
btnpan.setLayout(new GridLayout(1, 3));
btnpan.add(btn1);
add(btn1);
btnpan.add(btn2);
add(btn2);
- 11-14-2010, 12:01 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You need to (1) add all three buttons to btnpan then (2) add btnpan to the frame's content pane (the one with the 3x2 layout).
- 11-14-2010, 12:13 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
I understand adding 3 buttons to btnpan, but I did not understand the second part you said.
How to add btnpan to frame content pane??
- 11-14-2010, 12:20 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
How to add btnpan to frame content pane??
You add btnpan the same way you added the radio button and the text fields.
- 11-14-2010, 12:27 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
I am sorry did not get, could you show how
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Layout1 { public static void main(String [] args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(100, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyFrame extends JFrame { JPanel pan = new JPanel(); JRadioButton Rbtn = new JRadioButton("One", true); JTextField txt1 = new JTextField("one"); JTextField txt2 = new JTextField("two"); JTextField txt3 = new JTextField("three"); JTextField txt4 = new JTextField("four"); JButton btnpan = new JButton(); JButton btn1 = new JButton("btn1"); JButton btn2 = new JButton("btn2"); JButton btn3 = new JButton("btn2"); JButton btn4 = new JButton("btn3"); public MyFrame(String s) { super(s); setLayout ( new GridLayout (2, 1)); add(Rbtn); add(txt1); add(txt2); add(txt3); add(txt4); btnpan.add(btn1); btnpan.add(btn2); btnpan.add(btn3); add(btn1); add(btn2); //add(btn3);*/ } }
-
Your adding the buttons to both the contentPane and the btnpan, don't do that.
Again, add btn1, btn2, btn3 to the btnpan only. Then add the btnpan to the contentPane the very same way that you're adding txt1, txt2, txt3 to the contentPane. You can do this.Java Code:// adding buttons to the btnpan btnpan.add(btn1); btnpan.add(btn2); btnpan.add(btn3); // ??? adding buttons to the contentPane too? add(btn1); add(btn2);
Last edited by Fubarable; 11-14-2010 at 01:03 AM.
- 11-14-2010, 01:18 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Thanks I got it. But the buttons showed very small not filling all the space.
-
- 11-14-2010, 01:33 AM #12
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Layout1 { public static void main(String [] args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(100, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyFrame extends JFrame { JPanel pan = new JPanel(); JRadioButton Rbtn = new JRadioButton("One", true); JTextField txt1 = new JTextField("one"); JTextField txt2 = new JTextField("two"); JTextField txt3 = new JTextField("three"); JTextField txt4 = new JTextField("four"); JPanel btnpan = new JPanel(); JButton btn1 = new JButton("btn1"); JButton btn2 = new JButton("btn2"); JButton btn3 = new JButton("btn3"); public MyFrame(String s) { super(s); setLayout ( new GridLayout (2, 3)); add(Rbtn); add(txt1); add(txt2); add(txt3); add(txt4); btnpan.add(btn1); btnpan.add(btn2); btnpan.add(btn3); add(btnpan); } }
-
OK, somewhere previously you had set the btnpan layout properly. What happened to that bit of code?
- 11-14-2010, 01:59 AM #14
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
That's when I added did this
btnpanel.add(btn1);
add(btn1);
-
No, I'm talking about when you called btnpanel.setLayout(....).
Try that again!
- 11-14-2010, 02:28 AM #16
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Got it I set the layout to 3 rows Thanks man you the best.
-
Hey you had that code all along... I just reminded you.
Best of luck.
Similar Threads
-
JButton with Icon & text alignment issue
By ShardaD in forum AWT / SwingReplies: 4Last Post: 01-11-2011, 11:10 AM -
Issue with obtaining data from the JTextField when JButton is pressed
By turc0033 in forum AWT / SwingReplies: 7Last Post: 08-29-2010, 10:33 AM -
JButton Help
By ravjot28 in forum AWT / SwingReplies: 2Last Post: 01-17-2010, 01:07 PM -
JButton help :)
By yasmin k in forum AWT / SwingReplies: 7Last Post: 11-12-2009, 09:53 PM -
Help with JButton
By geoffreybarwise in forum New To JavaReplies: 4Last Post: 05-21-2008, 10:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks