How do I add a background image to my JFrame?

How to add background image to jframe confused

  • How can I can add a background image to my JFrame and where should U add the code? import java.awt.*; import java.awt.event.*; import javax.swing.ImageIcon.*; import javax.swing.*; //import java swing to use the Jframe etc public class MathQuiz extends JFrame implements ActionListener { JCheckBox answer1 = new JCheckBox(); //checkbozx for answer 1 JCheckBox answer2 = new JCheckBox(); //checkbozx for answer 2 JCheckBox answer3 = new JCheckBox(); //checkbozx for answer 3 JTextArea textArea = new JTextArea();//text area for question JButton exit = new JButton(); // button to exit quiz JButton calculator = new JButton(); // button to open calculator JLabel image = new JLabel(); //adds background image // Some names which will be associated with numbers. int answer; int questionnumber; /** * This bit of code starts the process of showing the window. **/ public MathQuiz() { // Drop down to the code labeled 'initComponents' below. try { initComponents(); } catch (Exception e) {} } /** * This bit of code is the first bit that's run. **/ static public void main(String[] args) { MathQuiz frame = new MathQuiz(); // The following code puts the Window on your screen. frame.setVisible(true); //allows the jframe to be visible frame.showQuestion(1);//shows the first question frame.setSize(370, 350);//sets the size of the frame } /** * This bit of code sets up how the Window looks. **/ public void initComponents() throws Exception { edits the checkboxes for the answers answer1.setSize(new Dimension(80, 20)); //set size of answer 1 checkbox answer1.setVisible(true); //sets visible to 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 to 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 to 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 to edit the text area on the JFrame textArea.setSize(new Dimension(340, 80)); //set size of text ares textArea.setLineWrap(true); textArea.setVisible(true); //sets visible to 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 to edit the exit button on the JFrame exit.setSize(new Dimension(104, 40)); //set size of button exit.setVisible(true); //sets visible to button exit.setLocation(new Point(230, 180)); //set location of button exit.setText("Exit"); code to edit the exit button on the JFrame calculator.setSize(new Dimension(104, 40)); //set size of button calculator.setVisible(true); //sets visible to button calculator.setLocation(new Point(230, 230)); //set location of button calculator.setText("Calculator"); image.setIcon(new ImageIcon("maths")); image.setVisible(true); This sets up the size of the Window and adds the swing utilities to the 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);

  • Answer:

    You do it properly by adding an image to a JLabel which you add in turn to the JFrame. As highlighted in the comments, the problem lies in the Null layout you use. Try another http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html.

user3504419 at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.