簡體   English   中英

如何在JPanel中使用RectangularShape繪制JPanel

[英]how to get JPanel with RectangularShape in JPanel to paint

我在繪制RectangularShapes或Shapes時遇到問題。 我需要在實際的Panel中而不是在Frame中,因為我需要這種靈活性。 但我一直陷入困境。 我沒有接受任何語言的GUI培訓,因此非常感謝您的反饋。

代碼如下,請幫助我找出為什么這不起作用,以及我如何讓它工作。

public class GUI_Test 
{
    public static void main(String[] args)
    {
        GUI_Test gui = new GUI_Test();
        gui.tryBasic();
    }


    public void tryBasic()
    {
        JFrame MyFrame = new JFrame();
        MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyFrame.setSize(800, 500);
        MyFrame.setLayout(new BorderLayout());

        // This won't work and can't figure out why!!!!
        // The button appears but not the Ellipse.
        JPanel JP = new JPanel();
        JButton btn3 = new JButton("This is a button");
        JP.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red));
        JP.add(btn3);
        MyFrame.getContentPane().add(JP);

        // This works but I need it in a panel for easier control 
        //MyFrame.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.blue));

        // This of course works.
        //MyFrame.getContentPane().add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.green));

        MyFrame.setVisible(true);

    }
    public class MyGUIObject extends JPanel
    {
        protected RectangularShape RecObj;
        protected Color myColor;    
        public MyGUIObject(RectangularShape SetShape, Color setColor)
        {
            RecObj = SetShape;
            myColor = setColor;
        }
        @Override
        protected void paintComponent(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(myColor);
            g2d.fill(RecObj);
        }
    }
}

我可以看到三個主要問題。

  1. 你不是從paintComponent方法調用super.paintComponent ,這會引起一些有趣但令人討厭的油漆文物
  2. 默認情況下, JPanel使用FlowLayoutJFrame使用BorderLayout ,這進一步復雜化......
  3. 您沒有為MyGUIObject類提供任何大小調整提示,因此FlowLayout將使用JPanel的默認preferredSize ,它在計算JPanel的布局時為0x0

首先,從paintComponent方法調用super.paintComponent並覆蓋getPreferredSize方法

public class MyGUIObject extends JPanel {

    protected RectangularShape RecObj;
    protected Color myColor;

    public MyGUIObject(RectangularShape SetShape, Color setColor) {
        RecObj = SetShape;
        myColor = setColor;
    }

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

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(myColor);
        g2d.fill(RecObj);
    }
}

接下來,將JPanel使用的布局更改為BorderLayout (雖然默認的FlowLayout可以使用,但BorderLayout會給你一點控制)

JPanel JP = new JPanel(new BorderLayout());
JButton btn3 = new JButton("This is a button");
JP.add(new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red));
JP.add(btn3, BorderLayout.SOUTH);
MyFrame.getContentPane().add(JP);

看看AWT和Swing中的 繪畫執行自定義繪畫在容器中布置組件以及如何使用邊框以獲取更多詳細信息

這個怎么樣:

public class GUI_Test {
    public static void main(String[] args) {
        GUI_Test gui = new GUI_Test();
        gui.tryBasic();
    }

    public void tryBasic() {
        JFrame frame = createFrame();
        JPanel panel = new JPanel(new BorderLayout());
        JButton button = new JButton("This is a button");
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(button);
        MyGUIObject guiObject = new MyGUIObject(new Ellipse2D.Double(70, 70, 100, 100), Color.red);
        panel.add(buttonPanel, BorderLayout.PAGE_START);
        panel.add(guiObject,BorderLayout.CENTER);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setVisible(true);
    }

    private JFrame createFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 500);
        frame.setLayout(new BorderLayout());
        return frame;
    }

    public class MyGUIObject extends JPanel {
        protected RectangularShape RecObj;
        protected Color myColor;

        public MyGUIObject(RectangularShape SetShape, Color setColor) {
            RecObj = SetShape;
            myColor = setColor;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(myColor);
            g2d.fill(RecObj);
        }
    }
}

暫無
暫無

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

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