1. The default frame Layout mode is borderlayout. Therefore, if you want to use the setbounds method to set a specific position for the component, it is best to remove the default Layout mode and set it to null.
For example:
// this.setLayout(null); gamePanel.setVisible(true); pokerPanel.setVisible(true); gameInfoPanel.setVisible(true); pokerPanel.setBounds(10, 10, 580, 200); pokerPanel.setBackground(Color.RED); gamePanel.setBounds(10, 220, 580, 200); gamePanel.setBackground(Color.GREEN); gameInfoPanel.setBounds(10, 430, 580, 140); gameInfoPanel.setBackground(Color.BLUE); add(pokerPanel); add(gamePanel); add(gameInfoPanel);
Effect: No matter which panel is added, the entire frame will be filled.
The correct effect after the comment is removed:
2. Show Images
The first method is to add an image to the label,
Instance:
Package com; import Java. AWT. borderlayout; import java.net. URL; import javax. swing. *; public class showimage extends jframe {/*** @ Param ARGs */Public showimage () {jpanel Panel = new jpanel (New borderlayout ()); jpanel panel2 = new jpanel (New borderlayout (); jpanel panel3 = new jpanel (New borderlayout (); string urlstring = "D: \ myeclipse 6.0 \ New Folder \ eXam \ bin \ images \ winter.jpg "; jlabel label = new jlabel (New imageicon (urlstring )); URL url = getclass (). getresource ("/images/orz2.jpg"); jlabel label2 = new jlabel (New imageicon (URL); Url url2 = getclass (). getresource ("/images/orz.jpg"); jlabel label3 = new jlabel (New imageicon (url2); panel. add (Label, borderlayout. center); panel2.add (label2, borderlayout. center); panel3.add (label3, borderlayout. center); this. getcontentpane (). setlayout (New borderlayout (); this. getcontentpane (). add (panel, borderlayout. center); this. getcontentpane (). add (panel2, borderlayout. south); this. getcontentpane (). add (panel3, borderlayout. east); this. setsize (400,300); this. setdefaclocloseoperation (jframe. exit_on_close); this. settitle ("show image"); this. setvisible (true);} public static void main (string [] ARGs) {// todo auto-generated method stub showimage = new showimage ();}}
Label1 uses absolute paths, label2 and label3 use relative paths, and they are relative to the paths of class files (. class files ).
Another better way to draw images: Use G. drawimage
Instance:
Color color = G. getcolor (); // first re-draw the Panel and clear all the content above G. setcolor (this. getbackground (); G. fillrect (0, 0, this. getwidth (), this. getheight (); G. setcolor (color); Url url1 = getclass (). getresource ("images/" + A + ". jpg "); image image1 = gettoolkit (). getimage (url1); G. drawimage (image1, 20, 10, null, this );
My instances:
URL url1 = getclass (). getresource ("images/" + A + ". jpg "); image image1 = gettoolkit (). getimage (url1); G. drawimage (image1, 30, 10, null, this); Url url2 = getclass (). getresource ("images/" + B + ". jpg "); image image2 = gettoolkit (). getimage (url2); G. drawimage (image2, 164, 10, null, this); Url url3 = getclass (). getresource ("images/" + C + ". jpg "); image image3 = gettoolkit (). getimage (url3); G. drawimage (image3, 298, 10, null, this); Url url4 = getclass (). getresource ("images/" + D + ". jpg "); image image4 = gettoolkit (). getimage (url4); G. drawimage (image4, 432, 10, null, this); // previously used label, not very good // URL url1 = getclass (). getresource ("images/" + A + ". jpg "); // jlabel label1 = new jlabel (New imageicon (url1); // This. add (label1); // URL url2 = getclass (). getresource ("images/" + B + ". jpg "); // jlabel label2 = new jlabel (New imageicon (url2); // This. add (label2); // URL url3 = getclass (). getresource ("images/" + C + ". jpg "); // jlabel label3 = new jlabel (New imageicon (url3); // This. add (label3); // URL url4 = getclass (). getresource ("images/" + D + ". jpg "); // jlabel label4 = new jlabel (New imageicon (url4); // This. add (label4 );
:
3. setbounds and setlocation
Instance:
JButton newButton = new JButton("New game"); newButton.setLocation(300, 20); // newButton.setBounds(400, 20,100,30); gamePanel.add(newButton);
The result is: [left: setlocation]. [Right: setbounds ].
So: If you want to display it, either use setbounds or setlocation plus setsize (both must be written, otherwise it will not be displayed)