简体   繁体   中英

How to set background image?

I created a jframe with jmenubar and added a background image.Then I added a sub menu.But the background image disappeared.How can I solve this problem?See the code below.

    public Welcome() {
    //JFrame frame=new JFrame();
    setTitle("PAIN AND PALIATIVE CARE UNIT VAZHAYOOR");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBackground(Color.WHITE);


    JLabel background=new JLabel(new ImageIcon("src/startApplication/Helping-Hands_1.png")); 
    background.setBackground(Color.white);
    add(background);


    mbar=new JMenuBar();
    menu = new JMenu("Medicine");
    menu.add(makeMenuItem("Add Stock"));
    menu.add(makeMenuItem("Add Medicine Details"));
    menu.add(makeMenuItem("Spent Medicine"));
    menu.add(makeMenuItem("Check Availability"));
    menu.add(makeMenuItem("View Stock Details"));

    viewMenu=new JMenu("Daily");

    viewMenu=new JMenu("Report");
    viewMenu.add(makeMenuItem("Daily"));
    viewMenu.add(makeMenuItem("Monthly"));
    viewMenu.add(makeMenuItem("Periodical"));

    funds = new JMenu("Funds");
    funds.add(makeMenuItem("Add"));
    funds.add(makeMenuItem("Edit"));
    funds.add(makeMenuItem("View"));
    funds.add(makeMenuItem("Mark Expense"));
    funds.add(add(viewMenu));



    mbar.add(menu);
    mbar.add(funds);
    setJMenuBar(mbar);
    setSize(getMaximumSize());
    setVisible(true);
}

You're calling JFrame#add twice, thereby displacing the JLabel component at the BorderLayout.CENTER location of the JFrame

funds.add(add(viewMenu));
          ^^^

replace with

funds.add(viewMenu);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM