java - How to add background image to jframe confused -


this question has answer here:

how can can add background image jframe , should u add code?

import java.awt.*; import java.awt.event.*; import javax.swing.imageicon.*; import javax.swing.*; //import java swing use jframe etc  public class mathquiz extends jframe implements actionlistener {  jcheckbox answer1 = new jcheckbox(); //checkbozx answer 1 jcheckbox answer2 = new jcheckbox(); //checkbozx answer 2 jcheckbox answer3 = new jcheckbox(); //checkbozx answer 3 jtextarea textarea = new jtextarea();//text area question jbutton exit = new jbutton(); // button exit quiz jbutton calculator = new jbutton(); // button open calculator jlabel image = new jlabel(); //adds background image  // names associated numbers. int answer; int questionnumber;   /**    * bit of code starts process of showing window. **/ public mathquiz() {     // drop down code labeled 'initcomponents' below.      try {         initcomponents();     } catch (exception e) {}  }   /**  * bit of code first bit that's run. **/ static public void main(string[] args) {     mathquiz frame = new mathquiz();      // following code puts window on screen.      frame.setvisible(true); //allows jframe visible     frame.showquestion(1);//shows first question     frame.setsize(370, 350);//sets size of frame  }  /**  * bit of code sets how window looks. **/ public void initcomponents() throws exception { 

edits checkboxes answers

    answer1.setsize(new dimension(80, 20)); //set size of answer 1 checkbox     answer1.setvisible(true); //sets visible answer 1 checkbox     answer1.setmaximumsize(new dimension(80, 20));      answer1.setminimumsize(new dimension(80, 20));      answer1.setlocation(new point(10, 180)); //set location answer 1 checkbox       answer2.setsize(new dimension(80, 20)); //set size of answer 2 checkbox     answer2.setvisible(true);//sets visible answer 2 checkbox     answer2.setmaximumsize(new dimension(80, 20));     answer2.setminimumsize(new dimension(80, 20));     answer2.setlocation(new point(10, 210)); //set location answer 2 checkbox      answer3.setsize(new dimension(80, 20)); //set size of answer 3 checkbox     answer3.setvisible(true);//sets visible answer 3 checkbox     answer3.setmaximumsize(new dimension(80, 20));     answer3.setminimumsize(new dimension(80, 20));     answer3.setlocation(new point(10, 240)); //set location answer 3 checkbox 

code edit text area on jframe

    textarea.setsize(new dimension(340, 80)); //set size of text ares     textarea.setlinewrap(true);      textarea.setvisible(true); //sets visible text area     textarea.setmaximumsize(new dimension(400, 140)); //set maximum size of text area     textarea.setminimumsize(new dimension(400, 140)); //set minimum size of text area     textarea.setlocation(new point(10, 20)); //set location of text area     textarea.seteditable(false); 

code edit exit button on jframe

    exit.setsize(new dimension(104, 40)); //set size of button     exit.setvisible(true); //sets visible button     exit.setlocation(new point(230, 180)); //set location of button     exit.settext("exit"); 

code edit exit button on jframe

    calculator.setsize(new dimension(104, 40)); //set size of button     calculator.setvisible(true); //sets visible button     calculator.setlocation(new point(230, 230)); //set location of button     calculator.settext("calculator");      image.seticon(new imageicon("maths"));     image.setvisible(true); 

this sets size of window , adds swing utilities

    setsize(new dimension(430, 292));     setresizable(false);     getcontentpane().setlayout(null);     settitle("multiple choice math quiz!");     setlocation(new point(200, 200));     getcontentpane().add(answer1);     getcontentpane().add(answer2);     getcontentpane().add(answer3);     getcontentpane().add(textarea);     getcontentpane().add(exit);     getcontentpane().add(calculator); 

you adding image jlabel add in turn jframe.

as highlighted in comments, problem lies in null layout use.

try layout manager.


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -