如何确定组件类型
How to determine component type
我正在处理许多 JDialog 组件,以寻找具有特定客户端的特定类型 属性。我了解如何检查客户端属性,但我找不到将组件类型returns 作为字符串的方法。我想做这样的事情:
Component[] fields = timeLineDialog.getContentPane().getComponents();
for (Component field : fields) {
if (field.<getType>.equals("JComboBox") {
.
.
.
}
我可以得到组件,但我不知道如何确定类型。我可以使用什么方法? TIA。
使用 instanceof
运算符。
for (Component field : fields) {
if (field instanceof JComboBox) {
// do something
} else if (field instanceof JButton) {
// do something
} else if (field instanceof JPanel) {
// do something
}
}
参见:http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm
我正在处理许多 JDialog 组件,以寻找具有特定客户端的特定类型 属性。我了解如何检查客户端属性,但我找不到将组件类型returns 作为字符串的方法。我想做这样的事情:
Component[] fields = timeLineDialog.getContentPane().getComponents();
for (Component field : fields) {
if (field.<getType>.equals("JComboBox") {
.
.
.
}
我可以得到组件,但我不知道如何确定类型。我可以使用什么方法? TIA。
使用 instanceof
运算符。
for (Component field : fields) {
if (field instanceof JComboBox) {
// do something
} else if (field instanceof JButton) {
// do something
} else if (field instanceof JPanel) {
// do something
}
}
参见:http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm