Adding oval shape to JPanel

后端 未结 1 1134
南旧
南旧 2021-01-22 14:44

Here\'s my simple code. I don\'t really know how to add a drawn oval to a JPanel. I did some paintings before, but I have never used the constructor so I don\'t hav

1条回答
  •  逝去的感伤
    2021-01-22 15:22

    The basic structure of your code is wrong. The Buffer class should not be creating a frame. The Buffer class should just be used for painting. The code should be something like:

    public static void main(String args[])
    {
        Buffer oval = new Buffer();
        oval.setBackground(Color.RED);
    
        JFrame frame=new JFrame();
        frame.add( oval );
        frame.setSize(500,500);
        frame.setVisible(true);
    }
    

    Make sure you invoke super.paintComponent() (without the "s"). You should also be overriding the getPreferredSize() method to set the size of your custom component. Read the Swing tutorial on Custom Painting for more information and a better example.

    0 讨论(0)
提交回复
热议问题