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

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

问题描述

我在 JFrame 中有一些我想要的组件引用另一个 JFrame 并且我想要按名称获取它们而不是为每个人做公共获取/设置方法.

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 访问文本字段中的文本或列表中的选择.

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 类变量.您需要在非常少.为简单起见,我将我的 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();
    }
    

  • 在您的类中定义以下两个方法.如果您还没有导入组件,则需要导入:

  • 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,它将框架/内容窗格/面板/等中所有当前存在的组件映射到它们各自的名称.

  • 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(String name) 一样简单.如果存在具有该名称的组件,它将返回该组件.如果不是,则返回 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天全站免登陆