Home > Software engineering >  Button and JSlider only appear when I hover the screen
Button and JSlider only appear when I hover the screen

Time:04-06

Can someone help guide me where my mistake is? It's stressing me out that the button and JSlider appear when I hover my mouse. It was fine when I'm using the button but as soon as i add a button the JSlider is missing and appear when I hover and click.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class MainDisplay extends JFrame implements MouseMotionListener, 
        MouseListener, ChangeListener, ActionListener{
    
    private Point mousepnt = new Point();
    public static Color penColor = new Color(255,105,180);
    private final JSlider penSize = new JSlider(JSlider.HORIZONTAL, 1, 30, 4);
    public static int pen = 4;
    JButton rbutton = new JButton();
    JButton cbutton = new JButton();
    JButton tbutton = new JButton();
    JButton qbutton = new JButton();
    ImageIcon ricon = new ImageIcon(getClass().getResource("ricon.png"));
    ImageIcon cicon = new ImageIcon(getClass().getResource("cicon.png"));
    ImageIcon ticon = new ImageIcon(getClass().getResource("ticon.png"));
    ImageIcon qicon = new ImageIcon(getClass().getResource("qicon.png"));

    public MainDisplay(){ 
        JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel toolbar2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel jp = new JPanel();
        jp.addMouseMotionListener(this);
        jp.addMouseListener(this);
        toolbar.add(new Label("Drag mouse to draw"));
        toolbar.add(penSize);
        penSize.addChangeListener(this);
        toolbar2.add(new Label("Choose shape to draw"));
        rbutton = new JButton(ricon);
        rbutton.addActionListener(this);
        rbutton.setMargin(new Insets(1, 2, 1, 2));
        toolbar2.add(rbutton);
        cbutton = new JButton(cicon);
        cbutton.addActionListener(this);
        cbutton.setMargin(new Insets(1, 2, 1, 2));
        toolbar2.add(cbutton);
        tbutton = new JButton(ticon);
        tbutton.addActionListener(this);
        tbutton.setMargin(new Insets(1, 2, 1, 2));
        toolbar2.add(tbutton);
        qbutton = new JButton(qicon);
        qbutton.addActionListener(this);
        qbutton.setMargin(new Insets(1, 2, 1, 2));
        toolbar2.add(qbutton);
    this.add(toolbar,BorderLayout.SOUTH);
        this.add(toolbar2,BorderLayout.NORTH);
        this.add(jp,BorderLayout.CENTER);
        setSize(800,600);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

Main class

public class Main {
    public static void main(String[] args){
    MainDisplay md = new MainDisplay();
    }
}

CodePudding user response:

What i would do:

Add a MouseMotionListener to the panel:

jp.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("motion"   e.getPoint());
            }
    });

Add a ChangeListener to the penSize:

penSize.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("new pensize: "   penSize.getValue());
            }
    });

And so on. So you have add the lsiteners to the components and not to the frame.

I hope to have helped you for now. If you have any questions, please post a minimal reproducible example as Andrew wrote.

Steffi

  • Related