JComboBox 参与其他 JComboBoxes ActionListeners
JComboBox engaging other JComboBoxes ActionListeners
所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试从我的原始代码中表示问题。我想要一个 JComboBox 添加一个 JComboBox,然后 JComboBox 将添加第三个 JComboBox 等等。每次我单击它们时,我都希望它们根据之前的 JComboBox 显示的内容更改内容。
无论如何,我现在最大的问题是当我 select 第一个 JComboBox "racebox" 中的东西时。它不仅将 "infantrybox" 添加到面板,它还添加了我拥有的所有其他 JComboBoxes,而不是仅在我 select 各自的 JComboBox 中添加它们一次。
这就像当我 select racebox 中的某些东西时,它开始从每个其他的 actionPerformed 中读取代码。
一个奇怪的事情是 JComboBoxes 在添加 "racebox" 之后向后添加。
第一:赛车箱
第二:infantrynmrbox
第三:步兵箱
...
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();
}
});
}
}
所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试从我的原始代码中表示问题。我想要一个 JComboBox 添加一个 JComboBox,然后 JComboBox 将添加第三个 JComboBox 等等。每次我单击它们时,我都希望它们根据之前的 JComboBox 显示的内容更改内容。
无论如何,我现在最大的问题是当我 select 第一个 JComboBox "racebox" 中的东西时。它不仅将 "infantrybox" 添加到面板,它还添加了我拥有的所有其他 JComboBoxes,而不是仅在我 select 各自的 JComboBox 中添加它们一次。
这就像当我 select racebox 中的某些东西时,它开始从每个其他的 actionPerformed 中读取代码。
一个奇怪的事情是 JComboBoxes 在添加 "racebox" 之后向后添加。 第一:赛车箱 第二:infantrynmrbox 第三:步兵箱
...
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();
}
});
}
}