将多个对象绘制到 JFrame

Drawing more than one object to JFrame

我尝试在同一个 JFrame 面板中绘制 2D 对象。

我写了这样的代码,但说实话,它并没有像我预期的那样工作,我猜是在添加第二个对象时,它破坏了前一个对象。

我一直在寻找解决方案,但我找不到任何东西:(

有没有人可以帮助我?

        public class Demo1 {
        
            public static void main(String[] args) {
                new Demo1();
            }
    
        public Demo1() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
                    TestPane objA = new TestPane(200, 300);
                    TestPane objB = new TestPane();
                    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(objA);
                    frame.add(objB);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
            int x = 0;
            int y = 0;
            int h = 0;
    
            public TestPane(int xa, int ya) {
                x = xa;
                y = ya;
            }
    
            public TestPane() {}
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 800);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                int width = x;
                int height = y;
    
                if ((x == 0) && (y == 0)) {
                    width = getWidth();
                    height = getHeight();
                }
    
                g2d.setColor(new Color(139, 69, 19));
                g2d.fillRect((width / 2) - 20, height / 2, 40, height / 2);
    
                g2d.setColor(Color.GREEN);
                int radius = 60;
                g2d.fillOval((width / 2) - radius, (height / 2) - (radius * 2), radius * 2, radius * 2);
                g2d.fillOval((width / 2) - radius, (height / 2) - radius, radius * 2, radius * 2);
                g2d.fillOval((width / 2) - (radius * 2), (height / 2) - radius, radius * 2, radius * 2);
                g2d.fillOval((width / 2), (height / 2) - radius, radius * 2, radius * 2);
                g2d.dispose();
            }
        }
    }

更新: 经过这些修改后,它可以工作 JFrame frame = new JFrame("测试");

            JPanel mainframe = new JPanel();

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainframe.add(objA);
            mainframe.add(objB);
            frame.getContentPane().add(mainframe);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

答案是user15358848写的。如果直接将它添加到 JFrame,它们最终会位于 CENTER,它只能包含一个组件。

如果你想在CENTER区域有多个组件,只需将它们嵌套在一个JPanel中(基本上是在所有从Container派生的东西中,但JPanel或子类是最合理的)并将那个JPanel添加到CENTER区域. 确保向其中添加符合您需求的布局,例如这两个组件彼此并排或彼此重叠。 JPanel 的默认布局是 FlowLayout。您可以使用 setLayout(LayoutManager) 更改它。 您应该能够使用这些关键字在网络上找到很多完整的示例。 JFC / Swing 提供了很多可能性。玩得开心!