日期:2014-05-20  浏览次数:20716 次

java无标题窗体的拖动效果能否类似于有标题窗体的拖动效果呢,请问如何实现,谢谢。
也就是当鼠标按下开始拖动时,当前窗体不动,而是一个只有边框没有内容的的窗体在移动,当鼠标释放时,当前窗体位置变换到鼠标释放时,只有边框窗体的位置处.

------解决方案--------------------
你描述的内容,个人感觉好奇怪,向你确认下。
是否要2个窗口,一个只有边框的窗口,一个只有内容的窗口。拖动时,先移动边框窗口,移动结束后,再把内容窗口移到边框窗口内?
为什么要这样做呢?太奇怪了,是要这种奇怪的效果吗?
------解决方案--------------------
楼主,我想你的表述让我们错乱了
是否就是一个无标题的窗口,但是可以移动
若果是的话,给你一个思路,继承JWindow
窗体为一个白板,再自己添加鼠标监听类
来实现移动
package com.kiritor;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JWindow;

public class PFrame extends JWindow {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private boolean flag = false;

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

private int xx;
private int yy;
private boolean isDraging;

public PFrame() {
}

public PFrame(int x, int y) {
setLocation(x, y);
}

public void showUI() {
setSize(150, 150);
setLayout(null);
setAlwaysOnTop(true);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
requestFocus();
isDraging = true;
xx = e.getX();
yy = e.getY();
}

public void mouseReleased(MouseEvent e) {
isDraging = false;
}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
setFlag(true);// 窗体内部暂时设置为不能取色
}
});
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);
}
}
});

setVisible(true);

}

public static void main(String[] args) {
PFrame frame = new PFrame();
frame.showUI();

}

}


------解决方案--------------------
黑色框窗体写法的关键代码如下:
JFrame中:
setUndecorated(true);// remove the system border
com.sun.awt.AWTUtilities.setWindowOpaque(this, false);// make the frame transparent
第二行代码若编译器报错,那是因为新的JDK中去除了这个包。使用老一点的JDK,1.6或1.5就可以了。
上述两行代码的效果是:窗口的系统边框没了,整个窗口可以变透明了。
剩下的事,就是写一个拥有黑色边框的JPanel,加入此JFrame。
其他的,你应该都会吧?