简体   繁体   English

Java简单形状程序

[英]Java Simple Shape Program

I am trying to make this simple program using GUI which creates a circle once the JButton is pressed. 我试图使用GUI创建这个简单的程序,一旦按下JButton就会创建一个圆圈。 I have been working on this and trying to figure out why this isn't working for a couples hours. 我一直在研究这个问题,并试图弄清楚为什么这对夫妻时间不起作用。 I looked at similar code on stackoverflow with people with similar questions like this, however, I still cannot figure this out. 我在stackoverflow上查看类似的代码与类似问题的人,但是,我仍然无法弄清楚这一点。 Can someone please tell me where I went wrong and why I am incorrect? 有人可以告诉我哪里出错了,为什么我不对? Thank you. 谢谢。

public class ColorShape {

    public static void main(String[] args) {

        CreatePanel c = new CreatePanel();
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setSize(300, 450);
        c.setVisible(true);

    }
}

public class CreatePanel extends JFrame {

    private JButton DC;
    private BorderLayout layout;

    public CreatePanel() {
        super("Color-Shape");
        layout = new BorderLayout();
        setLayout(layout);

        DC = new JButton("Circle");
        DC.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                DrawCirc circ = new DrawCirc();
                add(circ);
                repaint();
            }
        }
        );
        add(DC, BorderLayout.NORTH);

    }
}

public class DrawCirc extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.cyan);
        g.fillOval(100, 100, 50, 50);

    }
}

Well, your first problem is, DrawCirc provides no sizing hints, which means it's default size is going to be 0x0 好吧,你的第一个问题是, DrawCirc没有提供大小调整提示,这意味着它的默认大小将是0x0

public class DrawCirc extends JPanel {

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

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.cyan);
        g.fillOval(100, 100, 50, 50);
    }
}

Also, remember, the Graphics context is translated so that 0x0 is the top left corner of the component. 另外,请记住, Graphics上下文已被翻译,因此0x0是组件的左上角。

The second problem is, Swing is lazy. 第二个问题是,Swing很懒。 It allows you to make a number of changes to the UI and then batch process them. 它允许您对UI进行一些更改,然后批量处理它们。 This means that when you've finished updating the UI, you have to call both revalidate and repaint to trigger a layout and paint pass 这意味着当您完成UI的更新后,您必须同时调用revalidaterepaint以触​​发布局和绘制传递

DC.addActionListener(
        new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        DrawCirc circ = new DrawCirc();
        add(circ);
        revalidate();
        repaint();
    }
});

Both of these issues are not uncommon. 这两个问题并不罕见。 You should spend some more time understanding the layout management system as it will make your life simpler ;) 您应该花更多时间了解布局管理系统,因为它会让您的生活更简单;)

Change repaint() to revalidate() after changing the component hierarchy. 更改组件层次结构后,将repaint()更改为revalidate() You'll notice that your current version paints the circle if you resize the window, because this revalidates the layout. 如果调整窗口大小,您会注意到当前版本会绘制圆圈,因为这会重新验证布局。

From the docs: 来自文档:

Revalidates the component hierarchy up to the nearest validate root. 将组件层次结构重新验证到最近的验证根。 This method first invalidates the component hierarchy starting from this component up to the nearest validate root. 此方法首先使从此组件开始的组件层次结构无效,直至最近的验证根。 Afterwards, the component hierarchy is validated starting from the nearest validate root. 然后,从最近的验证根开始验证组件层次结构。 This is a convenience method supposed to help application developers avoid looking for validate roots manually. 这是一种方便的方法,可以帮助应用程序开发人员避免手动查找验证根。 Basically, it's equivalent to first calling the invalidate() method on this component, and then calling the validate() method on the nearest validate root. 基本上,它相当于首先在此组件上调用invalidate()方法,然后在最近的验证根上调用validate()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM