2015-10-14 106 views
0

Soo我正在嘗試使用GUI製作棋盤遊戲。我創建了一個帶有玩家名字的JLabel,並將其用作令牌。然後我製作了另一張包含電路板圖像的JLabel。我將兩個標籤添加到boardPanel上,現在,標籤並排放置。我該如何做到這一點,而不是左右搖擺,JLabel w/name在JLabel w/img上?將JLabel放在另一個具有圖像的JLabel的頂部

有什麼我應該考慮爲包含兩個標籤的面板?像某個佈局管理器一樣?

[只是我的代碼段]

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

    } 
} 

這樣的事情,但顯然不是一隻貓,它的控制板圖像,文本是玩家的名字。

編輯

我做了一個擴展的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; 

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

} 

板框類:

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

    } 
} 

播放器史蒂文似乎還沒有出現在地圖上。

回答

3

您可以使用OverlayLayout或者您可以簡單地將第二個JLabel添加到第一個。這裏的訣竅是爲第一個標籤設置一個佈局管理器。

要小心的是,一個JLabel只能使用icontext性質決定其首選的佈局尺寸,這最終可能會截斷它的孩子

Label overlay

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

    } 

}