今天无聊在网上找了个SWING的无边框后可拖拽的例子,信心能不能在wptyhon上也能做成这样的吗?
就按照SWING的原来用wpython实现了。
以下是代码:
#!/usr/bin/python
# Filename : wpy.py
# -*- coding:UTF-8 -*-
import wx
class InsertFrame(wx.Frame):
#-----------------------------------
# Function
#-----------------------------------
def __init__ (self,parent,id):
self.mouseX = 0; # 记入下鼠标点击时X坐标
self.mouseY = 0; # 记入下鼠标点击时Y坐标
#wx.DEFAULT_FRAME_STYLE&(wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)
wx.Frame.__init__(self,parent,id,'Frame With Button',pos=(100,100),style=wx.SIMPLE_BORDER)
self.panel = wx.Panel(self,pos=(0,0), size=self.GetSize())
self.panel.Bind(wx.EVT_MOUSE_EVENTS,self.OnMousePressed)
self.button = wx.Button(self.panel,label='关闭')
self.button.Bind(wx.EVT_BUTTON,self.OnClose)
#-------------------------------
# 鼠标事件,第一个判断是否点击右键,第二个判断是否拖拽
#--------------------------------
def OnMousePressed(self,event):
if event.GetButton() == 1:
self.mouseX = event.X
self.mouseY = event.Y
if event.Dragging():
#计算出新的frame的position
self.SetPosition(self.GetPosition()+(event.X-self.mouseX,event.Y-self.mouseY))
#----------------------------------
# close
#----------------------------------
def OnClose(self,event):
self.Close(True)
#-----------------------------------
# Test
#-----------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = InsertFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
以下是来源网络的SWING实现,我就按照它的原来做的
package swing.test;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 利用鼠标监听解决无标题窗体的移动问题
*/
public class Test {
private JFrame frame = null;
JPanel pane = null;
JLabel label = null;
int mouseX = 0;
int mouseY = 0;
public Test() {
frame = new JFrame("一个没有边框的jfame");
frame.setUndecorated(true);
pane = (JPanel) frame.getContentPane();
label = new JLabel();
label.setText("一个没有边框的jframe可以拖动");
pane.add(label);
pane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
if (mouseEvent.getButton() == mouseEvent.BUTTON1) {
mouseX = mouseEvent.getX();
mouseY = mouseEvent.getY();
}
}
});
pane.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (e.getModifiers() == e.BUTTON1_MASK) {
frame.setLocation(frame.getX() + e.getX() - mouseX,
frame.getY() + e.getY() - mouseY);
}
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}
希望对大家有用,第一次发文章