简体   繁体   English

如何在JPanel中使用RectangularShape绘制JPanel

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

I'm having an issue with drawing RectangularShapes Or Shapes. 我在绘制RectangularShapes或Shapes时遇到问题。 I need to have this within the actual Panel instead of within the Frame because I need that kind of flexibility. 我需要在实际的Panel中而不是在Frame中,因为我需要这种灵活性。 But I keep on getting stuck. 但我一直陷入困境。 I haven't been well trained in GUI with any language so feedback is much appreciated. 我没有接受任何语言的GUI培训,因此非常感谢您的反馈。

Code is below, Please help me find out why this isn't working, and how I can get it to work. 代码如下,请帮助我找出为什么这不起作用,以及我如何让它工作。

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

There are three main problems I can see. 我可以看到三个主要问题。

  1. You're not calling super.paintComponent from your paintComponent method, this is going to cause some interesting, but nasty paint artefacts 你不是从paintComponent方法调用super.paintComponent ,这会引起一些有趣但令人讨厌的油漆文物
  2. JPanel use a FlowLayout by default, where the JFrame uses a BorderLayout , this is further complicated by... 默认情况下, JPanel使用FlowLayoutJFrame使用BorderLayout ,这进一步复杂化......
  3. You don't provide any sizing hints for the MyGUIObject class, so the FlowLayout will use the JPanel 's default preferredSize , which is 0x0 when it calculates the layout for the JPanel . 您没有为MyGUIObject类提供任何大小调整提示,因此FlowLayout将使用JPanel的默认preferredSize ,它在计算JPanel的布局时为0x0

First, call super.paintComponent from your paintComponent method and override the getPreferredSize method 首先,从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);
    }
}

Next, change the layout that the JPanel is using to something like BorderLayout (although the default FlowLayout will work, BorderLayout will give you a little more control) 接下来,将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);

Have a look at Painting in AWT and Swing , Performing Custom Painting , Laying Out Components Within a Container and How to Use Borders for more details 看看AWT和Swing中的 绘画执行自定义绘画在容器中布置组件以及如何使用边框以获取更多详细信息

How about this: 这个怎么样:

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