在单选按钮上选择填充组合框

时间:2014-04-15 23:29:49

标签: java swing checkbox combobox

1.0 我正在填写复选框上的复选框 取决于你选择的wihich类型组合框应该显示选项 工作了一段时间,但我做了一些事情,现在它只显示一个选项,只有在取消选中复选框时才显示它 有更好的方法吗?

更新1.1:  好的,我更新了代码。删除所有不必要的我希望。 我尝试使用ItemListener和ActionListener。 但我认为这不是问题,我认为我填写JCOmboBox有点错误。 它在jcboCountry.removeAllItems();左右,这让我觉得一团糟。 我甚至尝试将方法empty();逐个删除,但它仍然只用一个项填充我的JCB。当我点击它们时,单选按钮有时也不会检查。有时它会在第二次点击RadioButton时提交JCB。

public class FinalPlayer extends JApplet implements ActionListener, ItemListener{

JPanel westP = new JPanel();
JPanel centerP = new JPanel();
JPanel bottomP = new JPanel();


JPanel genresP = new JPanel();
JPanel imagePanel = new JPanel();
JPanel countryPanel = new JPanel();
JComboBox jcboCountry = new JComboBox();

JTextField genreF = new JTextField();

String country;


JRadioButton jazzCB = new JRadioButton("Pop",true);
JRadioButton rockCB = new JRadioButton("Rock",false);
JRadioButton allCB = new JRadioButton("All",false);
ButtonGroup bG = new ButtonGroup();


/** Initialize the applet */
public void init() {

    Container contentPane = getContentPane();  
    mainPanel.setPreferredSize( new Dimension(701, 341));

    topP.setPreferredSize( new Dimension(700, 30));       
    eastP.setPreferredSize( new Dimension(225,280));        
    centerP.setPreferredSize( new Dimension(250,280));       
    westP.setPreferredSize( new Dimension(225,280));        
    bottomP.setPreferredSize( new Dimension(700, 30));

    // TOP

    //WEST

    genreF.setPreferredSize( new Dimension(124, 29));
    genreF.setEditable(false);  
    genreF.setText("Choose Genre");
    genresP.setPreferredSize( new Dimension(124, 250));
    jazzCB.setPreferredSize( new Dimension(123, 19));
    rockCB.setPreferredSize( new Dimension(123, 19));
    allCB.setPreferredSize( new Dimension(123, 19));
    bG.add(jazzCB);
    bG.add(rockCB);
    bG.add(allCB);

    //jazzCB.addItemListener(this);
    //rockCB.addItemListener(this);
    //allCB.addItemListener(this);


    jazzCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                empty();
                //jcboCountry.removeAllItems();

                jcboCountry.addItem("Bastille - Pompeii");
                jcboCountry.addItem("Imagine Dragons - Tiptoe");
                jcboCountry.addItem("John Legend - All of me");
                jcboCountry.addItem("Katy Perry - Dark horse");

            }
        });

    rockCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                empty();
                //jcboCountry.removeAllItems();

                jcboCountry.addItem("Red Hot Chilly Peppers - By the way");
                jcboCountry.addItem("Red Hot Chilly Peppers - Californication");
                jcboCountry.addItem("Red Hot Chilly Peppers - Dani California");
                jcboCountry.addItem("Red Hot Chilly Peppers - Under the Bridge");

            }
        });

    allCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                empty();
                //jcboCountry.removeAllItems();

                jcboCountry.addItem("Bastille - Pompeii");
                jcboCountry.addItem("Imagine Dragons - Tiptoe");
                jcboCountry.addItem("John Legend - All of me");
                jcboCountry.addItem("Katy Perry - Dark horse");
                jcboCountry.addItem("Red Hot Chilly Peppers - By the way");
                jcboCountry.addItem("Red Hot Chilly Peppers - Californication");
                jcboCountry.addItem("Red Hot Chilly Peppers - Dani California");
                jcboCountry.addItem("Red Hot Chilly Peppers - Under the Bridge");

            }
        });
    genresP.add(jazzCB);
    genresP.add(rockCB);
    genresP.add(allCB);


    //CENTER
    imagePanel.setPreferredSize( new Dimension(249, 249));
    descF.setPreferredSize( new Dimension(249, 30));
    descF.setEditable(false);  
    descF.setText("Descriptio");

    //EAST


    //BOTTOM
    countryPanel.setPreferredSize( new Dimension(249, 50));
    countryPanel.add(jcboCountry);
    jcboCountry.addItemListener(this);
    //jcboCountry.setEnabled(false);

    // FILLING 


    centerP.add(descF, BorderLayout.NORTH);
    centerP.add(imagePanel, BorderLayout.CENTER);
    westP.add(genreF ,BorderLayout.NORTH);
    westP.add(genresP ,BorderLayout.CENTER);
    bottomP.add(countryPanel, BorderLayout.CENTER);


    mainPanel.add(westP, BorderLayout.WEST);
    mainPanel.add(centerP, BorderLayout.CENTER);

    mainPanel.add(bottomP, BorderLayout.SOUTH);
    contentPane.add( mainPanel);
}

public void empty(){
    int itemCount = jcboCountry.getItemCount();

    for(int i=0;i<itemCount;i++){
        jcboCountry.removeItemAt(0);
    }
 }


 public void itemStateChanged(ItemEvent e) 
 {  
    /* Object source = e.getSource();

    if (source == jazzCB) {
    jcboCountry.removeAllItems();
    jcboCountry.addItem("Bastille - Pompeii");
    jcboCountry.addItem("Imagine Dragons - Tiptoe");
    jcboCountry.addItem("John Legend - All of me");
    jcboCountry.addItem("Katy Perry - Dark horse");

    }
    if (source == rockCB){
    jcboCountry.removeAllItems();
    jcboCountry.addItem("Red Hot Chilly Peppers - By the way");
    jcboCountry.addItem("Red Hot Chilly Peppers - Californication");
    jcboCountry.addItem("Red Hot Chilly Peppers - Dani California");
    jcboCountry.addItem("Red Hot Chilly Peppers - Under the Bridge");

    }   
    if (source == allCB){
    jcboCountry.removeAllItems();
    jcboCountry.addItem("Bastille - Pompeii");
    jcboCountry.addItem("Imagine Dragons - Tiptoe");
    jcboCountry.addItem("John Legend - All of me");
    jcboCountry.addItem("Katy Perry - Dark horse");
    jcboCountry.addItem("Red Hot Chilly Peppers - By the way");
    jcboCountry.addItem("Red Hot Chilly Peppers - Californication");
    jcboCountry.addItem("Red Hot Chilly Peppers - Dani California");
    jcboCountry.addItem("Red Hot Chilly Peppers - Under the Bridge");

    }*/
  }  



/** Main method */
public static void main(String[] args) {
    JFrame frame = new JFrame("Display Flags and Play Anthem");
    FinalPlayer applet = new FinalPlayer();
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    applet.init();
    applet.start();
    frame.setTitle("My Media Player ");
    frame.setResizable(false);
    // Display the frame
    frame.pack();
    frame.setVisible(true);
  }
 }

0 个答案:

没有答案
相关问题