【问题标题】:Why isn't my JLabel centering in the middle of JPanel (GridBagLayout) in Java GUI?为什么我的 JLabel 不在 Java GUI 中的 JPanel (GridBagLayout) 中间居中?
【发布时间】:2014-10-15 05:47:13
【问题描述】:

我正在尝试将以下 JLabel 添加到 JPanel 的中心:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;

public class DrawingPanel extends JLabel {

    protected void paintComponent(Graphics g){

        super.paintComponent(g);

        int[] xpoints = {230, 270, 290, 290, 270, 230, 210, 210};
        int[] ypoints = {37, 37, 87, 115, 165, 165, 115, 87};

        g.setColor(Color.white);
        g.fillPolygon(xpoints, ypoints, 8 );    
    }
}

致以下JPanel

JPanel jp = new JPanel(new GridBagLayout());
DrawingPanel dp = new DrawingPanel();
jp.add(dp);

但是DrawingPanel JPanel 甚至不会出现。有什么问题?谢谢

【问题讨论】:

  • 考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
  • GridBagLayout 使用GridBagConstraints 来管理定位。创建一个GridBagConstraints 对象,并在添加面板时添加它:jp.add(dp, gbc)
  • 您的DrawingPanel 没有定义大小的详细信息,因此布局管理器已将其大小调整为0x0 以供初学者使用...
  • 你的“形状”不会居中,因为,嗯......它不是。您的坐标应该根据已知值(如组件的宽度和高度)计算,而不是您编造的一些幻数......另外,为什么从JLabel...扩展?
  • @MadProgrammer 为什么没有定义大小?它有坐标。这些不应该是大小吗?

标签: java swing user-interface graphics layout-manager


【解决方案1】:
  1. 没有任何大小提示,您的组件将自动调整为 0x0,因为这是默认大小
  2. 您的输出不会居中,因为,嗯...不是。您使用了一堆与现实世界无关的“神奇”数字。你怎么知道你的组件将是给定的大小?
  3. 组件的绘制方法中的所有坐标和组件的上下文,即左上角是0x0...
  4. 为什么要从JLabel 扩展? JLabel 有足够复杂的功能,无需您尝试添加它...

您应该根据已知值进行计算,例如组件的宽度和高度,您还应该提供一些您希望组件在最佳情况下的首选尺寸的想法,例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

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.setLayout(new GridBagLayout());
                frame.add(new DrawingPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DrawingPanel extends JPanel {

        public DrawingPanel() {
            setBorder(new EmptyBorder(8, 8, 8, 8));
        }

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

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            Insets insets = getInsets();
            g2d.translate(insets.top, insets.left);

            int width = getWidth() - 1 - (insets.left + insets.right);
            int height = getHeight() - 1 - (insets.top + insets.bottom);

            int vHalf = height / 2;
            int hHalf = width / 2;
            int vPos = vHalf / 4;
            int hPos = hHalf / 4;

            int[] xpoints = {
                0,
                hHalf - hPos,
                hHalf + hPos,
                width,
                width,
                hHalf + hPos,
                hHalf - hPos,
                0
            };
            int[] ypoints = {
                vHalf - vPos,
                0,
                0, 
                vHalf - vPos,
                vHalf + vPos,
                height,
                height,
                vHalf + vPos,
            };

            g2d.setColor(Color.BLACK);
            g2d.drawPolygon(xpoints, ypoints, xpoints.length);
            g2d.dispose();
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 2013-07-24
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2011-11-08
    相关资源
    最近更新 更多