Java - 自定义形状的可拖动JFrame

时间:2014-04-04 09:15:50

标签: java swing jframe

因此我使用setShape(s);制作了自定义形状的JFrame 当我将JFrame设置为未装饰时,我看到了我想要的问题,你不能用鼠标在屏幕上拖动框架,所以我试图实现我自己的可拖动框架,但它不能正常工作,这是Frame类:

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);
    }
}

}

但是拖动动作没有按照我的预期运行,我猜问题就在于这些问题: 有人介意检查和解释我做错了什么以及如何解决它? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

我认为你的问题在执行mouseDragged时担心框架的形状。我认为你不必担心这一点。

如果您只是将方法改为以下方法,则可行。测试一下。

void updateFrameCoords(MouseEvent e) {
    setLocation(getLocation().x + e.getX() - initialPressedX,
                getLocation().y + e.getY() - initialPressedY);
}

答案 1 :(得分:0)

  1. 您必须在每次拖动时更新初始坐标
  2. evt.getX()提供相对于框架的坐标。您应该使用evt.getXOnScreen()来代替。
  3. 替换

    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();
    }
    

    void updateFrameCoords(MouseEvent e) {
        int dx = e.getXOnScreen() - initialPressedX;
        int dy = e.getYOnScreen() - initialPressedY;
        for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
            OCTAGON_COORDS_X[i] += dx;
            OCTAGON_COORDS_Y[i] += dy;
        }
        updateFrame();
        initialPressedX = e.getXOnScreen();
        initialPressedY = e.getYOnScreen();
    }
    

    mousePressed事件相同。

    祝你好运。