Java获取JPanel组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/370310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java get JPanel Components
提问by Devoted
I have a JPanel full of JTextFields...
我有一个充满 JTextFields 的 JPanel ......
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
}
How do I later get the JTextFields in that JPanel? Like if I want their values with
我以后如何在该 JPanel 中获取 JTextFields?就像我想要他们的价值观一样
TextField.getText();
Thanks
谢谢
采纳答案by OscarRyz
Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime )
请记住,他们并没有自己到达那里(我认为阅读一些有关在运行时动态创建这些面板的问题)
In the answers posted there, someone said you should kept reference to those textfields in an array. That's exactly what you need here:
在那里发布的答案中,有人说您应该在数组中保留对这些文本字段的引用。这正是您所需要的:
List<JTextField> list = new ArrayLists<JTextField>();
// your code...
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
list.add( textField ); // keep a reference to those fields.
}
// Later
// 之后
for( JTextField f : list ) {
System.out.println( f.getText() ) ;
}
Wasn't that easy?
那不是很容易吗?
Just remember to keep these kinds of artifacts ( list ) as private as possible. They are for your control only, I don't think they belong to the interface.
请记住将这些类型的工件(列表)尽可能保密。它们仅供您控制,我认为它们不属于接口。
Let's say you want to get the array of texts, instead of
假设您想要获取文本数组,而不是
public List<JTextField> getFields();
You should consider:
你应该考虑:
public List<String> getTexts(); // get them from the textfields ...
回答by Uri
Every JPanel in Java is also an AWT container. Thus, you should be able to use getComponents to get the array of contained components in the panel, iterate over them, check their types (To make sure you didn't get other controls), and do whatever you need with them.
Java 中的每个 JPanel 也是一个 AWT 容器。因此,您应该能够使用 getComponents 来获取面板中包含的组件数组,遍历它们,检查它们的类型(以确保您没有获得其他控件),并使用它们执行任何您需要的操作。
However, this is generally poor design. If you know that you will need to access specific components, it is better to maintain an array of those text fields and pass it around, even though they are visually contained within the container.
然而,这通常是糟糕的设计。如果您知道需要访问特定组件,最好维护这些文本字段的数组并将其传递出去,即使它们在视觉上包含在容器中。
If this is a recurrent task or could be done from multiple points, it may even make sense to have a special class representing a panel with text fields, that will then provide through its interface means of accessing these fields.
如果这是一个经常性的任务或者可以从多个点完成,甚至可以有一个特殊的类来表示一个带有文本字段的面板,然后通过它的接口提供访问这些字段的方法。
回答by cardaba713
//una forma de recorer todos los elementos dentro de un jpanel
Component[] components = jPanelX.getComponents();
for (int i = 0; i < components.length; i++) {
if(components[i].getClass().getName().toString().equals("javax.swing.JTextField")){
components[i].setEnabled(false);
}
}
回答by Akros
You should call the getComponents method this returns with an array of all elements on your JPanel. After you can iterate on the array and check if its equals with the sought after component.
您应该调用 getComponents 方法,该方法返回 JPanel 上所有元素的数组。在您可以迭代数组并检查它是否与寻求的组件相等之后。
List<JTextField> list = new ArrayLists<JTextField>();
Component[] components = panel.getComponents();
for (Component component : components) {
if (component.getClass().equals(JTextField.class)) {
list.add((JTextField)component);
}
}
回答by Deepeshkumar
Your problem is writing the tedious code text. Why not just generate it and paste in the program!!...
您的问题是编写乏味的代码文本。为什么不直接生成它并粘贴到程序中!!...
for(int i=1 ; i<=maxpoints ;i++){
System.out.println("JTextField tf"+i+" = new JTextField()"+";");
System.out.println("points.add(tf"+i+")"+";");
}
Paste the output of the above code in your program and you are done. Now, to access the content of text fields you can generate the tedious code text in a similar way....
将上述代码的输出粘贴到您的程序中,您就完成了。现在,要访问文本字段的内容,您可以以类似的方式生成繁琐的代码文本......
for(int i=1 ; i<=maxpoints ;i++){
System.out.println("String s"+i+" = JTextField tf"+i+".getText()"+";");
}
回答by NetCollector
This is what I did to recursively go through the container and get the textfields that are on the JPanels.
这就是我为递归遍历容器并获取 JPanel 上的文本字段所做的工作。
private void ClearAllFields(Container myContainer) {
Component myComps[] = myContainer.getComponents();
for (int i=0; i<myComps.length; i++) {
if(myComps[i] instanceof JPanel) {
JPanel myPanel = (JPanel) myComps[i];
ClearAllFields(myPanel);
}
if(myComps[i] instanceof JTextField) {
JTextField myTextField = (JTextField) myComps[i];
myTextField.setText("");
}
}
}
And then to use it, you call it this way
然后使用它,你这样称呼它
ClearAllFields([jdialog or jframe etc].getContentPane());
回答by Nico
Dynamicly give it a name during creation and then do this.
在创建期间动态为其命名,然后执行此操作。
Component[] components = panel.getComponents();
for (Component component: components) {
var name = component.getName();
if(name != null){
if(name.equals("textfield 1")){
var field = (JTextField)component;
field.setText("whatever you want / same for options and other components")
}
}
}