java - Adding JPanel to JFrame? -
i'm trying add jlabel jpanel jframe. set border jpanel, see on jframe small black square in center of frame. whatever can't change size or location of it. please help.
start main = new start(); random random = new random(); jframe mainframe = new jframe("mainframe"); jpanel mainpanel = new jpanel(); jlabel welcomelabel = new jlabel(); mainframe.add(main); mainframe.setlayout(new gridbaglayout()); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainframe.settitle(names[random.nextint(names.length)]); mainframe.pack(); mainframe.setvisible(true); mainframe.setsize(mainframex, mainframey); mainframe.setresizable(false); mainframe.setlocationrelativeto(null); mainframe.add(mainpanel); mainpanel.add(welcomelabel); mainpanel.setborder(new lineborder(color.black)); mainpanel.setsize(new dimension(200, 200)); welcomelabel.setfont(new font("verdana", 1, 20)); welcomelabel.setlocation(100, 100); main.start();
suggestions:
- you want read tutorial, laying out components, explain how code swing layout managers, , information essential solve current problem.
- one caveat: urge avoid temptation use null layout use of lead creation of code hard maintain or upgrade.
- your jlabel, welcomelabel, of course need text visible.
- don't set it's location via setlocation(...) again use layout managers dirty work of placing , sizing components.
- you want call pack() , setvisible(true) on jframe after adding initial components.
Comments
Post a Comment