2017-04-17 60 views
0

我想知道是否有简单的方法来做到这一点。我试图从武器组合框中移除物品(成功),然后将数组添加到组合框中,就像第一次实例化时那样。我不想创建和复制一个新的,因为已经设置了喜欢的身体约束。下面是代码...用数组替换JComboBox中的项目

import org.apache.commons.lang3.ArrayUtils; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 
import javax.swing.border.Border; 

public class TestGui extends JFrame { 
    private final String[] guiCharSelDefault = {"--- Select Character ---"}; 
    private final String[] characters = {"charOne", "charTwo", "charThree", "charFour"}; 
    private final String[] GuiCharSel = (String[]) ArrayUtils.addAll(guiCharSelDefault, characters); 
    private final String[] weapon = {"Weapon"}; 
    private final String[][] allWeapons = { 
      { 
        "weakWeaponOne", "strongWeaponOne", "shortWeaponOne", "longWeaponOne" 
      }, 
      { 
        "weakWeaponTwo", "strongWeaponTwo", "shortWeaponTwo", "longWeaponTwo" 
      }, 
      { 
        "weakWeaponThree", "strongWeaponThree", "shortWeaponThree", "longWeaponThree" 
      }, 
      { 
        "weakWeaponFour", "strongWeaponFour", "shortWeaponFour", "longWeaponFour" 
      } 
    }; 
    private JComboBox charCombo = new JComboBox(GuiCharSel); 
    private JComboBox weaponsCombo = new JComboBox(weapon); 
    private JPanel centerFrame = createCenterFrame(); 

    //************************************************************************************** 

    private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){ 
     GridBagConstraints gbc = new GridBagConstraints(); 

     if (anchorLocation.toUpperCase().equals("NORTHWEST")){ 
      gbc.anchor = GridBagConstraints.NORTHWEST; 
     } else if (anchorLocation.toUpperCase().equals("NORTH")){ 
      gbc.anchor = GridBagConstraints.NORTH; 
     } else if (anchorLocation.toUpperCase().equals("NORTHEAST")){ 
      gbc.anchor = GridBagConstraints.NORTHEAST; 
     } else if (anchorLocation.toUpperCase().equals("WEST")){ 
      gbc.anchor = GridBagConstraints.WEST; 
     } else if (anchorLocation.toUpperCase().equals("EAST")){ 
      gbc.anchor = GridBagConstraints.EAST; 
     } else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){ 
      gbc.anchor = GridBagConstraints.SOUTHWEST; 
     } else if (anchorLocation.toUpperCase().equals("SOUTH")){ 
      gbc.anchor = GridBagConstraints.SOUTH; 
     } else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){ 
      gbc.anchor = GridBagConstraints.SOUTHEAST; 
     } else { 
      gbc.anchor = GridBagConstraints.CENTER; 
     } 

     gbc.gridx = gridx; 
     gbc.gridy = gridy; 
     gbc.ipadx = ipadx; 
     gbc.ipady = ipady; 
     gbc.weightx = weightx; 
     gbc.weighty = weighty; 
     gbc.insets = insets; 

     return gbc; 
    } 

    private Insets setInsets(int top, int left, int bottom, int right){ 
     Insets insets = new Insets(top,left,bottom,right); 
     return insets; 
    } 

    //************************************************************************************** 

    private JPanel createCenterFrame(){ 
     JPanel pnl = new JPanel(); 
     Border raisedBevel = BorderFactory.createRaisedBevelBorder(); 
     Border loweredBevel = BorderFactory.createLoweredBevelBorder(); 
     Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel); 

     pnl.setBorder(compound); 

     charCombo.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString(); 
         if (charName.equals("charOne")){ 
          weaponsCombo.removeAllItems(); 
          weaponsCombo.addItem(allWeapons[1]); // <---- Help Please!!! 
         } 
        } 
       } 
     ); 
     pnl.add(charCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0))); 
     pnl.add(weaponsCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0))); 

     return pnl; 
    } 

    TestGui(){ 
     add(centerFrame, BorderLayout.CENTER); 

     setSize(800,600); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    //************************************************************************************** 

    public static void main(String[] args) { 
     new TestGui(); 
    } 
} 

95号线(// <---- Help Please!!!)是我想知道如果有一个简单的方法来做到这一点。问题是当你在gui的字符组合框中选择“charOne”时,你会看到一个奇怪的语言字符串,而不是武器组合框中4个武器的列表。

回答

2

简单的解决方法是更换ComboBoxModel ...

if (charName.equals("charOne")) { 
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(allWeapons[1]); 
    weaponsCombo.setModel(model); 
} 

更多细节

+0

这似乎看到工作How to Use Combo Boxes以及所以我仍然可以使用'JComboBox'>'weaponsCombo.setModel (新的DefaultComboBoxModel(allWeapons [1]));'。谢谢 :) –