Java Swing,看不到错误

发布于 2024-12-14 03:02:16 字数 317 浏览 1 评论 0原文

我遇到一个非常奇怪的问题,我有一个定制的 JPanel,我想画一个圆圈,但什么也没发生...这是我的来源,希望有人看到错误,我找不到它。

import javax.swing.JPanel;

public class CircleView extends JPanel {

public CircleView() {}

@Override
    public void paintComponent(Graphics g) {
    g.setColor(Color.red);
    g.drawOval(10, 10, 50, 50);
    }
}

I am at a very strange problem, I have a customized JPanel, which I want to draw a circle, but nothing happens ... here is my source, hope somebody sees the mistake, I can't find it.

import javax.swing.JPanel;

public class CircleView extends JPanel {

public CircleView() {}

@Override
    public void paintComponent(Graphics g) {
    g.setColor(Color.red);
    g.drawOval(10, 10, 50, 50);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-21 03:02:16

这根本不是真的

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at KondorExport.Util.Helping.CustomComponent12.<init>(CustomComponent12.java:19)
        at KondorExport.Util.Helping.CustomComponent12$1.run(CustomComponent12.java:37)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.RuntimeException: Uncompilable source code - class CircleView is public, should be declared in a file named CircleView.java
        at KondorExport.Util.Helping.CircleView.<clinit>(CustomComponent12.java:44)
        ... 10 more

1) 删除构造函数

2) 添加 super.paintComponent(g);

这个可以运行

class CircleView extends JPanel {

    private static final long serialVersionUID = 1L;

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

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawOval(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

that not true at all

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at KondorExport.Util.Helping.CustomComponent12.<init>(CustomComponent12.java:19)
        at KondorExport.Util.Helping.CustomComponent12$1.run(CustomComponent12.java:37)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.RuntimeException: Uncompilable source code - class CircleView is public, should be declared in a file named CircleView.java
        at KondorExport.Util.Helping.CircleView.<clinit>(CustomComponent12.java:44)
        ... 10 more

1) remove constructor

2) add super.paintComponent(g);

this one can run

class CircleView extends JPanel {

    private static final long serialVersionUID = 1L;

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

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawOval(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}
桃扇骨 2024-12-21 03:02:16

这是因为您的组件没有尺寸,因此为什么 @mKorbel 提供 sscce 在定义组件的首选大小时使用一些“神奇”尺寸。

It's because your component has no dimensions, hence why the sscce provided by @mKorbel uses some "magic" dimensions when defining the component's preferred size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文