簡體   English   中英

為什么我的JLabel不在Java GUI中的JPanel(GridBagLayout)中間?

[英]Why isn't my JLabel centering in the middle of JPanel (GridBagLayout) in Java GUI?

我正在嘗試將以下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甚至不會顯示。 怎么了? 謝謝

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM