JFrame with JMenuBar and background in java Netbeans

1.1k views Asked by At

I am trying to create a JFrame with a JMenubar and a JPanel in it with a background image. The code I have so far create 2 window, a JFrame and a JPanel, the JFrame has the JMenuBar and the JPanel has the background, can I put these together into 1 window?

package RPGPackage;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.JMenuBar;

public class RPG extends JFrame {

  public RPG() {
      JFrame frame = new JFrame("RPG");
      frame.setVisible(true);
      frame.setSize(1005,710);
      frame.setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);

      JPanel panel = new JPanel();
      panel.setLayout(new BorderLayout());
      frame.add(panel);
      setLocation(0,0);
      setVisible(true);
      setSize(1005,710);
      setResizable(false);

      JMenuBar menubar = new JMenuBar();
      frame.setJMenuBar(menubar);

      JMenu file = new JMenu("File");
      file.setMnemonic(KeyEvent.VK_A);

      menubar.add(file);

      JMenuItem Load = new JMenuItem("Load",KeyEvent.VK_L);
      file.add(Load);

      JMenuItem Save = new JMenuItem("Save",KeyEvent.VK_S);
      file.add(Save);

      JMenuItem exit = new JMenuItem(new AbstractAction("Quit"){//Adds Exit button
      public void actionPerformed(ActionEvent e) {//adds an action to the JMenuItem     "Exit"
        System.exit(0);}//when action is performed program will exit
      });
      file.add(exit);//adds the JMenuitem to the JMenuBar thats above.

      Background background = new Background();
      add(background);
  }

  public static void main(String[] args) {
          RPG window = new RPG();
  }
}







package RPGPackage;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Background extends JPanel {

    @Override
    public void paintComponent(Graphics g) {

      super.paintComponent(g);

      ImageIcon background = new ImageIcon("Image.png");

      background.paintIcon(this, g, 0, 0);
  }
}
1

There are 1 answers

0
alex2410 On

1) your RPG class extends JFrame, you needn't to create another instance of frame

(JFrame frame = new JFrame("RPG");). Use your RPG instance.

2) Use BufferedImage instead of ImageIcon, and draw image like next :

private BufferedImage img = ImageIO.read(getClass().getResourceAsStream("Image.png"));

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(img, 0, 0, this);
}

3) call setVisible(true); at the end of interface construction or like next :

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        setVisible(true);
    }
});

4) setResizable(false); it's bad practice. Manage component resizing with help of LayoutManager.

5)use pack(); method instead of setSize(...)

I have fixed your code, examine it :

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class RPG extends JFrame {

    public RPG() {
        super("RPG");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);

        JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_A);

        menubar.add(file);

        JMenuItem Load = new JMenuItem("Load", KeyEvent.VK_L);
        file.add(Load);

        JMenuItem Save = new JMenuItem("Save", KeyEvent.VK_S);
        file.add(Save);

        JMenuItem exit = new JMenuItem(new AbstractAction("Quit") {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        file.add(exit);

        Background background = new Background();
        add(background);

        setSize(200,200);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        RPG window = new RPG();
    }

    class Background extends JPanel {

        private BufferedImage img;

        public Background() {
            try {
                img = ImageIO.read(getClass().getResourceAsStream("Image.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(img, 0, 0, this);
        }

    }
}

Also instead of painting image you can use JLabel for holding and painting that, it helps you to get some features from the box :

class Background extends JPanel {

    private ImageIcon img;

    public Background() {
        URL resource = getClass().getResource("Image.jpg");
        img = new ImageIcon(resource);
        JLabel l = new JLabel(img);
        add(l);
    }
}