【问题标题】:Background picture problems with other panels其他面板的背景图片问题
【发布时间】:2012-12-06 07:10:39
【问题描述】:

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

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);
}

【问题讨论】:

  • “一切都结束了..” 著名的遗言。 ;)
  • 如需尽快获得更好的帮助,请发帖 SSCCE。热链接到图像或在代码中创建它们。

标签: java image swing background-image jlabel


【解决方案1】:

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

将标签布局管理器设置为您需要的。像往常一样将其他组件添加到标签中。

将标签添加到框架中。

看看

举例

【讨论】:

    【解决方案2】:

    使“框架”成为匿名内部类。覆盖“绘画”方法。通过作为“paint”中的参数发送的 Graphics 对象绘制背景图像。然后,调用超类的“paint”,绘制所有的GUI元素。示例:

    final Image backgroundImage = ...(load image);
    JFrame frame = new JFrame() {
        @Override
        public void paint(java.awt.Graphics g) {
            g.drawImage(backgroundImage); // draw the background image
            super.paint(); // call the superclass method so all the 
                                    // GUI elements are still drawn.
        }
    };
    

    当然,如果您对他们有一些私人恩怨,您不必将其设为匿名内部类。你总是可以这样做:

    public class GameFrame extends JFrame {
        private Image backgroundImage;
        public GameFrame() {
            super(); // call the super constructor 
            backgroundImage = ...(load image);
        }
        @Override
        public void paint(java.awt.Graphics g) {
            g.drawImage(backgroundImage); // draw the background image
            super.paint(); // call the superclass method so all the 
                                    // GUI elements are still drawn.
        }
    }
    

    编辑:我正在查看此内容,您可能必须使用 JPanel“背景”而不是 JFrame“框架”来执行我在此处显示的操作,因为它看起来像 JPanel 完全覆盖了“框架”。如果用继承 JFrame 的类执行我在这里展示的操作不起作用,我会认为这是因为 JPanel 覆盖了在 JFrame 上绘制的任何内容,所以除了“背景”之外,只需执行相同的操作即可。

    【讨论】:

    • 1. JFrame 没有 paintComponent 方法。 2. 调用super.paintComponent 将(通常)清除之前对Graphics 上下文所做的任何更改——这就是它的工作......
    • @MadProgrammer 改了,我觉得可以。
    • 对不起,我对编程还很陌生,所以我不明白你所说的。你能改写一下吗?
    • @user1914437 我想我的回答是假设您了解相当多的编程概念。看看MadProgrammer的回答,你就明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2014-04-10
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多