[英]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. 在Windows 8中,当用户将鼠标光标向右移动时,将显示带有某些按钮的控制栏。 So, can this be done in a java application?
那么,这可以在Java应用程序中完成吗? 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).
注意:我的Java应用程序适合屏幕(全屏应用程序)的宽度和高度。
You can make use of a MouseMotionListener
on the contentPane
of the frame you are using. 您可以在要使用的帧的
contentPane
上使用MouseMotionListener
。 And within the MouseMotionListener
get the mouse position and compare with the value of the corner pixel. 并在
MouseMotionListener
获取鼠标位置,并与角点像素的值进行比较。
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: 这是一个基于@VamshiAlladi的答案的示例:
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
? 为什么要使用
GlassPane
而不是新的JPanel
? - If you have components on your ContentPane
in the area where the bar will be shown, a (for example) glasspane "overlays" the contentpane. -如果您的
ContentPane
上有显示条的区域中有组件,则玻璃窗格(例如)“覆盖”内容窗格。 With a JPanel you would have to replace the components (which can cause problems). 使用JPanel,您将不得不更换组件(这可能会导致问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.