java - JPanel does not appear correctly while extending JFrame -
this homework.
my directions: class guessgame: class contains main function creates jframe object, sets jframe’s size, visibility , exit.
class guessgameframe: guessgameframe class extends jframe. guessgameframe class has variables , methods defined in class jframe.
i not 100% sure problem believe jpanel not working correctly
any appreciated, if left out important please let me know , can add more detail.
main class:
public class guessgame { public static void main(string[] args){ guessgameframe localguessgameframe = new guessgameframe(); localguessgameframe.setvisible(true); //set visible localguessgameframe.setsize(380,175); // set size 380 width 175 height localguessgameframe.setdefaultcloseoperation(localguessgameframe.exit_on_close); // needed enable red x button close program } } extended class:
import javax.swing.*; import java.awt.*; public class guessgameframe extends jframe { private int lastdistance; // distance between last guess , number private color background; // background color of application private jframe f; static jpanel p; private jbutton b1; private jlabel l1; private jlabel l2; jtextfield t1 = new jtextfield(8); private jlabel l3; //declare constructor public guessgameframe() { //create our jframe f = new jframe("guessing game"); //create our panel p = new jpanel(); p.setbackground(color.magenta); // background color magenta //create our labels l1 = new jlabel("i have number between 1 , 1000."); l2 = new jlabel("can guess number? enter first guess:."); l3 = new jlabel("guess result appears here."); //create our button b1 = new jbutton("new game"); //add button , label panel p.add(l1); // adds label 1 p.add(l2); // adds label 2 p.add(t1); // adds textfield 1 p.add(l3); // adds label 3 p.add(b1); // adds button 1 //add panel jframe f.add(p); } } screenshot of output window when executed: 
i expect this:
you need remove jframe f in guessgameframe class because guessgameframe jframe:
and update code:
f = new jframe("guessing game"); this.settitle("guessing game");
f.add(p); this.getcontentpane().add(p);

Comments
Post a Comment