Results 1 to 9 of 9
Thread: Adding JButton to JPanel
- 05-21-2011, 12:59 AM #1
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
Adding JButton to JPanel
Hi guys
i have extended a class to JPanel to create a image panel witch is loading a image as a background code is follows
and then i use this panel to my JFrameJava Code:public class ImagePanel extends JPanel { private Image img; public ImagePanel (Image img){ this.img = img; Dimension size = new Dimension(img.getWidth(null),img.getHeight(null)); setSize(size); setPreferredSize(size); setMaximumSize(size); setMinimumSize(size); setLayout(null); } public void paintComponent(Graphics g){ g.drawImage(img,0,0,null); }
i wanted to add a JLabel and JButton to this panel. i have added both of them. there is no error in the code but when i run the program it doesn't show the Jbutton and JLabel
Code..
if any one know the solution please postJava Code:private static final long serialVersionUID = 1L; static ImagePanel panel = new ImagePanel(new ImageIcon("res/back10.png").getImage()); static JLabel username = new JLabel("User Name"); static JButton ok = new JButton("ok"); public JWinTest(){ super("Enter Username | Password"); getContentPane().add(panel); panel.add(username); panel.add(ok); setVisible(true); } public static void main(String[] args){ JWinTest t = new JWinTest(); t.getContentPane().add(panel); t.pack(); t.setVisible(true); }
it would be great full
thank you guys for your time
- 05-21-2011, 01:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You are adding all sorts of things to the frame's content pane in this order:
panel (with the image)
username
ok
panel (again)
Notice that t has a BorderLayout (that's the default for a frame's content pane) and the effect of add() is to place the component being added into the middle of the pane replacing whatever was there. So adding four things the way you do will mean that only the last remains added.
---------
Adding panel twice betrays a little desparation. You might find it useful to have a read of the Swing section of Oracle's tutorial or some other textbook to see how layouts work and what options are available.
- 05-21-2011, 01:25 AM #3
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
is there anyway to add username and ok button to top of that panel
-
You can nest JPanels, each one of them having its own layout. So create JPanel, add your username and ok button on it, and then add the JPanel to another JPanel (or the contentPane) that uses BorderLayout in the BorderLayout.NORTH position. But most important -- please read the Oracle Swing tutorial on use of layout managers.
- 05-21-2011, 02:05 AM #5
Other points:
-- Why do you construct an ImageIcon only to retrieve its image? Load the image using ImageIO#read(...) instead.
-- As Fubarable said, learn to use layout managers. Don't set a null layout to ImagePanel.
db
- 05-22-2011, 03:36 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
There is no need to create a custom class for this. The only time you need to do custom painting is when you want to scale the image or manipulate it in some other way.
To display an image at its actual size just use a JLabel and add an Icon to it. YOu can then set the layout manager of the label and add other components to it the same way you add components to a panel.
- 05-23-2011, 03:16 AM #7
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
thank you guys for your posts. but stile i didnt sort out my problem
i dont want to use the layouts. i wanted to set the layout to null and i want to place the component (JButtons, JLabels) where i wants
-
- 05-23-2011, 11:03 PM #9
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
Adding a jpanel to a customized Jpanel Class
By trishtren in forum AWT / SwingReplies: 7Last Post: 04-05-2011, 06:52 PM -
Adding Jpanel ontop of another Jpanel
By Manfizy in forum AWT / SwingReplies: 4Last Post: 03-05-2011, 10:34 PM -
[SWING]Adding JButton(class1) to JPanel(class2)
By equal in forum New To JavaReplies: 8Last Post: 02-20-2011, 01:09 AM -
Adding JButton to a JTable
By ting.at.net@hotmail.com in forum AWT / SwingReplies: 6Last Post: 05-26-2009, 03:37 AM -
Problem on adding JButton on JPanel NEED HELP
By boisk in forum AWT / SwingReplies: 15Last Post: 03-15-2009, 02:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks