java - Background color does not change in bluej -
i have written code has label , button in frame. have changed background never changes.
import javax.swing.*; import java.awt.*; import javax.swing.border.*; public class frames { jframe main_menu=new jframe("main menu");jframe circumference=new jframe("circumference"); jframe area=new jframe("area");jframe perimeter=new jframe("perimeter");jframe sets=new jframe("sets"); jframe fundamental_oprations=new jframe("fundamental oprations");jframe power_and_roots=new jframe("power_and_roots"); void main_menu() { jpanel contentpane = (jpanel) main_menu.getcontentpane(); contentpane.setlayout(new borderlayout(10,10)); contentpane.setborder(new emptyborder(300, 150, 300, 150)); contentpane.setlayout(new gridlayout(4, 4)); jpanel buttonpanel = new jpanel(new gridlayout(8,8)); contentpane.add(labels.main_menu,borderlayout.north); contentpane.add(buttons.sets,borderlayout.south); main_menu.setbackground(color.yellow); main_menu.pack(); main_menu.setvisible(true); } }
you should setting background color of content pane via getcontentpane().setbackground(color.yellow)
:
import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import javax.swing.jframe; public class frames extends jframe { private static final long serialversionuid = 1l; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { new frames(); } catch (exception e) { e.printstacktrace(); } } }); } public frames() { setsize(new dimension(100, 100)); settitle("main menu"); getcontentpane().setbackground(color.yellow); setvisible(true); } }
also, consider using variable naming conventions; instance, main_menu
should named mainmenu
.
Comments
Post a Comment