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