Java - JLabel图标未对齐(向右)

时间:2014-07-08 14:10:05

标签: java swing jtable jlabel

我正在尝试为纸牌游戏(Yu-Gi-Oh:D)制作一个卡片管理员,现在我只有一张带有可用卡片的桌子和一个显示用户选择的卡片的面板更大的尺寸和卡的描述。 MVCE:

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

    public class SelectedCardPanel extends JPanel{
        private final JLabel cardArea;
        private final JTextArea cardInfo;

        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Yu-Gi-Oh!");
                    frame.add(new SelectedCardPanel());
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }

        public SelectedCardPanel(){
            super();

            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            setPreferredSize(new Dimension(200, 400));

            cardArea = new JLabel("                                 ");
            cardArea.setOpaque(true);
            cardArea.setBackground(Color.white);
            cardArea.setPreferredSize(new Dimension(200, 300));
            cardArea.setSize(cardArea.getPreferredSize());
            cardArea.setBorder(BorderFactory.createLineBorder(Color.black));

            cardInfo = new JTextArea();
            cardInfo.setEditable(false);
            cardInfo.setWrapStyleWord(true);
            cardInfo.setLineWrap(true);
            cardInfo.setPreferredSize(new Dimension(200, 100));
            cardInfo.setBorder(BorderFactory.createLineBorder(Color.black));

            add(cardArea);
            add(cardInfo);
        }

        public final void setImage(ImageIcon icon){
            cardArea.setIcon(icon);
        }
    }

但是那会发生什么: deck manager 在MVCE中:

label to the right

cardArea向右移动,我不明白为什么。

我在表格中选择的卡片对所选的卡片面板没有任何问题。 为什么JLabel会向右移动?

1 个答案:

答案 0 :(得分:1)

问题在于BoxLayout。我建议使用BorderLayout并删除所有set[Preferred]Sizes。对于文本区域,请使用构造函数JTextArea(rows, cols)。对于列,您可以保留0,使用BorderLayout,它将拉伸以匹配图像的宽度

enter image description here

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

public class SelectCardPanel extends JPanel{
    private final JLabel cardArea;
    private final JTextArea cardInfo;

    public static void main(String args[]){
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Yu-Gi-Oh!");
                SelectCardPanel panel = new SelectCardPanel();
                panel.setImage(new ImageIcon(SelectCardPanel.class.getResource("images.jpg")));
                frame.add(panel);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    public SelectCardPanel(){
        super();

        setLayout(new BorderLayout());
        //setPreferredSize(new Dimension(200, 400));

        cardArea = new JLabel();
        cardArea.setOpaque(true);
        cardArea.setBackground(Color.white);
        //cardArea.setPreferredSize(new Dimension(200, 300));
        //cardArea.setSize(cardArea.getPreferredSize());
        cardArea.setBorder(BorderFactory.createLineBorder(Color.black));

        cardInfo = new JTextArea(5, 0);
        cardInfo.setEditable(false);
        cardInfo.setWrapStyleWord(true);
        cardInfo.setLineWrap(true);
        //cardInfo.setPreferredSize(new Dimension(200, 100));
        cardInfo.setBorder(BorderFactory.createLineBorder(Color.black));

        add(cardArea);
        add(cardInfo, BorderLayout.PAGE_END);
    }

    public final void setImage(ImageIcon icon){
        cardArea.setIcon(icon);
    }
}