请问Java中如何写代码实现无标题无边框的窗体能够用鼠标拖动改变窗口大小

Python014

请问Java中如何写代码实现无标题无边框的窗体能够用鼠标拖动改变窗口大小,第1张

下面是一个类用来完成你所要求的:

class TestFrame extends JFrame

{

public TestFrame()

{

setTitle("")//设置无标题

setResizable(true)//设置用户可以改变窗口大小

setVisible(true)//窗体可见

}

}

至于所说的无边框,在没有给窗体设置大小时,系统默认就是无边框的

希望我的回答能使你满意,如果满意 请给我最佳答案,谢谢

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import java.awt.event.MouseMotionAdapter

import javax.swing.JFrame

public class test extends JFrame{

private int xx, yy

private boolean isDraging = false

public test(){

setUndecorated(true)// 没有标题栏

setSize(200, 200)

setVisible(true)

this.addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

isDraging = true

xx = e.getX()

yy = e.getY()

}

public void mouseReleased(MouseEvent e) {

isDraging = false

}

})

this.addMouseMotionListener(new MouseMotionAdapter() {

public void mouseDragged(MouseEvent e) {

if (isDraging) {

int left = getLocation().x

int top = getLocation().y

setLocation(left + e.getX() - xx, top + e.getY() - yy)

}

}

})

}

public static void main(String[] args) {

test t =new test()

t.setDefaultCloseOperation(3)

}

}

问题其实很简单,只是被绕进去了,呵呵。

少记录了一组变量,开始时的窗口位置,示例代码如下:

protected int dragStartMX,  dragStartMY// 开始拖动时的鼠标位置

protected int dragStartWX,  dragStartWY// 开始拖动时的窗口位置

// .........................

setLocation(dragStartWX + curMouseX - dragStartX      

          , dragStartWY + curMouseY  - dragStartY)