然后组合getSelectedItem

时间:2013-03-15 15:34:35

标签: java swing

简单的问题,我输入此代码,我看到组合框和标签,但在选择组合后,标签应该是一个图像。这不会发生......当然我忘记了什么

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ChooseFrame extends JFrame
{
    public ChooseFrame()
    {
        labelLeagueImage = new JLabel("Liga");
        comboLeague = createComboLeague();

        class ChooseListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                String league = (String)comboLeague.getSelectedItem();

                if (league.equals("Italia - Serie A"))
                {
                    ImageIcon icon = new ImageIcon("Italia - Serie A.png");
                    labelLeagueImage.setIcon(icon);  
                }

            }
        }

        listener = new ChooseListener();
        leaguePanel = new JPanel();
        leaguePanel.add(comboLeague);
        leaguePanel.add(labelLeagueImage);
        add(leaguePanel);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }

    public JComboBox createComboLeague()
    {
        JComboBox combo = new JComboBox();
        combo.addItem("Select a league");
        combo.addItem("Italia - Serie A");
        combo.addItem("Italia - Serie B");
        combo.addActionListener(listener);
        return combo;
    }

    private JPanel leaguePanel;
    private JComboBox comboLeague;
    private JLabel labelLeagueImage;
    private ActionListener listener;

    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 600;            

}

2 个答案:

答案 0 :(得分:4)

在创建组合框并分配侦听器时,在createComboLeague()中,侦听器仍为空。它仅在调用createComboLeague()方法后初始化。

答案 1 :(得分:1)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ChooseFrame extends JFrame
{
     private JPanel leaguePanel;
        private JComboBox comboLeague;
        private JLabel labelLeagueImage;
        private ActionListener listener;

        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 600;  
    public ChooseFrame()
    {
        labelLeagueImage = new JLabel("Liga");
          comboLeague = createComboLeague();



       // listener = new ChooseListener();
        leaguePanel = new JPanel();
        leaguePanel.add(comboLeague);
        leaguePanel.add(labelLeagueImage);
        add(leaguePanel);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }

    public JComboBox createComboLeague()
    {
        JComboBox combo = new JComboBox();
        combo.addItem("Select a league");
        combo.addItem("Italia - Serie A");
        combo.addItem("Italia - Serie B");
        combo.addActionListener(listener);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                String league = (String)comboLeague.getSelectedItem();

                if (league.equals("Italia - Serie A"))
                {
                    ImageIcon icon = new ImageIcon("Italia - Serie A.png");
                    labelLeagueImage.setIcon(icon);  
                }


            }
        });
        return combo;
    }


    public static void main(String[] args) {
        ChooseFrame cs=new ChooseFrame();
        cs.setVisible(true);
    }

}