gpt4 book ai didi

java - JComboBox 与其他 JComboBoxes ActionListener 交互

转载 作者:行者123 更新时间:2023-12-01 09:03:57 28 4
gpt4 key购买 nike

所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试代表我的原始代码中的问题。我想要一个 JComboBox 添加一个 JComboBox,然后 JComboBox 将添加第三个 JComboBox 等等。每次我单击它们时,我希望它们根据之前的 JComboBox 显示的内容更改内容。

无论如何,我现在最大的问题是当我在第一个 JComboBox“racebox”中选择某些内容时。它不仅将“infantrybox”添加到面板中,还添加了我拥有的所有其他 JComboBox,而不是仅在我在相应的 JComboBox 中选择某些内容后才添加它们。

就像当我在racebox中选择某些内容时,它开始从所有其他actionPerformed中读取代码。

一件奇怪的事情是 JComboBoxes 在添加“racebox”后向后添加。第一:赛车盒第二:步兵箱第三:步兵箱

...
public void Attacker(){

racebox = new JComboBox(array);
infantrybox = new JComboBox();
infantrynmrbox = new JComboBox();

panel.add(racebox);
panel.revalidate();
panel.repaint();

racebox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
race = (String)cb.getSelectedItem();

infantrybox.removeAllItems();
for(String s : otherarray){
infantrybox.addItem(s);
}

panel.add(infantrybox);
panel.revalidate();
panel.repaint();
}
});

infantrybox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
infantry = (String)cb.getSelectedItem();

infantrynmrbox.removeAllItems();
for(String s : nmr){
infantrynmrbox.addItem(s);
System.out.println(s + " ");
}

panel.add(infantrynmrbox);
panel.revalidate();
panel.repaint();
}
});

infantrynmrbox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
infantrynmr = Integer.parseInt((String)cb.getSelectedItem());
}
});
...
}

最佳答案

不要不断向面板添加组合框。

您只需更改现有组合框的模型即可。

例如:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxTwo extends JPanel implements ActionListener
{
private JComboBox<String> mainComboBox;
private JComboBox<String> subComboBox;
private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

public ComboBoxTwo()
{
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
mainComboBox = new JComboBox<String>( items );
mainComboBox.addActionListener( this );

// prevent action events from being fired when the up/down arrow keys are used
mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
add( mainComboBox );

// Create sub combo box with multiple models

subComboBox = new JComboBox<String>();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
add( subComboBox );

JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", "");
Dimension d = arrow.getPreferredSize();
System.out.println(arrow.getClass());
System.out.println(d);
d.width = 100;
arrow.setPreferredSize(d);

String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
subItems.put(items[1], subItems1);

String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
subItems.put(items[2], subItems2);

String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
subItems.put(items[3], subItems3);
}

public void actionPerformed(ActionEvent e)
{
String item = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( item );

if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}

private static void createAndShowUI()
{
try
{
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ComboBoxTwo() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

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

关于java - JComboBox 与其他 JComboBoxes ActionListener 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436812/

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