将JLabel置于另一个具有图像的JLabel之上 [英] Having a JLabel on top of another JLabel that has an image

查看:170
本文介绍了将JLabel置于另一个具有图像的JLabel之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Soo我正在尝试用GUI制作棋盘游戏。我用玩家的名字创建了一个JLabel,并希望将其用作令牌。然后我制作了另一个包含棋盘图像的JLabel。我将两个标签添加到了BoardPanel上,现在,标签是并排的。我如何做到这一点,而不是它并排,JLabel W /名称是在JLabel w / img?



有什么我应该考虑的对于包含两个标签的面板?像某个布局管理器?



[只是我的代码的部分]

  import java.awt。*; 
import javax.swing。*;

公共类BoardFrame扩展JFrame {

私有JPanel mainPanel,boardImgPanel,jPanelSouth,buttonPanel
,cardPanel,statsPanel;
private boardImgLabel;

玩家player1;

public BoardFrame(){
// boardPanel和其中的东西
boardImgPanel = new JPanel();
boardImgLabel = new JLabel();
boardImgLabel.setIcon(new ImageIcon(BOARDPICTUREHERE));
boardImgPanel.add(boardImgLabel);

/////////////////////添加播放器/////////////////// //
player1 =新玩家(史蒂文,1,1,1,1,1);
JLabel player1Label = new JLabel(player1.getPlayerName());
boardImgPanel.add(player1Label);

mainPanel = new JPanel(new GridLayout(0,1));

add(mainPanel);

}
}

这样的东西但显然不是一只猫,它是棋盘图像,文字是玩家的名字。



编辑



我创建了一个扩展jpanel的类,然后想将它添加到BoardFrame类中

  import java.awt.BorderLayout; 
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

公共类BoardPanelNorth扩展JPanel {
文件imageFile = new File(....);

JLabel boardImgLabel = new JLabel();
玩家player1;

public BoardPanelNorth(){
setLayout(new BorderLayout());
try {
boardImgLabel = new JLabel(new ImageIcon(ImageIO.read(imageFile)));
boardImgLabel.setLayout(new BorderLayout());

player1 =新玩家(Steven,1,1,1,1,1);
JLabel player1Label = new JLabel(player1.getPlayerName());
player1Label.setFont(player1Label.getFont()。deriveFont(128f));
player1Label.setHorizo​​ntalAlignment(JLabel.RIGHT);
player1Label.setVerticalAlignment(JLabel.BOTTOM);
player1Label.setForeground(Color.WHITE);

add(boardImgLabel);
} catch(IOException ex){
ex.printStackTrace();
}
}

}

Board Frame class:

  import java.awt。*; 
import javax.swing。*;

公共类BoardFrame扩展JFrame {

私有JPanel mainPanel,boardImgPanel;
private JSplitPane splitPane;

public BoardFrame(){

boardImgPanel = new BoardPanelNorth();


//分割平面
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
new JScrollPane(boardImgPanel),jPanelSouth);
splitPane.setDividerLocation(470); // top size
splitPane.enable(false); //无法调整


mainPanel = new JPanel(new GridLayout(0,1));
mainPanel.add(splitPane);

add(mainPanel);

}
}

玩家史蒂文似乎仍然没有出现在地图上。

解决方案

您可以使用

  import java.awt .BorderLayout; 
import java.awt.Color;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

公共类测试{

public static void main(String [] args){
new Test();
}

public Test(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}

JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类TestPane扩展JPanel {

public TestPane(){
setLayout(new BorderLayout());
try {
JLabel background = new JLabel(new ImageIcon(ImageIO.read(...)));
background.setLayout(new BorderLayout());
JLabel text = new JLabel(我只是这样绘制的);
text.setFont(text.getFont()。deriveFont(128f));
text.setHorizo​​ntalAlignment(JLabel.RIGHT);
text.setVerticalAlignment(JLabel.BOTTOM);
text.setForeground(Color.WHITE);

background.add(text);

add(背景);
} catch(IOException ex){
ex.printStackTrace();
}
}

}

}


Soo I'm trying to make a board game with GUI. I created a JLabel with the player's name and want to use that as a token. Then I made another JLabel that contains the image of the board. I added both the labels onto a boardPanel and right now, the labels are side by side. How do I make it so that instead of it being side to side, the JLabel w/ name is on the JLabel w/ img?

Is there something I should be considering for the panel that contains the two labels? Like a certain Layout Manager?

[just sections of my code]

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

public class BoardFrame extends JFrame {

   private JPanel mainPanel, boardImgPanel,jPanelSouth,buttonPanel
           ,cardPanel,statsPanel;
   private boardImgLabel;

   Player player1;

   public BoardFrame() {
      //boardPanel and stuff in it
      boardImgPanel = new JPanel();
      boardImgLabel = new JLabel();
      boardImgLabel.setIcon(new ImageIcon("BOARDPICTUREHERE"));
      boardImgPanel.add(boardImgLabel);

      /////////////////////ADDING PLAYERS/////////////////////
      player1 = new Player("Steven", 1,1,1,1,1);
      JLabel player1Label = new JLabel(player1.getPlayerName());
      boardImgPanel.add(player1Label);

      mainPanel = new JPanel(new GridLayout(0, 1));

      add(mainPanel);

   }
}

something like this but obviously instead of a cat, it's the board image, and the text is the player name.

EDIT

I made a class that extends the jpanel and then wanted to add it into the BoardFrame class

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BoardPanelNorth extends JPanel {
        File imageFile = new File("....");

        JLabel boardImgLabel = new JLabel();
        Player player1;

        public BoardPanelNorth() {
            setLayout(new BorderLayout());
            try {
                boardImgLabel = new JLabel(new ImageIcon(ImageIO.read(imageFile)));
                boardImgLabel.setLayout(new BorderLayout());

                player1 = new Player("Steven", 1,1,1,1,1);
                JLabel player1Label = new JLabel(player1.getPlayerName());
                player1Label.setFont(player1Label.getFont().deriveFont(128f));
                player1Label.setHorizontalAlignment(JLabel.RIGHT);
                player1Label.setVerticalAlignment(JLabel.BOTTOM);
                player1Label.setForeground(Color.WHITE);

                add(boardImgLabel);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

}

Board Frame class:

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

public class BoardFrame extends JFrame {

   private JPanel mainPanel, boardImgPanel;
   private JSplitPane splitPane;

   public BoardFrame() {

      boardImgPanel = new BoardPanelNorth();


      //split plane
      splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
         new JScrollPane(boardImgPanel), jPanelSouth);
      splitPane.setDividerLocation(470); //top size
      splitPane.enable(false); //cant adjust


      mainPanel = new JPanel(new GridLayout(0, 1));
      mainPanel.add(splitPane);

      add(mainPanel);

   }
}

Player Steven still does not seem to appear on the map.

解决方案

You could make use of an OverlayLayout or you could simply add the second JLabel to the first. The trick here though, is to set up a layout manager for the first label.

Beware though, a JLabel only use the icon and text properties its determine the preferred layout size, which could end up truncating it's children

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                JLabel background = new JLabel(new ImageIcon(ImageIO.read(...)));
                background.setLayout(new BorderLayout());
                JLabel text = new JLabel("I'm just drawn this way");
                text.setFont(text.getFont().deriveFont(128f));
                text.setHorizontalAlignment(JLabel.RIGHT);
                text.setVerticalAlignment(JLabel.BOTTOM);
                text.setForeground(Color.WHITE);

                background.add(text);

                add(background);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

}

这篇关于将JLabel置于另一个具有图像的JLabel之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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