无法同时显示两个JLabel

时间:2015-07-13 22:28:22

标签: java swing jlabel layout-manager gridbaglayout

因为我花了好几天才找到解决方案,所以我冒风险看起来很荒谬。 我试图在GridBagConstraints中添加和显示两个JLabel。似乎第二个JLabel覆盖了第一个。我想把所有JLabel都放在一个容器里,但是我无法做到这一点

这就是我得到的: enter image description here

这就是我想要的原因:enter image description here

以下代码可以运行以重现问题

package myproblem;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
public class MyProblem {
public int sizeSquare = 50;

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

public MyProblem() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }
            JFrame frame = new JFrame("myProblem");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new TestPb());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

public class TestPb extends JPanel {

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

        GridBagConstraints gbc = new GridBagConstraints();
        Border border = new MatteBorder(2, 2, 2, 2, Color.BLACK);
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                gbc.gridx = col;
                gbc.gridy = row;
                FillSquare square = new FillSquare(sizeSquare);
                square.setBorder(border);
                square.setBackground(Color.RED);

                square.addLabel("45", "28");
                add(square, gbc);
            }
        }
    }
}

public class FillSquare extends JPanel {

    public int sizeSquare;
    public JLabel labelRight;
    public JLabel labelLeft;
    public JLabel label;

    public FillSquare(int sizeSquare) {

        this.sizeSquare = sizeSquare;
        this.labelRight = new JLabel("", SwingConstants.RIGHT);
        this.labelLeft = new JLabel("", SwingConstants.LEFT);
        this.label = new JLabel();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(sizeSquare, sizeSquare);
    }

    public void addLabel(String number1, String number2) {
        add(addLabelLeft(number1));
        add(addLabelRight(number2));
    }

    public JLabel addLabelRight(String number) {
        this.labelRight.setText(number);
        labelRight.setPreferredSize(new Dimension(sizeSquare * 80 / 100, sizeSquare * 40 / 100));
        labelRight.setFont(new Font("Arial", 0, sizeSquare * 40 / 100));
        labelRight.setAlignmentY(SwingConstants.TOP);
        return labelRight;
    }

    public JLabel addLabelLeft(String number) {
        this.labelLeft.setText(number);
        labelLeft.setPreferredSize(new Dimension(25, 70));
        labelLeft.setFont(new Font("Arial", 0, sizeSquare * 40 / 100));
        labelLeft.setAlignmentY(SwingConstants.BOTTOM);
        return labelLeft;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(2));
        Line2D line = new Line2D.Double(0, 0, 100, 100);
        g2d.draw(line);
    }

}
}

任何帮助和建议将不胜感激

0 个答案:

没有答案