1

我在这里有一个程序可以更改头像帽子和耳环问题是它只重新启动一次(加载按钮只工作一次)

这就是我所拥有的:

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


public class Lab14_Navarro 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

/**
 * Creates a new instance of <code>Lab14_Navarro</code>.
 */
public Lab14_Navarro() {
        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) {
    Lab14_Navarro fs = new Lab14_Navarro();
    fs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

任何帮助都被接受了,谢谢我刚开始使用 Java,所以我不是那么好......但是

4

1 回答 1

3

当你第二次写作时:

load = new JButton("Load Image");

您正在创建一个新按钮,但是,您并没有给它一个新的 ActionListener

另一方面,你不需要创建一个新的按钮,你原来的加载按钮还在,你只需要再次将它添加到 contentPane 中。

于 2013-09-28T15:36:29.013 回答