gpt4 book ai didi

Java Swing Button 执行 have 的功能

转载 作者:行者123 更新时间:2023-11-29 07:14:12 25 4
gpt4 key购买 nike

我使用 Netbeans 7.0.1 及其在 gui builder 中的构建编写了一个 java 应用程序,它可以做一些不同的事情。我遇到的问题是屏幕上有一些供用户输入的文本字段。最初是一组字段和一个按钮,最多可以添加 10 个。我还有一个删除按钮,用于删除一个字段。所以基本上按钮在面板中添加和删除 jTextFields 和 jLabels。

当我点击按钮时似乎有延迟,所以我添加了一些 System.out.prints 并发现有时会按下按钮,系统会打印它应该打印的内容但忽略添加/移除组件。

这是一个已知问题吗?(我还没有找到任何东西,虽然我不是 100% 确定如何用我的搜索措辞)或者我做错了什么?

代码示例:注意:当前组件是一个包含每个组件及其值的映射

private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {                                       
if(hiddenValue != 81) {
currentComponents.get(hiddenValue).setVisible(true);
currentComponents.get(hiddenValue + 1).setVisible(true);
currentComponents.get(hiddenValue + 2).setVisible(true);
currentComponents.get(hiddenValue + 3).setVisible(true);
currentComponents.get(hiddenValue + 4).setVisible(true);
currentComponents.get(hiddenValue + 5).setVisible(true);
currentComponents.get(hiddenValue + 6).setVisible(true);
currentComponents.get(hiddenValue + 7).setVisible(true);
currentComponents.get(hiddenValue + 8).setVisible(true);
hiddenValue = hiddenValue + 10;
numEntries++;
removeButton.setVisible(true);
removeButton.setEnabled(true);
System.out.println(hiddenValue);
}
else {
currentComponents.get(hiddenValue).setVisible(true);
currentComponents.get(hiddenValue + 1).setVisible(true);
currentComponents.get(hiddenValue + 2).setVisible(true);
currentComponents.get(hiddenValue + 3).setVisible(true);
currentComponents.get(hiddenValue + 4).setVisible(true);
currentComponents.get(hiddenValue + 5).setVisible(true);
currentComponents.get(hiddenValue + 6).setVisible(true);
currentComponents.get(hiddenValue + 7).setVisible(true);
currentComponents.get(hiddenValue + 8).setVisible(true);
hiddenValue = hiddenValue + 10;
numEntries++;
addButton.setVisible(false);
addButton.setEnabled(false);
System.out.println(hiddenValue);
}
}

最佳答案

通过 index(es) 调用 JComponents 确实不是个好主意,也不是为什么要重新发明轮子,请使用(避免任何 XxxExceptions 也是)

for (Component c : currentComponents.getComponents()) {
//if (c instanceof JButton) {
c.setVisible(true);
//}
}

关于 JButtons ActionPerfomedsetEnabled(true / false) 的类似示例对于 JPanel

内的所有 JComponents

关于Java Swing Button 执行 have 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com