java - MouseListener not responding even when I put in code which should work - Stack Overflow

package mainz;import java.awt.Color;import java.awt.event.MouseEvent;import java.awt.event.MouseListe

package mainz;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class myFrame extends JFrame implements MouseListener{

    JLabel label;


    myFrame(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLayout(null);
        
        label = new JLabel();
        label.setBounds(0, 0, 100, 100);
        label.setBackground(Color.red);
        label.setOpaque(true);
        label.addMouseListener(this);

        this.add(label);
        this.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        label.setBackground(Color.yellow);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

I need a lot of help. This code looks perfectly fine and I even got some working code from a bro code tutorial yet it still didn't work. Is there a problem with my client because it's been working properly all this time. Thank you for any help. I've tried everything yet it still doesn't work

package mainz;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class myFrame extends JFrame implements MouseListener{

    JLabel label;


    myFrame(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLayout(null);
        
        label = new JLabel();
        label.setBounds(0, 0, 100, 100);
        label.setBackground(Color.red);
        label.setOpaque(true);
        label.addMouseListener(this);

        this.add(label);
        this.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        label.setBackground(Color.yellow);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

I need a lot of help. This code looks perfectly fine and I even got some working code from a bro code tutorial yet it still didn't work. Is there a problem with my client because it's been working properly all this time. Thank you for any help. I've tried everything yet it still doesn't work

Share Improve this question edited Mar 6 at 5:56 Slaw 46.8k8 gold badges62 silver badges100 bronze badges asked Mar 6 at 5:42 Eyoel GeremewEyoel Geremew 312 bronze badges 3
  • And when should the mouse listener respond? When you click on the JFrame window or, when you click on the JLabel? – DevilsHnd - 退した Commented Mar 6 at 6:02
  • 2 With label.addMouseListener(this); you put a listener on the label, if you click on it, it changes color... :) – Marce Puente Commented Mar 6 at 6:13
  • The mouseClicked method is sensitive to the time between the mouse press and the mouse release. Using the mousePressed or mouseReleased methods is better. – Gilbert Le Blanc Commented Mar 6 at 15:19
Add a comment  | 

1 Answer 1

Reset to default 1

As was stated, your program works just fine. You need to click on the label to have it change color. Also note that a mouse click includes a release. So it won't change color until you release the button. You may want to try mousePressed which responds immediately. Here are some other things you should be aware of.

  • Class names should start with an upper case letter.

  • In general it's bad practice to use null layouts and absolute positioning. Use a layout manager or write your own.

  • Don't extend JFrame but do extend JPanel.. Then add the panel to an instance of JFrame.

  • Not required but reduces code, use a MouseAdapter. This and other similar adapter classes have default, empty implementations. All you need to do is override the ones you need.

  • Use SwingUtilities to start your program on the Event Dispatch Thread (EDT).

Here is your program, rewritten to include the above.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyProgram extends JPanel {

    public JLabel label = new JLabel();
    public JFrame frame = new JFrame();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new MyProgram().start());
    }

    public void start() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label.setBackground(Color.red);
        label.setPreferredSize(new Dimension(100,100));
        label.setOpaque(true);
        setBackground(Color.white); // 
        label.addMouseListener(new MyMouseListener());
        add(label); // add label to panel
        frame.add(this); // add the panel to the frame
        frame.pack();  // resize and position components
        frame.setVisible(true);
    }
    @Override
    public Dimension getPreferredSize() {
       return new Dimension(500,500);
    }
    
    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            label.setBackground(Color.yellow);
        }
    }
}

For more information, check out the The Java Tutorials for using Swing and custom painting.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744995049a4605137.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信