gpt4 book ai didi

java - 根据 JComboBox.SelectedItem 启用和禁用 JCheckBox

转载 作者:行者123 更新时间:2023-11-29 05:02:33 24 4
gpt4 key购买 nike

好的,我有一个包含 12 个 JCheckBox 的面板,在加载界面时所有 JCheckBox 都被禁用。当用户从 JComboBox 中选择一个选项时,我只想根据在 JComboBox 中选择的项目启用某些 JCheckBox。

目前在 JComboBoxActionPerformed 中,我启用了与选择相关的所有 JCheckBox,但在这样做之前尝试禁用当前启用的所有按钮(以防 JComboBox 中的选定项目发生更改)。

我必须禁用任何已启用按钮的代码如下:

public void disableBoxes() {
for (JCheckBox j : ArrayOfJCheckBox) {
if (j.isEnabled()) {
j.setEnabled(false);
}
}
}

但是这并没有做任何事情,如果我从JComboBoxActionPerformed 方法,然后是 JCheckBox 的启用,正如我所期望的那样。让我假设问题出在这段代码上。

此外,我目前正在手动创建 ArrayOfJCheckBox,我想知道是否有一种方法可以通过添加获取面板内的所有 JCheckBox 并将它们添加到列表中来实现?如果这是可能的,那么是否可以像我已经尝试做的那样遍历列表?

感谢您的帮助!

院长

最佳答案

你说:

OK so I have a Panel containing 12 JCheckBox's, Upon the interface loading all of the JCheckBox's are disabled. When a user selects an option from the JComboBox I want only certain JCheckBox's to be enabled depending on the item selected in the JComboBox.

我们不知道您将使用什么标准来决定启用或禁用哪个 JCheckBox,但目前这并不是非常关键。

At the moment in the JComboBoxActionPerformed i am enabling all of the JCheckBoxes relevant to the selection but prior to doing so attempting to disable all buttons that are currently enabled (In case the selected item in the JComboBox is changed).

这应该有效。

The code i have to disable any enabled buttons is as follows:

public void disableBoxes() {
for (JCheckBox j : ArrayOfJCheckBox) {
if (j.isEnabled()) {
j.setEnabled(false);
}
}
}

This does not however do anything, If I remove the call for this method from the JComboBoxActionPerformed method then the JCheckBox's enable as i would expect. Leading me to assume that the problem lies with this code.

这应该可行,但您的帖子没有向我们展示它为何不起作用,因此根据所提供的信息,我们无法帮助解决它尚未起作用的具体原因。

Furthermore i am currently creating the ArrayOfJCheckBox manually, i was wondering if there is a way i could do it by maybe adding getting all of the JCheckBox's inside the panel and adding them to a list? If this is possible would it

无法提供帮助,因为我们不知道您是如何手动创建“ArrayOfJCheckBox”的,因为您还没有发布任何代码,我们也无法帮助处理第二个请求,因为我们不知道您的程序是怎样的结构化。


说了这么多,应该很容易创建一个 JCheckBox 或 JToggleButton 列表(父类),然后在需要时遍历启用和禁用组件的列表。

例如:

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class CheckBoxList extends JPanel {
private static final int CHECK_BOX_COUNT = 12; // number of JCheckBoxes
private List<JCheckBox> checkBoxList = new ArrayList<>(); // list of JCheckBoxes
private DefaultComboBoxModel<Integer> cModel = new DefaultComboBoxModel<>(); // combo box model
private JComboBox<Integer> comboBox = new JComboBox<>(cModel);

public CheckBoxList() {
// create JPanel to hold JCheckBoxes
JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1, 0, 5));
// create JCheckBoxes and add to both the above JPanel and the checkBoxList
for (int i = 0; i < CHECK_BOX_COUNT; i++) {
String text = "CheckBox " + i;
JCheckBox checkBox = new JCheckBox(text);
checkBox.setEnabled(false); // disabled by default
checkBoxPanel.add(checkBox);
checkBoxList.add(checkBox);
}

// fill our combo box's model. For this example, I'm just going to use
// Integers, and then enable only the JCheckBoxes that are multiples of the selected int
for (int i = 0; i < 5; i++) {
cModel.addElement(i + 1);
}

comboBox.setSelectedIndex(-1); // set combo at empty
comboBox.addActionListener(new ComboListener()); // add ActionListner

// JPanel to hold the JComboBox
JPanel centerPanel = new JPanel(new GridBagLayout());
centerPanel.add(comboBox);
int gap = 35;
centerPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));

// add all to the main JPanel (this)
gap = 5;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(checkBoxPanel, BorderLayout.LINE_END);
}

private class ComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// get combo's selection
int selection = (Integer) comboBox.getSelectedItem();

// use the information to enable/disable JCheckBoxes
for (int i = 0; i < checkBoxList.size(); i++) {
if (i % selection == 0) {
checkBoxList.get(i).setEnabled(true);
} else {
checkBoxList.get(i).setEnabled(false);
}
}
}
}

// create and display GUI
private static void createAndShowGui() {
CheckBoxList mainPanel = new CheckBoxList();

JFrame frame = new JFrame("CheckBoxList");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

同样,如果这没有帮助,那么您最好创建并发布一个 Minimal, Complete, and Verifiable Example Program将代码压缩为仍然可以编译和运行的最小位,没有外部依赖性(例如需要链接到数据库或图像),没有与您的问题无关的额外代码,但仍然可以证明您的问题,类似到我上面的代码。

关于java - 根据 JComboBox.SelectedItem 启用和禁用 JCheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31596382/

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