按名称获取Swing组件 [英] Get a Swing component by name

查看:108
本文介绍了按名称获取Swing组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JFrame 我想要
的一些组件引用另一个 JFrame 和我希望
按名称获取,而不是
为每个获取公共get / set方法。

I have in a JFrame some components that I want to refer into another JFrame and I want to get them by name and not do public get/set methods for each.

是否有一种方法可以从Swing获取组件引用名称如do
c#?

Is there a way from Swing to get a component reference by its name like do c#?

例如 form.Controls [text]

谢谢

推荐答案

我知道这是一个老问题,但我发现自己现在才问这个问题。我想要一种简单的方法来按名称获取组件,这样我就不必每次都能编写一些复杂的代码来访问不同的组件。例如,让JButton访问文本字段中的文本或List中的选择。

I know this is an old question, but I found myself asking it just now. I wanted an easy way to get components by name so I didn't have to write some convoluted code each time to access different components. For example, having a JButton access the text in a text field or a selection in a List.

最简单的解决方案是使所有组件变量成为类变量,你可以在任何地方访问它们。但是,不是每个人都想这样做,有些人(比如我自己)正在使用不会将组件生成为类变量的GUI编辑器。

The easiest solution is to make all of the component variables be class variables so that you can access them anywhere. However, not everyone wants to do that, and some (like myself) are using GUI Editors that don't generate the components as class variables.

我的解决方案很简单,我想,并且并没有真正违反任何编程标准,据我所知(参考fortran所得到的)。它允许以简单直接的方式按名称访问组件。

My solution is simple, I'd like to think, and doesn't really violate any programming standards, as far as I know (referencing what fortran was getting at). It allows for an easy and straightforward way to access components by name.


  1. 创建Map类变量。您至少需要在
    导入HashMap。为简单起见,我将我的命名为componentMap。

  1. Create a Map class variable. You'll need to import HashMap at the very least. I named mine componentMap for simplicity.

private HashMap componentMap;


  • 正常将所有组件添加到框架中。

  • Add all of your components to the frame as normal.

    initialize() {
        //add your components and be sure
        //to name them.
        ...
        //after adding all the components,
        //call this method we're about to create.
        createComponentMap();
    }
    


  • 在您的班级中定义以下两种方法。你还需要导入Component:

  • Define the following two methods in your class. You'll need to import Component if you haven't already:

    private void createComponentMap() {
            componentMap = new HashMap<String,Component>();
            Component[] components = yourForm.getContentPane().getComponents();
            for (int i=0; i < components.length; i++) {
                    componentMap.put(components[i].getName(), components[i]);
            }
    }
    
    public Component getComponentByName(String name) {
            if (componentMap.containsKey(name)) {
                    return (Component) componentMap.get(name);
            }
            else return null;
    }
    


  • 现在你有一个HashMap可以映射现有的所有HashMap框架/内容窗格/面板/等中的组件各自的名称。

  • Now you've got a HashMap that maps all the currently existing components in your frame/content pane/panel/etc to their respective names.

    现在访问这些组件,就像调用getComponentByName一样简单(字符串名称)。如果存在具有该名称的组件,则它将返回该组件。如果不是,则返回null。您有责任将组件转换为正确的类型。我建议使用instanceof来确定。

    To now access these components, it is as simple as a call to getComponentByName(String name). If a component with that name exists, it will return that component. If not, it returns null. It is your responsibility to cast the component to the proper type. I suggest using instanceof to be sure.

    如果您计划在任何时候添加,删除或重命名组件运行时,我会考虑根据你的变化添加修改HashMap的方法。

    If you plan on adding, removing, or renaming components at any point during runtime, I would consider adding methods that modify the HashMap according to your changes.

    这篇关于按名称获取Swing组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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