java swt 中的悬浮框

这次写的小程序中要使用悬浮框。网上关于swt的资料很少。最后和同学讨论了半天,决定使用java swing awt的JWindow  来做。由于那个悬浮框是根据鼠标的坐标来改变位置的。所以你获取鼠标坐标的时候要获取在整个屏幕中的位置,因为你的主框有可能会移动

下边的代码是写的一个悬浮框的类

package testJwindow;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class thewindow01 implements ActionListener{
  JWindow window1 = new JWindow();
  JPanel panel = new JPanel();
  JLabel label01= new JLabel("双击显示答案");
  JButton btnexit = new JButton("Exit");
  Color  color;
  public  thewindow01(Point point)
  {
  window1.setLocation(point.x+20,point.y+30);
   window1.setSize(120, 40);
   window1.getContentPane().setLayout(null);
   window1.getContentPane().add(panel);  
   window1.setVisible(true);
   panel.setLayout(null);
   panel.setLocation(0, 0);
   panel.setSize(400, 300);
   label01.setLocation(10, 10);
   label01.setSize(100, 30);
 
   panel.add(label01);
   color=new Color(52,32,63);
label01.setForeground(Color.red);
  
  
  // window1.setVisible(false);
   window1.setAlwaysOnTop(true);
  }
 
  public void actionPerformed(ActionEvent e)
  {
   if(e.getSource() == btnexit)
   {
   window1.dispose();
   }
  }
 
 
  public void close()
  {
   window1.dispose();
  }
 
  public  void move(int x,int y)
  {
   window1.setLocation(x+20, y+30);
  
  }
  public  void hide(int i)
  {
   if(i==1)
   window1.setVisible(false);
   else
    window1.setVisible(true);
  }
  public static void main(String[] args)
  {
   //thewindow01 m = new thewindow01();
  }
 
}
测试的例子

package UsingSwingSwt;

import java.awt.MouseInfo;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import testJwindow.thewindow01;

 

public class SWTPane implements  MouseMoveListener,MouseTrackListener {
//显示信息的标签
Label myLabel = null;
Shell shell = null;
static int time=0;
thewindow01  window;
public SWTPane() {
}
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setSize(450, 200);


Point point = MouseInfo.getPointerInfo().getLocation();
window=new thewindow01(point);


shell.setText("Mouse Event Example");
myLabel = new Label(shell, SWT.BORDER);
myLabel.setText("I ain't afraid of any old mouse");
//在当前窗口上添加鼠标响应事件

shell.addMouseMoveListener(this);
shell.addMouseTrackListener(this);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
window.close();
display.dispose();
}
public static void main(String[] args) {
new SWTPane().run();
}
public void mouseDoubleClick(MouseEvent e) {

 myLabel.setText("Mouse click at: " + e.x + "," + e.y);
 System.out.println("Mouse click at: " + e.x + "," + e.y);
}

public void mouseDown(MouseEvent e) {
 myLabel.setText("Mouse sown at: " + e.x + "," + e.y);
 System.out.println("Mouse down at: " + e.x + "," + e.y);
 
}
public void mouseUp(MouseEvent e) {
 myLabel.setText("Mouse up at: " + e.x + "," + e.y);
 System.out.println("Mouse up at: " + e.x + "," + e.y);
}
public void mouseMove(final MouseEvent e) {
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
 
 Point point = MouseInfo.getPointerInfo().getLocation();
 window.move(point.x, point.y);
}


});

}
public void mouseEnter(MouseEvent e) {

}
public void mouseExit(MouseEvent e) {

}
public void mouseHover(MouseEvent e) {
myLabel.setText("Mouse Hover at: " + e.x + "," + e.y);
System.out.println("Mouse Hover at: " + e.x + "," + e.y);
}
}

 测试的用例是从网上临时找的一个代码,出处忘记了。

 截图效果,如果有什么不合理的地方,请大家说出来

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值