2

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

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;            

}
4

2 回答 2

4

在您创建组合框并分配侦听器时,在 中createComboLeague(),侦听器仍然为空。仅在调用该createComboLeague()方法后才对其进行初始化。

于 2013-03-15T15:39:08.840 回答
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);
    }

}
于 2013-03-15T15:51:04.067 回答