简体   繁体   English

我有一个gui程序,我不知道要在mouseclick方法中做什么才能使该程序正常工作

[英]I have a gui program that I do not know what to do in the method mouseclick in order to make the program to work

This program does not work because I do not know the code for the method mouseClick. 该程序不起作用,因为我不知道方法mouseClick的代码。 Could somebody help me with that.I am new, but new in java. 有人可以帮我吗。我是新手,但是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`

In your mouseclick button you can make this : 在您的mouseclick按钮中,您可以进行以下操作:

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

This will change the picture contain in fried JLabel . 这将更改油炸JLabel中包含的图片。 You can replace fried by another JLabel object. 您可以通过其他的JLabel对象替换

If you have any question, you are welcome :) 如有任何疑问,欢迎您:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的Java程序中有一个错误,我认为这是一个小错误,但我不知道该怎么办? - I have an error in my Java program and I think it's a minor error but I don't know what to do? 为什么必须使用Object类作为引用类型才能使该程序正常工作? - Why do I have to use the Object class as a reference type to make this program work? 我怎么知道“程序文件”在哪里? - How do I know where “Program Files” are? 如何在不暂停整个程序的情况下暂停方法? - How do I make method pause without pausing the whole program? 如何用一种方法制作最简单的Java程序? - How do I make the simplest Java program with one method? 在我制作的另一个程序中按一个按钮后,如何使已创建的程序运行? - How do I make a program that I have already created run after I press a button in a different program that I have made? 如何从Java应用程序拨打语音电话? 如何使该程序正常工作? - How do I make a voice call from my Java app? How do I make this program work? 如何使ActionListener按钮在我的Java GUI程序中保持骰子? - how do i make an ActionListener button to hold a dice in my java GUI program? 如何制作HSQLDB 2客户端程序? - How do I make a HSQLDB 2 client program? 如何使我的程序继续进行? - How do I make my program continue on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM