简体   繁体   English

Java Swing-为什么JComponent无法显示?

[英]Java Swing - Why JComponent won't display?

I'm trying to extend a class to eventually make a custom button. 我正在尝试扩展一个类,以最终创建一个自定义按钮。 I read in several places that it's best to extend as high as possible on the heirarchy (for better polymorphism?), so I'm trying to extend JComponent : 我在几个地方读到,最好在层次结构上尽可能扩展(为了更好的多态性?),所以我尝试扩展JComponent

import javax.swing.*;
import java.awt.*;

public class TestButton extends JComponent {
    private static final long serialVersionUID = 1L;


    public TestButton() {
        super();        
    }

}

And the code that calls this is: 调用此代码是:

 b1 = new TestButton();     
 basePanel.add(b1,gbc);  // (gbc is GridBagConstraints object)

The thing is, my JComponent isn't displayed in my layout. 问题是,我的JComponent未显示在布局中。 If I extend the class as JButton , it shows no problem. 如果将类扩展为JButton ,则不会出现问题。 What's the deal? 这是怎么回事?

Update: 更新:

FYI, this is sort of a noob conceptual question, I'm far from proficient here obviously. 仅供参考,这是一个概念上的菜鸟问题,显然我在这里还远远不够熟练。

Here's a picture to describe. 这是一张描述的图片。 The only thing changed is extends ______ . 唯一改变的是extends ______

What should be happening is a purple-filled block, the same height as the yellow block on the bottom. 应该发生的是一个紫色填充的块,其高度与底部的黄色块相同。

What is happening is a default sized block that has no background (the black is from the JFrame). 发生的是一个没有背景的默认大小的块(黑色来自JFrame)。

在此处输入图片说明

One of the main differences between JComponent and it's subclasses is that the latter have UI delegates, while JComponent does not. JComponent及其子类之间的主要区别之一是后者具有UI委托,而JComponent没有。 Note that the setBackground() "color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations." 请注意, setBackground() “仅当组件不透明时才使用颜色,并且仅由JComponentComponentUI实现的子类使用。” As a result, you "must override paintComponent() to honor this property." 结果,您“必须重写paintComponent()才能使用此属性”。

What do you expect to see in the place where JComponent is supposed to be? 您希望在应该存在JComponent的地方看到什么? when you extend JButton , you get all its graphic with it, but you created an empty component, with nothing in it. 扩展JButton ,将获得其所有图形,但是创建了一个空组件,其中没有任何内容。 Try putting something in the component (such as a JLabel or similar) 尝试在组件中放置一些东西(例如JLabel或类似的东西)

You still need to extends JButton if you want JButton 's functionality. 如果需要JButton的功能,则仍然需要扩展JButton Otherwise everybody can extend Object and expect everything. 否则,每个人都可以扩展Object并期望得到一切。

It is a blank component (basically a template). 它是一个空白组件(基本上是模板)。 It has no properties. 它没有属性。 You have to add your own graphical elements by overriding the paintComponent method and then add logical elements by overriding the update method. 您必须通过覆盖paintComponent方法来添加自己的图形元素,然后通过覆盖update方法来添加逻辑元素。

Either use the JButton as it is: 按原样使用JButton:

 JButton b1 = new JButton("Enter JButton text here");     
 basePanel.add(b1,gbc); 

or extend the JButton class if you wish to change certain properties and customize it for your own preferences: 或扩展JButton类(如果您希望更改某些属性并根据自己的喜好自定义它):

public class TestButton extends JButton {
    private static final long serialVersionUID = 1L;


    public TestButton(String buttonText) 
    {
        super(buttonText);        
    }

}

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

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