我有一个gui程序,我不知道在方法鼠标点击中做什么,以使程序工作

时间:2014-03-25 21:58:29

标签: java

这个程序不起作用,因为我不知道方法mouseClick的代码。有人可以帮助我吗。我是新手,但是java新手。

          import java.awt.*;
            import javax.swing.*;
          import java.awt.event.MouseListener;
           import java.awt.event.MouseEvent;
          import java.awt.event.MouseAdapter;
            public class PracticeButton extends JFrame

          {
    JPanel pa;
    JButton bu;
    JLabel vi;
    JLabel fried;
    public PracticeButton()
    {
    super ("Practicing with Buttons");
    setLayout (new  FlowLayout());

    pa=new JPanel();
    add(pa);  //adding a panel


    ImageIcon ce= new ImageIcon ("ceviche.jpg");
    vi=new JLabel(ce);
    add(vi);


    bu= new JButton ("Click  Here to change the image");
    add(bu);  //adding a button


     fried = new JLabel ("friedcow.jpg");
    //cow=new JLabel(fried);
    //add(cow);


   MouseHandler handler = new MouseHandler();
   bu.addMouseListener(handler);



}//end constructor

public class MouseHandler extends  MouseAdapter   // see  fig 14.21  page 581
{
    public void mouseClicked (MouseEvent event )

    {
if((event.getButton() == MouseEvent.BUTTON1))

  {

    //I DO NOT KNOW WHAT GOES HERE.
    //I WANT THAT THE PICTURE CHANGES
    //WHEN I CLICK BUTTON bu.


   }

}
}`enter code here`

1 个答案:

答案 0 :(得分:0)

在鼠标点击按钮中,您可以执行以下操作:

newPicture = new ImageIcon("image/newPicture.png");
fried.setIcon(newPicture);

这将改变炒JLabel 中包含的图片。您可以使用其他 JLabel 对象替换

如果您有任何疑问,欢迎您:)