import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.JLabel;public class face
{
 public static void main(String [] argv)
 {
face_frame frame=new face_frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
  }
} class face_frame extends JFrame
  {
    public face_frame()
    {
     setTitle("Face Change");
     setSize(400,400);
     JPanel panel=new JPanel();
 JButton cryButton =new JButton("流泪");
 JButton smileButton =new JButton("微笑");
     JButton angryButton =new JButton("生气");
     JButton exitButton =new JButton("退出");
      panel.add(cryButton);
      panel.add(smileButton);
      panel.add(angryButton);
      panel.add(exitButton);
      //add(panel,"South");
    JLabel   pLabel   =   new   JLabel("");
      add(pLabel,"Center");
      setLayout(null);
      add(panel);
      panel.setBounds(0,300,300,45);
    //监听器对象的生成
      FaceAction cryAction=new FaceAction("cry.gif");
      FaceAction smileAction=new FaceAction("smile.gif");
      FaceAction angryAction=new FaceAction("angry.gif");    //事件源在监听器中的注册
     cryButton.addActionListener(cryAction);
     smileButton.addActionListener(smileAction);
     angryButton.addActionListener(angryAction);
     exitButton.addActionListener(new ActionListener()    //退出事件      {   public void actionPerformed(ActionEvent event)     {
System.exit(0); }
});     } JLabel   pLabel   =   new   JLabel("");     public   class FaceAction implements ActionListener       // 定义监听器    {      public FaceAction(String filename)      {   name=filename;
                  picture = new ImageIcon(name);      }      public void actionPerformed(ActionEvent event)
       {
                                   add(pLabel,"Center");
           pLabel.setIcon(picture);//为什么picture出现在face_frame面板的左边?怎么让它出现在面板的中央?  } private ImageIcon picture; } private String name; }/*
  JLabel   pLabel   =   new   JLabel("");
  ImageIcon   picture   =   new   ImageIcon("图片名");
  pLabel.setIcon(picture);
*/

解决方案 »

  1.   

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class face {
    public static void main(String[] argv) {
    face_frame frame = new face_frame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class face_frame extends JFrame {
    public face_frame() {
    setTitle("Face Change");
    setSize(400, 400);
    setLayout(new BorderLayout());

    JPanel panel = new JPanel();
    JButton cryButton = new JButton("流泪");
    JButton smileButton = new JButton("微笑");
    JButton angryButton = new JButton("生气");
    JButton exitButton = new JButton("退出");
    panel.add(cryButton);
    panel.add(smileButton);
    panel.add(angryButton);
    panel.add(exitButton);
    // add(panel,"South");
    /*JLabel*/ pLabel = new JLabel("Emotion");
    add(new JLabel("East"), BorderLayout.EAST);
    add(new JLabel("West"), BorderLayout.WEST);
    add(new JLabel("North"), BorderLayout.NORTH);
    add(pLabel, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);
    panel.setBounds(0, 300, 300, 45); // 监听器对象的生成
    FaceAction cryAction = new FaceAction("cry.gif");
    FaceAction smileAction = new FaceAction("smile.gif");
    FaceAction angryAction = new FaceAction("angry.gif"); // 事件源在监听器中的注册
    cryButton.addActionListener(cryAction);
    smileButton.addActionListener(smileAction);
    angryButton.addActionListener(angryAction); exitButton.addActionListener(new ActionListener() // 退出事件 { public void actionPerformed(ActionEvent event) {
    System.exit(0); }
    }); } JLabel pLabel = new JLabel("Emotion"); public class FaceAction implements ActionListener // 定义监听器 { public FaceAction(String filename) { name = filename;
    picture = new ImageIcon(name); } public void actionPerformed(ActionEvent event) {
    add(pLabel, "Center");
    System.out.println(picture.getImageLoadStatus());
    pLabel.setIcon(picture);// 为什么picture出现在face_frame面板的左边?怎么让它出现在面板的中央? } private ImageIcon picture; } private String name;}/*
     * JLabel pLabel = new JLabel(""); ImageIcon picture = new ImageIcon("图片名"); pLabel.setIcon(picture);
     */