簡體   English   中英

如何獲得Swing組件的繪制大小?

[英]How to get the painted size of a Swing component?

當我將Swing組件(如JButton)添加到JPanel時,它會使用它的'首選大小'進行渲染。

但是,首選尺寸實際上大於繪制按鈕。 它周圍似乎有一個看不見的邊框。

這是我的測試面板的簡單框架:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

TestPanel pnl = new TestPanel();
frame.getContentPane().add(pnl);

frame.pack();
frame.setVisible(true);

這是我的測試面板......

public class TestPanel extends JPanel {

    JButton btn1 = new JButton("Test1");
    JButton btn2 = new JButton("Test2");

    public TestPanel() {
        this.add(btn1);
        this.add(btn2);
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.setColor(Color.RED);
        Dimension dim = btn1.getPreferredSize();
        g.drawRect(btn1.getX(), btn1.getY(), (int)(dim.getWidth()), (int)(dim.getHeight()));
    }

}

請注意,我在RED中繪制了btn1的“PreferredSize”,以證明preferredSize實際上大於按鈕本身。

在此輸入圖像描述

我的問題是,如何確定繪制按鈕的寬度和高度,而不是JButton的preferredSize?

非常感謝任何幫助,謝謝!

UPDATE

因為我實際上需要這個適用於所有Swing組件,這里是一個包含更多組件的屏幕截圖。

不幸的是,我需要弄清楚這一點,確定可見小部件的“真實”大小對我的應用程序至關重要。

在此輸入圖像描述

我認為這不是特別或實際可行的。

問題是,按鈕正在使用“未上漆”區域來繪制其他元素,例如焦點突出顯示。

您可以嘗試查看AbstractButton#set/getMargin

如果沒有更好的出現時,請注意,作者“建議你把組件在JPanel ,並設置邊框JPanel 。”

附錄:根據您在下面的評論,很明顯您的問題不是關於渲染邊界,而是關於建立組件的邊界。 您認為未使用空間的內容實際上是由UI代表保留用於任何數量的用途,例如選擇突出顯示或美學一致性。 通過在此處此處的示例中選擇不同的外觀和感覺主題,您可以了解這是如何變化的。

使用getbounds()

圖片

使用setBorder()

圖片

import component.Laf;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @see https://stackoverflow.com/a/15490187/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());
        // https://stackoverflow.com/a/11949899/230513
        f.add(Laf.createToolBar(f));
        f.add(decorate(new JButton("Test")));
        f.add(decorate(new JTextField("Test")));
        f.add(decorate(new JTextArea(3, 8)));
        f.add(decorate(new JCheckBox("Test")));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel decorate(final JComponent c) {
        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Rectangle r = c.getBounds();
                g.setColor(Color.red);
                // NB pen hangs down and to the right
                g.drawRect(r.x - 1, r.y - 1, r.width + 1, r.height + 1);
            }
        };
        p.add(c);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

暫無
暫無

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

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