[英]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.