简体   繁体   中英

how to display a bar when mouse cursor is moved to a corner?

In windows 8 when a user moves his mouse cursor to the right a control bar with some buttons is shown. So, can this be done in a java application? I want that when a user moves his mouse cursor to left a similar control bar pops-up with some buttons. Is something like that possible? Note: My java application fits the width and height of the screen(fullscreen application).

You can make use of a MouseMotionListener on the contentPane of the frame you are using. And within the MouseMotionListener get the mouse position and compare with the value of the corner pixel.

You can do something like

getContentPane().addMouseMotionListener(new MouseMotionAdapter(){

    @Override
    public void mouseMoved(MouseEvent e) {
        if(e.getX() == 0) //for left corner
            charmsPanel.setVisible(true);   
    }

});

Here's an example based on @VamshiAlladi 's answer:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example extends JFrame {

    public Example() {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();

        JPanel glass = (JPanel) getGlassPane();
        glass.setLayout(new BorderLayout());

        JPanel bar = new JPanel();
        bar.setLayout(new GridLayout(10, 1));
        for (int i = 1; i <= 10; i++) {
            bar.add(new JButton("Button " + i));
        }

        addMouseMotionListener(new MouseMotionListener() {
            public void mouseMoved(MouseEvent e) {
                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int) b.getX();
                int y = (int) b.getY();
                if (x > Example.this.getWidth() - 10) {
                    glass.setVisible(true);
                } else {
                    glass.setVisible(false);
                }
                revalidate();
                repaint();
            }

            public void mouseDragged(MouseEvent e) {
            }
        });

        setSize(width, height);
        glass.add(bar, BorderLayout.EAST);
        setVisible(true);

    }

    public static void main(String[] args) {
        new Example();
    }

}

Edit:

Why should you use GlassPane and not just a new JPanel ? - If you have components on your ContentPane in the area where the bar will be shown, a (for example) glasspane "overlays" the contentpane. With a JPanel you would have to replace the components (which can cause problems).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM