创建基于另一个但具有不同API的Swing组件 [英] Create a Swing component based on another but with different API

查看:85
本文介绍了创建基于另一个但具有不同API的Swing组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于现有的Swing JComponent创建一个新的Swing JComponent,但是使用不同的API。换句话说,我不想扩展现有的组件,因为我不希望它的API可以访问。

I would like to create a new Swing JComponent based on an existing one, but with a different API. In other words, I don't want to extend the existing component, because I don't want it's API to be accessible.

这里有一个例子来澄清我的需求:

Here an example to clarify my needs:

替换 JCheckBox ,它显示两个按钮ON / OFF。这可以基于预先配置的 JCommandButtonStrip (某些信息这里)但是公开了与 JCheckBox完全相同的API 。不得更改 JCommandButtonStrip 的配置。

A replacement of the JCheckBox which show two buttons ON/OFF. This could be based on a pre-configured JCommandButtonStrip (some info here) but exposing exactly the same API of JCheckBox. The configuration of the JCommandButtonStrip must not be altered.

出现此类问题的最佳方法是什么?

What is the best approach for such a problem?

澄清:

有人指出,我写的关于API的内容并不清楚。

As someone pointed out, what I wrote about API is not clear.

当然 JComponent 有许多公共字段和方法可供每个子类使用。然后 JComponent 的每个子类可以添加自己的公共字段和方法。例如, AbstractButton 添加 isSelected()方法,而 JCommandButtonStrip 添加 getButtonCount()方法。

Of course JComponent have a number of public fields and methods which will be available for each sub-class. Then each sub-class of JComponent may add its own public fields and methods. For example, AbstractButton adds the isSelected() method, while JCommandButtonStrip adds the getButtonCount() method.

所以,我的意思是:我想创建一个新的 JComponent 子类 MyJComponent ,它基于现有的 ExistingJComponent 。我不希望 ExistingJComponent 的公共方法,除了 JComponent 的公共方法,由我的类<$公开C $ C> MyJComponent 。然后我想为 MyJComponent 添加一些公共方法。

So, what I meant is: I want to create a new JComponent sub-class MyJComponent, which is based on an existing one ExistingJComponent. I don't want the public methods of ExistingJComponent, except those of JComponent, to be exposed by my class MyJComponent. Then I want to add some public methods to MyJComponent.

请注意我不是在寻找替代方案到 JCommandButtonStrip / JCheckBox 示例。我对这种问题的一般方法很感兴趣。

Please note that I'm not looking for an alternative to the JCommandButtonStrip/JCheckBox example. I'm interested in a general approach to such a problem.

推荐答案

你可以创建一个新的类来扩展JComponent然后在构造函数在自身中插入一个复选框。

You can create a new class which extends JComponent then inside the constructor insert a checkbox into itself.

public class MyCoolCheckbox extends JComponent{
    private JCheckBox checkbox;
    public MyCoolCheckbox(String label) {
        checkbox= new JCheckBox(label);
        this.setLayout(new BorderLayout());
        this.add(checkbox, BorderLayout.CENTER);
    }
}

这显然是不完整的,您可能需要委托某些给孩子的方法。它可能会变得混乱。像IntelliJ IDEA这样的IDE会为你生成所有这些,如果你点击alt-ins(默认情况下)然后委托,然后选择复选框成员并选择你想委派的条目。例如:

This is obviously incomplete and you may need to delegate certain methods to the child. It might get messy. IDEs like IntelliJ IDEA will generate all this for you if you hit alt-ins (by default) then delegate, then select the checkbox member and pick the entries you want to delegate. For example:

public void setForeground(Color fg) {
    checkbox.setForeground(fg);
}

public void setBackground(Color bg) {
    checkbox.setBackground(bg);
}
public Color getForeground() {
    return checkbox.getForeground();
}

public Color getBackground() {
    return checkbox.getBackground();
}

请记住,因为孩子在Swing组件树中,其他代码即使他们被标记为私人,也可以访问孩子。

Keep in mind that because the child is within the Swing component tree, other code will have access to the children even though they are marked private.

((JCheckBox)myCoolCheckbox.getComponents()[0]).setSelected(true);

这篇关于创建基于另一个但具有不同API的Swing组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆