通过先前标签的验证将ImageIcon设置为另一个类中的标签

时间:2016-08-30 13:25:58

标签: java class button imageicon

我有一个程序需要选择一个角色,在这种情况下是一个性别。当女性'在同一JFrame上按下按钮,在课程Genders中,女性ImageIcon会显示在标签'头像上。和男性一样。如果已点击,则会在ImageIcon上显示男JFrame。在另一个JFrame上,我有一个班级StartGame,我选择的性别显示在标签playerbattle中。我不知道如何显示所选的性别。请帮忙,这是我的代码:

班级性别:

  import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.sun.prism.Image;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Genders extends JFrame {

    public JPanel contentPane;
    protected ImageIcon avtMale = new ImageIcon("male warrior.png");
    protected ImageIcon avtFemale = new ImageIcon("female warrior.png");

    /**
     * Launch the application.
     */
    public static void main(String[] args) throws IOException{
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Genders frame = new Genders();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    /*String[] imageList = 
            {
                    "female warrior.jpg", "male warrior.jpg"
            };
    */

    public Genders() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        ImageIcon maleicon = new ImageIcon("male warrior.png");

        JLabel Avatar = new JLabel();
        Avatar.setBounds(0, 0, 227, 261);
        contentPane.add(Avatar);

        ImageIcon femaleicon = new ImageIcon("female warrior.png");

        JButton btnFemale = new JButton("Female");
        btnFemale.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ex) {

                //play sound:
                try {
                     // Open an audio input stream.           
                      File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/btnclicksfx.wav"); //you could also get the sound file with a URL
                      AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
                     // Get a sound clip resource.
                     Clip clip = AudioSystem.getClip();
                     // Open audio clip and load samples from the audio input stream.
                     clip.open(audioIn);
                     clip.start();
                  } catch (UnsupportedAudioFileException e) {
                     e.printStackTrace();
                  } catch (IOException e) {
                     e.printStackTrace();
                  } catch (LineUnavailableException e) {
                     e.printStackTrace();
                  }

            Avatar.setIcon(avtFemale);

            }
        });
        btnFemale.setBounds(335, 11, 89, 23);
        contentPane.add(btnFemale);

            JButton btnMale = new JButton("Male");
            btnMale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    //play sound:
                    try {
                         // Open an audio input stream.           
                          File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/btnclicksfx.wav"); //you could also get the sound file with a URL
                          AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
                         // Get a sound clip resource.
                         Clip clip = AudioSystem.getClip();
                         // Open audio clip and load samples from the audio input stream.
                         clip.open(audioIn);
                         clip.start();
                      } catch (UnsupportedAudioFileException ex) {
                         ex.printStackTrace();
                      } catch (IOException ex) {
                         ex.printStackTrace();
                      } catch (LineUnavailableException ex) {
                         ex.printStackTrace();
                      }

                    Avatar.setIcon(avtMale);

                }
            });
            btnMale.setBounds(335, 45, 89, 23);
            contentPane.add(btnMale);

            JButton btnOkay = new JButton("Okay");
            btnOkay.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    //play sound:
                    try {
                         // Open an audio input stream.           
                          File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/btnclicksfx.wav"); //you could also get the sound file with a URL
                          AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
                         // Get a sound clip resource.
                         Clip clip = AudioSystem.getClip();
                         // Open audio clip and load samples from the audio input stream.
                         clip.open(audioIn);
                         clip.start();
                      } catch (UnsupportedAudioFileException ex) {
                         ex.printStackTrace();
                      } catch (IOException ex) {
                         ex.printStackTrace();
                      } catch (LineUnavailableException ex) {
                         ex.printStackTrace();
                      }

                    {

                    }
                    // go to Start Game class:
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                           dispose();
                            new StartGame().setVisible(true);
                        }
                    });

                }
            });

            //set playericon:


            btnOkay.setBounds(335, 227, 89, 23);
            contentPane.add(btnOkay);

            JButton btngenderback = new JButton("<");
            btngenderback.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    //play sound:
                    try {
                         // Open an audio input stream.           
                          File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/btnclicksfx.wav"); //you could also get the sound file with a URL
                          AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
                         // Get a sound clip resource.
                         Clip clip = AudioSystem.getClip();
                         // Open audio clip and load samples from the audio input stream.
                         clip.open(audioIn);
                         clip.start();
                      } catch (UnsupportedAudioFileException ex) {
                         ex.printStackTrace();
                      } catch (IOException ex) {
                         ex.printStackTrace();
                      } catch (LineUnavailableException ex) {
                         ex.printStackTrace();
                      }

                    //go to ProfileHome class:
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            dispose();
                            new ProfileHome().setVisible(true);
                        }
                    });
                }
            });
            btngenderback.setBounds(0, 0, 41, 15);
            contentPane.add(btngenderback);

            JLabel Genderbg = new JLabel("");
            Genderbg.setIcon(new ImageIcon("C:\\Users\\WhiteFringe\\Pictures\\Wallpapers\\9gag\\Gifs\\llkokigiphy.gif"));
            Genderbg.setBounds(0, 0, 434, 261);
            contentPane.add(Genderbg);
    }
}

类StartGame:

    import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.JLabel;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class StartGame extends JFrame {

    public JPanel contentPane;
    //protected ImageIcon playerMale = new ImageIcon("male warrior.png");
    //protected ImageIcon playerFemale = new ImageIcon("female warrior.png");

    protected ImageIcon playericon = new ImageIcon();


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StartGame frame = new StartGame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public StartGame() {

        String[] gametext = new String[100];
        String t1, t2;


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent Escape) {

            }
        });

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenuItem mntmPause = new JMenuItem("Pause");
        mntmPause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //play sound:
                try {
                     // Open an audio input stream.           
                      File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/pausesfx.wav"); //you could also get the sound file with a URL
                      AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
                     // Get a sound clip resource.
                     Clip clip = AudioSystem.getClip();
                     // Open audio clip and load samples from the audio input stream.
                     clip.open(audioIn);
                     clip.start();
                  } catch (UnsupportedAudioFileException e) {
                     e.printStackTrace();
                  } catch (IOException e) {
                     e.printStackTrace();
                  } catch (LineUnavailableException e) {
                     e.printStackTrace();
                  }

            //go to PauseMenu class:
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                       setVisible(false);
                        new PauseMenu().setVisible(true);
                    }
                });
            }
        });
        menuBar.add(mntmPause);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel playerbattle = new JLabel();
        playerbattle.setIcon(playericon);
        playerbattle.setBounds(0, 0, 194, 250);
        contentPane.add(playerbattle);

        JButton btnNext = new JButton("Attack");
        btnNext.setFont(new Font("Tahoma", Font.PLAIN, 9));
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        btnNext.setBounds(355, 219, 69, 18);
        contentPane.add(btnNext);

        ImageIcon enemyicon = new ImageIcon("enemy1.png");

        JLabel enemybattle = new JLabel(enemyicon);
        enemybattle.setBounds(309, 79, 94, 147);
        contentPane.add(enemybattle);

        JLabel battle1bg = new JLabel("");
        battle1bg.setIcon(new ImageIcon("C:\\My Awesome Stuff\\Personal\\Carman\\My Eclipse Programs\\Game\\battle1.jpg"));
        battle1bg.setBounds(0, 0, 434, 237);
        contentPane.add(battle1bg);


    }
}

如果某些内容仍然不清楚,以下是JFrame的屏幕截图: 性别: Image link:

StartGame: ImageLink: My playerbattle label should show on the left hand side (I've already inserted the label there)

最小,完整,可验证,例如:

性别:

    import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MDVGenders extends JFrame {

    private JPanel contentPane;

    //Initialise ImageIcons:
    protected ImageIcon avtMale = new ImageIcon("male warrior.png");
    protected ImageIcon avtFemale = new ImageIcon("female warrior.png");

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MDVGenders frame = new MDVGenders();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MDVGenders() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel Avatar = new JLabel("");
        Avatar.setBounds(10, 11, 134, 239);
        contentPane.add(Avatar);

        JButton btnMale = new JButton("Male");
        btnMale.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Avatar.setIcon(avtMale);
            }
        });
        btnMale.setBounds(335, 45, 89, 23);
        contentPane.add(btnMale);

        JButton btnOkay = new JButton("Okay");
        btnOkay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                // go to MDVStartGame class:
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        dispose();
                        new MDVStartGame().setVisible(true);
                    }
                });
            }
        });
        btnOkay.setBounds(335, 227, 89, 23);
        contentPane.add(btnOkay);

        JButton btnFemale = new JButton("Female");
        btnFemale.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Avatar.setIcon(avtFemale);
            }
        });
        btnFemale.setBounds(335, 11, 89, 23);
        contentPane.add(btnFemale);
    }

}

StartGame:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class MDVStartGame extends JFrame {

    private JPanel contentPane;
    protected ImageIcon playericon = new ImageIcon();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MDVStartGame frame = new MDVStartGame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MDVStartGame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel playerbattle = new JLabel(playericon);
        playerbattle.setBounds(10, 11, 119, 239);
        contentPane.add(playerbattle);
    }

}

1 个答案:

答案 0 :(得分:0)

为StartGame类创建一个新的构造函数

public StartGame(ImageIcon genderImage){

 Avatar.setIcon(avtMale);

 Avatar.setIcon(avtFemale);

 //add this line:

 inUseGender = avtMale;

}

在你的性别等级

//在inUseGender中声明一个ImageIcon对象

ImageIcon inUseGender;

每行之后

    dispose();

   new StartGame(inUseGender).setVisible(true);

主要方法:

@override

public void run(){

PCL_INSTANTIATE(function,template);

}