2014-09-20 129 views
1

所以我在這裏有一個代碼,它就像遊戲中的模擬人生,你改變模擬人或人的外觀。所以我的問題是,一旦我選擇組合框上的選擇,圖像將會改變,一旦我點擊加載按鈕。但是,一旦我點擊另一個選擇,因爲我想改變它,圖像不會再改變。這裏是代碼。我將提供我用過的照片的鏈接。這裏是圖片的下載鏈接:https://onedrive.live.com/redir?resid=ED1942ABAA45D6A5%21245以及代碼。當按鈕被第二次點擊時圖像不會改變

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


    public class Sims extends JFrame { 
    private int x,y,z; 
    private Container game; 
    private JComboBox Head; 
    private JComboBox Ear; 
    private JLabel Avatar; 
    private JTextArea details; 

    private String []Hat = {"No Hat", "Captain Hat", "Black Leather Hat"}; 
    private JButton load; 
    private String []Earrings = {"No Earrings", "Silver Dangling Earrings", "Gold Dangling  Earrings"}; 
    private ImageIcon [] Accessories = 
    { new ImageIcon("blackleather.PNG"),//0 
    new ImageIcon("blackleather_goldear.PNG"),//1 
    new ImageIcon("blackleather_silverear.PNG"),//2 
    new ImageIcon("captainhat.PNG"),//3 
    new ImageIcon("captainhat_goldear.PNG"),//4 
    new ImageIcon("captainhat_silverear.PNG"),//5 
    new ImageIcon("goldear.PNG"),//6 
    new ImageIcon("noaccessories.PNG"),//7 
    new ImageIcon("silverear.PNG")};//8 

    public Sims() { 
    getContentPane().removeAll(); 
    setTitle("Avatar!"); 
    setSize(250,450); 
    setLocationRelativeTo(null); 
    game = getContentPane(); 
    game.setLayout(new FlowLayout()); 
    Head = new JComboBox(Hat); 
    Ear = new JComboBox(Earrings); 
    Avatar = new JLabel(Accessories[7]); 
    load = new JButton("Load Image"); 
    details = new JTextArea("AVATAR DETAILS:    "+"\n"+"  Hat:   "+Hat[Head.getSelectedIndex()]+"\n"+"  Earrings: "+Earrings[Ear.getSelectedIndex()]); 
game.add(Avatar); 
game.add(Head); 
game.add(Ear); 
game.add(load); 
game.add(details, BorderLayout.SOUTH); 
setVisible(true); 
details.setEditable(false); 

Head.addActionListener(
    new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     JComboBox temphead = (JComboBox) e.getSource(); 
     int temphat = (int) temphead.getSelectedIndex(); 
     x = temphat; 
    } 
}); 
Ear.addActionListener(
    new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     JComboBox tempear = (JComboBox) e.getSource(); 
     int tempearrings = (int) tempear.getSelectedIndex(); 
     y = tempearrings; 
    } 
}); 
load.addActionListener(
    new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
    getContentPane().removeAll(); 
    if(x==0&&y==0){ 
     z = 7; 
    } 
    if(x==0&&y==1){ 
     z = 8; 
    } 
    if(x==0&&y==2){ 
     z = 6; 
    } 
    if(x==1&&y==0){ 
     z = 3; 
    } 
    if(x==1&&y==1){ 
     z = 5; 
    } 
    if(x==1&&y==2){ 
     z = 4; 
    } 
    if(x==2&&y==0){ 
     z = 0; 
    } 
    if(x==2&&y==1){ 
     z = 2; 
    } 
    if(x==2&&y==2){ 
     z = 1; 
    } 


    setTitle("Avatar"); 
    setSize(250,450); 
    setLocationRelativeTo(null); 
    game = getContentPane(); 
    game.setLayout(new FlowLayout()); 
    Head = new JComboBox(Hat); 
    Ear = new JComboBox(Earrings); 
    Avatar = new JLabel(Accessories[z]); 
    load = new JButton("Load Image"); 

    details = new JTextArea("AVATAR DETAILS:    "+"\n"+"  Hat:   "+Hat[x]+"\n"+"  Earrings: "+Earrings[y]); 
    game.add(Avatar); 

    game.add(Head); 
    game.add(Ear); 

    game.add(load); 
    game.add(details, BorderLayout.SOUTH); 
    setVisible(true); 
    details.setEditable(false); 

    } 
    }); 



} 


    public static void main(String[] args) { 
    Sims fs = new Sims(); 

     fs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    } 

    } 
+0

請不要刪除你的代碼!這個問題不僅適用於您,還適用於所有未來的訪問者,但也有類似的問題。通過傷害你的問題,你對這些未來的訪問者沒有什麼幫助,這對他們來說不公平。 – 2014-09-20 14:50:35

+0

同樣,通過刪除代碼,您可以使您的問題失效。再次請留下來爲未來的訪客。 – 2014-09-20 16:09:56

回答

1

你完全加載新組件按下JButton的時候,所以新的組件將不會響應一樣的舊的。不要這樣做。相反,在ActionLIstener中,只需更改現有顯示器JLabel的圖標即可。

重申,你的JButton的ActionListener應該從JComboBoxes獲得數據(不需要給組合框ActionListeners),然後交換圖標,就是這樣。它不應該創建新組件。

的底線是,你JButton的半僞代碼的ActionListener應該是這個樣子:

@Override 
    public void actionPerformed(ActionEvent e) { 
    // get earrings selection from earrings combo box 
    // get hat selection from hat combo box 

    avatarLabel.setIcon(.... some value based on the selections abovbe ...); 
    } 

我自己,我發現最容易創建兩個枚舉,一頂帽子枚舉和耳環枚舉,然後創建一個Head類,該類持有這兩個枚舉的值,並且重要地覆蓋Object的equals和hashCode方法,因此它可以在HashMap中正常工作。然後,我將每個Head對象與HashMap中的ImageIcon相關聯,並在我的ActionListener中使用此HashMap選擇正確的圖標。這對你來說有點過分了,我懷疑你可以將它用於你的任務,但無論如何,這裏是:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ImageSwap extends JPanel { 
    private Map<Head, Icon> headIconMap = new HashMap<>(); 
    private JComboBox<Earrings> earingCombo = new JComboBox<>(Earrings.values()); 
    private JComboBox<Hat> hatCombo = new JComboBox<>(Hat.values()); 
    private JButton loadButton = new JButton(new LoadAction("Load", KeyEvent.VK_L)); 
    private JLabel avatarLabel = new JLabel(); 

    public ImageSwap() throws IOException { 
     addToMap("blackleather.PNG", Earrings.NONE, Hat.BLACK_LEATHER); 
     addToMap("blackleather_goldear.PNG", Earrings.GOLD, Hat.BLACK_LEATHER); 
     addToMap("blackleather_silverear.PNG", Earrings.SILVER, Hat.BLACK_LEATHER); 
     addToMap("captainhat.PNG", Earrings.NONE, Hat.CAPTAIN); 
     addToMap("captainhat_goldear.PNG", Earrings.GOLD, Hat.CAPTAIN); 
     addToMap("captainhat_silverear.PNG", Earrings.SILVER, Hat.CAPTAIN); 
     addToMap("goldear.PNG", Earrings.GOLD, Hat.NONE); 
     addToMap("noaccessories.PNG", Earrings.NONE, Hat.NONE); 
     addToMap("silverear.PNG", Earrings.SILVER, Hat.NONE); 

     avatarLabel.setIcon(headIconMap.get(new Head(Earrings.NONE, Hat.NONE))); 

     JPanel buttonComboPanel = new JPanel(new GridLayout(0, 1, 0, 5)); 
     buttonComboPanel.add(earingCombo); 
     buttonComboPanel.add(hatCombo); 
     buttonComboPanel.add(loadButton); 

     setLayout(new BorderLayout()); 
     add(avatarLabel, BorderLayout.CENTER); 
     add(buttonComboPanel, BorderLayout.PAGE_END); 
    } 

    private void addToMap(String resourceText, Earrings earrings, Hat hat) 
     throws IOException { 
     BufferedImage img = ImageIO.read(getClass().getResource(resourceText)); 
     Icon icon = new ImageIcon(img); 
     headIconMap.put(new Head(earrings, hat), icon); 
    } 

    private class LoadAction extends AbstractAction { 
     public LoadAction(String name, int mnemonic) { 
     super(name); 
     putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     Earrings selectedEarrings = (Earrings) earingCombo.getSelectedItem(); 
     Hat selectedHat = (Hat) hatCombo.getSelectedItem(); 

     if (selectedEarrings != null && selectedHat != null) { 
      avatarLabel.setIcon(headIconMap.get(new Head(selectedEarrings, selectedHat))); 
     } 
     } 
    } 

    private static void createAndShowGui() { 
     ImageSwap mainPanel; 
     try { 
     mainPanel = new ImageSwap(); 
     JFrame frame = new JFrame("ImageSwap"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

class Head { 
    Earrings earrings; 
    Hat hat; 

    public Head(Earrings earrings, Hat hat) { 
     this.earrings = earrings; 
     this.hat = hat; 
    } 

    public Earrings getEarrings() { 
     return earrings; 
    } 

    public Hat getHat() { 
     return hat; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((earrings == null) ? 0 : earrings.hashCode()); 
     result = prime * result + ((hat == null) ? 0 : hat.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
     return true; 
     if (obj == null) 
     return false; 
     if (getClass() != obj.getClass()) 
     return false; 
     Head other = (Head) obj; 
     if (earrings != other.earrings) 
     return false; 
     if (hat != other.hat) 
     return false; 
     return true; 
    } 

} 

enum Earrings { 
    NONE("No Earrings"), SILVER("Silver Dangling Earrings"), GOLD(
     "Gold Dangling Earrings"); 
    private String text; 

    private Earrings(String text) { 
     this.text = text; 
    } 

    @Override 
    public String toString() { 
     return text; 
    } 
} 

enum Hat { 
    NONE("No Hat"), CAPTAIN("Captain Hat"), BLACK_LEATHER("Black Leather Hat"); 
    private String text; 

    private Hat(String text) { 
     this.text = text; 
    } 

    @Override 
    public String toString() { 
     return text; 
    } 
} 
+0

所以你的意思是我應該刪除負載=新的JButton(「負載圖像」);?這裏是我的代碼在JLabel頭像=新JLabel(配件[7])的圖標; – user3767918 2014-09-20 13:27:23

+0

對不起,我很困惑。這是我第一次用圖像編碼。 @Hovercraft Full Of Eels – user3767918 2014-09-20 13:27:49

+0

@ user3767918:看編輯回答。拿出一些代碼,掛上... – 2014-09-20 13:31:48

相關問題