GridBagLayout未正确对齐图像

use*_*678 6 java swing

最初我有一个甲板图像和文本"甲板"正好在图像下方看起来很好

PIC1

public class GuiTut extends JPanel { 
   private GridBagConstraints c = new GridBagConstraints();
   private JLabel deckLabel = new JLabel();

   public GuiTut() {
        setLayout(new GridBagLayout());  

        try {
            deck = ImageIO.read(new File("resources/images/deck.jpg"));  
        } catch (Exception e) {}   

        c.gridx = 0;
        c.gridy = 0;
        JLabel deckPic = new JLabel(new ImageIcon(deck));    
        add(deckPic, c);

        deckLabel.setText("Deck");
        c.gridx = 0;
        c.gridy = 1;
        add(deckLabel, c);
}
Run Code Online (Sandbox Code Playgroud)

但是在我添加我的gridLayout面板后,我的整个GUI设计已经搞砸了.正如您所看到的,我的deck图像与gridLayout的第一行没有正确对齐,而我的文本"deck"已经被几个宽阔的空间隔开.

PIC2

public class GuiTut extends JPanel { 
   private GridBagConstraints c = new GridBagConstraints();
   private JLabel deckLabel = new JLabel();
   private JPanel gridLayoutPanel = new JPanel(new GridLayout(2, 0));
   private JLabel[] label = new JLabel[14];

   public GuiTut() {
        setLayout(new GridBagLayout());  

        try {
            card = ImageIO.read(new File("resources/images/card.jpg"));  
        } catch (Exception e) {} 

        for(int i = 0; i < 14; i++) {   
            label[i] = new JLabel(new ImageIcon(card);
            gridLayoutPanel.add(label[i]);
        }

        try {
            deck = ImageIO.read(new File("resources/images/deck.jpg"));  
        } catch (Exception e) {}   

        c.gridx = 0;
        c.gridy = 0;
        JLabel deckPic = new JLabel(new ImageIcon(deck));    
        add(deckPic, c);

        deckLabel.setText("Deck");
        c.gridx = 0;
        c.gridy = 1;
        add(deckLabel, c);

        c.gridx = 1;
        c.gridy = 0;
        add(gridLayoutPanel, c);
}
Run Code Online (Sandbox Code Playgroud)

我想要的是甲板图像与甲板图像正下方的第一行gridLayout和文本对齐,"deck"但我似乎无法得到它.

Pau*_*tha 3

热链接图像

在此输入图像描述在此输入图像描述

  1. 对于卡片组标签,您可以只设置标签的文本并设置垂直/水平文本位置

    JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL)));
    label.setText("DECK");
    label.setHorizontalTextPosition(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.BOTTOM);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对于甲板定位问题,您可以使用不同的布局管理器,例如用于外部面板的 BorderLayout,以及用于甲板面板的默认流布局。卡片仍将是 GridLayout。当您需要时,嵌套面板通常很有帮助

例子

在此输入图像描述

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GuiTest {

    private static final String DECK_URL = "https://i.stack.imgur.com/xNffR.png";
    private static final String CARD_URL = "https://i.stack.imgur.com/uVS1D.png";

    private static JPanel createDeckPanel() {
        JPanel panel = new JPanel();
        try {
            JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL)));
            label.setText("DECK");
            label.setHorizontalTextPosition(JLabel.CENTER);
            label.setVerticalTextPosition(JLabel.BOTTOM);
            panel.add(label);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return panel;
    }

    private static JPanel createCenterPanel(int rows, int cols) {
        JPanel panel = new JPanel(new GridLayout(rows, cols));
        for (int i = 0; i < rows*cols; i++) {
            try {
                panel.add(new JLabel(new ImageIcon(new URL(CARD_URL))));
            } catch (Exception ex) {
                ex.printStackTrace();
            }   
        }
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(createDeckPanel(), BorderLayout.LINE_START);
                panel.add(createCenterPanel(2, 6));
                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)