如何在JPanel中使用RectangularShape绘制JPanel

时间:2015-06-27 08:13:00

标签: java swing awt

我遇到绘制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);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我可以看到三个主要问题。

  1. 你没有从你的paintComponent方法中调用JPanel,这会导致一些有趣但令人讨厌的油漆文物
  2. FlowLayout默认情况下使用JFrameBorderLayout使用MyGUIObject,这会更加复杂......
  3. 您没有为FlowLayout课程提供任何大小调整提示,因此JPanel将使用preferredSize的默认0x0,即JPanel当它计算super.paintComponent
  4. 的布局时

    首先,从您的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); 会给你更多控制权)

    2&>1

    有关详细信息,请查看Painting in AWT and SwingPerforming Custom PaintingLaying Out Components Within a ContainerHow to Use Borders

答案 1 :(得分:1)

这个怎么样:

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