其他面板的背景图片问题 [英] Background picture problems with other panels

查看:94
本文介绍了其他面板的背景图片问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我为纸牌游戏War创建了一个程序.一切都完成了,除了我想添加背景.问题是我不知道如何添加背景图片,以便其他JLabel和JButton仍然出现在图片的顶部.我是否需要设置背景图片,以便其他面板也显示在顶部?到目前为止,我的程序是这样:

So i have created a program for the card game War. Everything is finished except I wanted to add a background. The problem is I don't know how to add a background picture so that the other JLabels and JButtons still appear at the top of the picture. Is there any for me set up a background picture so that the other panels show up on top as well? My program so far is this:

public War()
{
    JFrame frame = new JFrame();

    frame.setSize(500, 500);
    frame.setTitle("WAR");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    //Set up for background image
    Background = new JPanel();
    Background.add(new Background("Background.png"));
    frame.add(Background);
    Background.setLayout(new OverlayLayout(Background));

    cardPanel = new JPanel (new GridLayout(2,1));
    ///create labels and init with card back
    String strCard1 = "cardback.png";
    String strCard2 = "cardback.png";
    lblPlayer1Card = new JLabel(new ImageIcon(strCard1));
    lblPlayer2Card = new JLabel(new ImageIcon(strCard2));
    cardPanel.add(lblPlayer1Card);
    cardPanel.add(lblPlayer2Card);

    //Create panels for when war is played
    warPanel = new JPanel (new GridLayout(2, 3));
    String emptycards = "blankcard.png";
    player1war1 = new JLabel(new ImageIcon(emptycards));
    player1war2 = new JLabel(new ImageIcon(emptycards));
    player1war3 = new JLabel(new ImageIcon(emptycards));
    player2war1 = new JLabel(new ImageIcon(emptycards));
    player2war2 = new JLabel(new ImageIcon(emptycards));
    player2war3 = new JLabel(new ImageIcon(emptycards));
    warPanel.add(player1war1);
    warPanel.add(player1war2);
    warPanel.add(player1war3);
    warPanel.add(player2war1);
    warPanel.add(player2war2);
    warPanel.add(player2war3);


    // Create a panel
    btnPanel = new JPanel(new GridLayout(2,2));
    //set up next card button
    JButton btnNext = new JButton("Next Card");
    NextListener listener1 = new NextListener();
    btnNext.addActionListener(listener1);
    //set up new game button
    JButton btnNewGame = new JButton("New Game");
    NewGameListener listener2 = new NewGameListener();
    btnNewGame.addActionListener(listener2);


    //Sets up new panel showing size of main pile and discard pile of both players
    statusPanel = new JPanel(new GridLayout(4,2));

    //status labels that show the size of piles
    status1 = new JLabel("");// Size of player 1's main pile
    status2 = new JLabel("");//Size of player 1's discard pile
    status3 = new JLabel("");//Size of player 2's main pile
    status4 = new JLabel("");//Size of player 2's discard pile
    statusWar = new JLabel("");//Status label that shows the winner of War
    statusEmpty1 = new JLabel(""); // Creates empty status to put space between War status and size statuses
    statusEmpty2 = new JLabel(""); // Creates empty status to put space between War status and size statuses

    statusBar = new JLabel("");

    //Adds the labels to the panels
    btnPanel.add(btnNext);
    btnPanel.add(btnNewGame);
    btnPanel.add(statusBar);
    statusPanel.add(status1);
    statusPanel.add(status2);
    statusPanel.add(status3);
    statusPanel.add(status4);
    statusPanel.add(statusEmpty1);
    statusPanel.add(statusEmpty2);
    statusPanel.add(statusWar);

    cardPanel.setOpaque(false);
    btnPanel.setOpaque(false);
    statusPanel.setOpaque(false);
    warPanel.setOpaque(false);

    //The layout of the frame
    Background.add(cardPanel, BorderLayout.WEST);
    Background.add(btnPanel, BorderLayout.SOUTH);
    Background.add(statusPanel, BorderLayout.NORTH);
    Background.add(warPanel, BorderLayout.EAST);

    frame.setVisible(true);
}

推荐答案

为自己创建一个JLabel.加载图像并将其应用于标签(JLabel#setIcon).

Create your self a JLabel. Load the image and apply it to the label (JLabel#setIcon).

根据需要设置标签布局管理器.像往常一样将其他组件添加到标签上.

Set the labels layout manager to what you need. Add your other components to the label as normal.

将标签添加到框架.

看看

  • Place JLabel on top of JLabel with image in
  • More than one JPanel in a Frame / having a brackground Image and another Layer with Components on the top

例如

这篇关于其他面板的背景图片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆