如何连接菜单类与backgroundImage类并在视图类中调用它?

时间:2019-04-03 13:40:53

标签: java

我正在使用Model,View和Controller类进行Java项目。 问题是我又增加了2个类,每个类都有其用途。 第一类“菜单”在视图类中被调用。有用! 但是第二个类“ BackgroundImage”不起作用。在View类中调用它们之前,我无法先连接这些类。的背景 图片未显示!

代码如下: //菜单类

package menuPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class Menu extends JFrame{

    // Declaring the Java frame
    JFrame frame = new JFrame();
    // Declaring Jmenubar, Jmenu, and JmenuItem.
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem menuNewGame, menuSave, menuExit;

    public Menu(){

        // Adding frame functionalities.
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setSize(900,800);
        frame.setTitle("Oil Platform Defence");
        frame.setLocationRelativeTo(null);


        // creating menu with bar and sub menu
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar); // frame to set our menubar to the frame.

        // menu naming, and functionality
        menu = new JMenu("Game");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription("menuPackage.Menu description"); // menu to be accessible ...

        // Adding menu to the menuBar
        menuBar.add(menu);

        // Adding menuitems in the submenu
        menuNewGame = new JMenuItem("New Game");
        menu.add(menuNewGame);
        menu.addSeparator();
        menuExit = new JMenuItem("Exit Game");
        menu.add(menuExit);




        // Setting frame visible/ not resizeable.
        frame.setResizable(false);
        frame.setVisible(true);

    }
}


// Background Image class: 

    package imagePackage;
import javax.swing.*;
import java.awt.*;

public class BackgroundImage extends JFrame{

    JFrame frame = new JFrame();

    public BackgroundImage(){

        // Adding background image
        ImageIcon imageIcon = new ImageIcon("Pictures/Background.png");
        Image image = imageIcon.getImage();
        Image newImg = image.getScaledInstance(900, 800, Image.SCALE_SMOOTH);
        imageIcon = new ImageIcon(newImg);

        JLabel background = new JLabel(imageIcon);
        frame.add(background);

    }
}


// View class: 

    import menuPackage.Menu;
import imagePackage.BackgroundImage;

public class View{

    Menu menu;
    BackgroundImage backgroundImage;


    public View(){

        menu = new Menu();
        backgroundImage = new BackgroundImage();

    }
}

0 个答案:

没有答案