简单的自定义 Swing JComponent 始终平坦

发布于 2024-12-04 14:37:14 字数 613 浏览 1 评论 0原文

我只是在玩 Swing,并且正在开发一个非常简单的 Swing 组件。我有一个继承自 JComponent 类的组件,其 UI 继承自 ComponentUIpaint() 方法如下所示:

public void paint(Graphics g, JComponent c) {
    int x = c.getX();
    int y = c.getY();
    c.setBounds(x, y, 100, 25);
    int width = c.getWidth();
    int height = c.getHeight();
    Rectangle r = g.getClipBounds();
    g.fillRect(0, 0, 10, 10);
    g.drawString("Baf!", 3, 3);
}

但是完全不可能获得除 1 之外的另一个 r.height 值。组件的宽度为给定的,但高度始终为 1仅点。还有其他人使用类似组件的经验吗?不幸的是没有任何简单的教程。所有教程都非常复杂(或过时)。

布局管理器似乎总是将该组件的大小调整为 1 高度(无论最小值)。

I'm just playing with Swing and I'm working on a really simple Swing component. I have a component inherited from JComponent class and its UI inherited from ComponentUI. The paint() method looks like this:

public void paint(Graphics g, JComponent c) {
    int x = c.getX();
    int y = c.getY();
    c.setBounds(x, y, 100, 25);
    int width = c.getWidth();
    int height = c.getHeight();
    Rectangle r = g.getClipBounds();
    g.fillRect(0, 0, 10, 10);
    g.drawString("Baf!", 3, 3);
}

But it is totally impossible to get another value of r.height than 1. The component is width as given, but height allways one point only. Has anybody else experiences with suchlike components? Unfortunately there is no any easy tutorial. All tutorials are incomprehensible complicated (or obsolete).

It seems, that the layout manager resizes this component allways to 1 height (regardless to minimal value).

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

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

发布评论

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

评论(2

不必你懂 2024-12-11 14:37:14

切勿在绘画方法中调用 setBound()。这是布局管理器的工作,而不是绘画代码的工作。

我猜想主要问题(除了Heisenbug的观点)是你没有给你的组件一个大小。这是通过重写 getPreferredSize() 以返回适合您的组件的大小来完成的。

有关详细信息,请阅读 Swing 教程中关于 自定义绘制 的部分和工作示例。

Never invoke setBound() in a painting method. That is a job for the layout manager, not your painting code.

I would guess the main problem (other than Heisenbug's points) are that you don't give you component a size. This is done by overriding the getPreferredSize() to return a size appropriate to your component.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

本宫微胖 2024-12-11 14:37:14

您的代码存在几个问题:

对于扩展 JComponent 的类,有什么问题:

  1. public void Paint(Graphics g, JComponent c) {}

    不是有效的签名,因此您不会覆盖方法paint,而是创建一个新的paint方法。

  2. 您应该重写paintComponent(Graphics g)方法而不是paint。

  3. 因为您要扩展 JComponent,所以您需要首先在重写的 PaintComponent 方法中调用 super.paintComponent(g):

    公共类 JPanelExtended{
    公共无效paintComponent(图形g){
    super.paintComponent(g);
    ...
    }
    }

,您甚至应该显式调用超类上的 Paint 方法:

public void paint(Graphics g, JComponent c) {
    super.paint(g,c);
}

编辑:
一个小建议:当你想重写一个方法时,在签名之前放置 @override 符号非常有用:

@Override
public void superMethodToBeOverridden(){}

这样,如果你正在定义一个新方法而不是定义一个新方法,编译器将通过错误消息通知你覆盖现有的。

There are several problems with your code:

For what concern the class extending JComponent:

  1. public void paint(Graphics g, JComponent c) {}

    isn't a valid signature so you are not overriding the method paint, but create a new paint method.

  2. you should override paintComponent(Graphics g) method instead of paint.

  3. Because of you are extending a JComponent you need first call super.paintComponent(g) inside your overridden paintComponent method:

    public class JPanelExtended{
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    ...
    }
    }

For what concern the class extending ComponentUI, you should even there call explicitly the method paint on the super class:

public void paint(Graphics g, JComponent c) {
    super.paint(g,c);
}

EDIT:
a little suggestion: when you want to override a method, it's quite useful to put the @override notation before the signature:

@Override
public void superMethodToBeOverridden(){}

This way you will be notified by the compiler with an error message, in the case you are defining a new method and not overriding an existing one.

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