java - Color for JFrame not working even though getcontentpane is already added -



java - Color for JFrame not working even though getcontentpane is already added -

i pretty new java. class homework. finished except jframe color doesn't show. looked @ other similar questions. of them said utilize getcontentpane(). problem added not showing. below code. separated 2 parts. sec part has jframe.getcontentpane().setbackground(color.**) code. give thanks you.

import java.awt.borderlayout; import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; public class numbergame extends jframe { private jpanel content; private jframe displayframe; private jtextfield input; private jlabel displaytext, message; private int guess; private jbutton button; private jbutton newgame; private int number; private int lowest = 0; private int highest = 0; public void numbergame () { content = new jpanel (); displayframe = new jframe ("welcome"); displayframe.setsize(700, 400); displayframe.setlayout (new borderlayout()); displaytext = new jlabel ("i have number between 1 , 1000. can guess number? come in first guess."); input = new jtextfield (20); content.add(input); message = new jlabel (""); button = new jbutton ("submit"); button.addactionlistener (new guesshandler()); newgame = new jbutton ("new game"); newgame.addactionlistener (new actionlistener () { public void actionperformed (actionevent ae) { input.settext(""); message.settext(""); repaint(); } } ); displayframe.add(content); content.add(displaytext); content.add(input); content.add(button); content.add(newgame); content.add(message); thegame(); displayframe.setvisible(true); } public void thegame () { number = (int) (math.random() * 1000 +1); } public static void main(string[] args) { numbergame = new numbergame (); a.numbergame(); }

here rest of code have problem with.

class guesshandler implements actionlistener { @override public void actionperformed (actionevent ae) { guess = integer.parseint(input.gettext()); if(guess>number) { message.settext("too high!"); if (guess < lowest) { lowest = guess; displayframe.getcontentpane().setbackground(color.red); } else displayframe.getcontentpane().setbackground(color.blue); } else if (guess < number) { message.settext("too low!"); if (guess > highest) { highest = guess; displayframe.getcontentpane().setbackground(color.red); } else displayframe.getcontentpane().setbackground(color.blue); } else { message.settext("correct!"); input.seteditable(false); lowest = 0; highest = 1000; } repaint (); } } }

content jpanel, because jframe uses borderlayout, panel content occupy entire available space, covering frames own content pane (don't confuse two, different).

try changing

displayframe.add(content);

to

displayframe.setcontentpane(content);

you might have read through code conventions java tm programming language, create easier people read code , read others

java swing jframe jpanel

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -