本文介绍了如何访问JPanel上的控件...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java n0ob....让我提前道歉...

I'm a Java n0ob....so let me apologize in advance...

我有一个jPanel,我在运行时动态添加了许多控件.稍后,在我的代码中,我想遍历所有这些控件(它们是jCheckBoxes)以查看是否选中了它们.

I've got a jPanel that I've added a bunch of controls to, dynamically, at run-time. Later, in my code, I want to loop through all of those controls (they are jCheckBoxes) to see if they are checked or not.

在.NET中-我会看一些类似的东西...

In .NET - I'd be looking at some like...

对于每个myControl作为myPanel.Controls中的控件下一个

For Each myControl as Control In myPanel.ControlsNext

有没有一种方法可以通过jPanel完成?我不应该为此使用jPanel吗?

Is there a way to accomplish that with the jPanel? Should I not be using a jPanel for this?

推荐答案

这是我编写的一种方法,用于设置JPanel中所有控件的字体,您可以使用类似的方式来访问您的CheckBoxes(请注意,它是递归的,并且会通过任何子面板也是如此).

Here is a method I wrote to set the font for all controls within a JPanel you could use something similar to access you CheckBoxes (Note it is recursive and goes through any child panels also).

public static final void setJPanelFont(JPanel aPanel, Font font)
{
    Component c = null;
    Component[] components = aPanel.getComponents();

    aPanel.setFont(font);
    if(components != null)
    {
        int numComponents = components.length;
        for(int i = 0; i < numComponents; i++)
        {
            c = components[i];
            if(c != null)
            {
                if(c instanceof JPanel)
                {
                    setJPanelFont((JPanel)c,font);
                }
                else
                {
                    c.setFont(font);
                }
            }
        }
    }
}  

这篇关于如何访问JPanel上的控件...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:56