所以我做了一个自定义的形状JFrame使用setShape(s);
我看到了我想要的问题,当你将JFrame设置为undecorated时,你不能用鼠标拖动框架屏幕上,所以我试图实现自己的可拖动框架,但应该是它不工作,这里的Frame类:Java - 自定义形状的可拖动JFrame
package rt;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
public class MyFrame extends JFrame{
private static final int WIDTH = 1024;
private static final int HEIGHT = 1024;
private static int [] OCTAGON_COORDS_X = {300, 524, 680, 680, 524, 300, 144, 144};
private static int [] OCTAGON_COORDS_Y = {300, 300, 457, 680, 836, 836, 680, 457};
public MyFrame() throws IOException {
// Determine what the default GraphicsDevice can support.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isUniformTranslucencySupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
boolean isShapedWindowSupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT);
if (isUniformTranslucencySupported && isShapedWindowSupported) {
setUndecorated(true);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
updateFrame();
//setOpacity(0.55f);
setVisible(true);
MyListener myListener = new MyListener();
addMouseListener(myListener);
addMouseMotionListener(myListener);
}
else {
System.err.println("Custom shaped JFrames and trunslucent JFrames are not supported by this device!");
System.exit(0);
}
}
public void updateFrame() {
Shape s = new Polygon (OCTAGON_COORDS_X, OCTAGON_COORDS_Y, OCTAGON_COORDS_X.length);
setShape(s);
}
private class MyListener extends MouseInputAdapter {
private int initialPressedX;
private int initialPressedY;
@Override
public void mousePressed(MouseEvent e) {
initialPressedX = e.getX();
initialPressedY = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
updateFrameCoords(e);
}
void updateFrameCoords(MouseEvent e) {
int dx = e.getX() - initialPressedX;
int dy = e.getY() - initialPressedY;
for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
OCTAGON_COORDS_X[i] += dx;
OCTAGON_COORDS_Y[i] += dy;
}
updateFrame();
}
}
}
主要类:
package rt;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
MyFrame frame = new MyFrame();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
但正如我预料我想问题出在那些线拖动动作不工作: 任何人的头脑检查和解释我做错了什么,以及如何解决它? 在此先感谢!
为了尽快获得更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可验证示例)。 –
好吧,如果有人想在这里看到完整的代码,它是:http://pastebin.com/fLx5hSDA - 只需在主类 – user3497284
*中创建一个新的MyFrame实例*“如果有人想看完整的代码”*我不喜欢不想看到“完整的代码”,也不会关注链接。如果您需要我的帮助(和许多人),请将[MCVE](http://stackoverflow.com/help/mcve)直接发送到[编辑](http://stackoverflow.com/posts/22858498/edit)其他人也会这么想)。 –