3

我使用 JPanel 扩展了 Mac 上的工具栏(参见图片),但作为实际工具栏的部分(不是 JPanel)是唯一可以单击和拖动的部分。如何允许用户单击并拖动 JPanel 以移动窗口,就像他们移动工具栏一样

图像的顶部毫米左右是实际的工具栏(带有文本),其余的是 JPanel(带有按钮)。 在此处输入图像描述

这是 UnifiedToolPanel 的代码,它设置在 JFrame 中边框布局的北边:

package gui;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.Borders;

public class UnifiedToolbarPanel extends JPanel implements WindowFocusListener {

    public static final Color OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR =
            new Color(64, 64, 64);
    public static final Color OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR =
            new Color(135, 135, 135);    

    public static final Color OS_X_TOP_FOCUSED_GRADIENT = new Color(214+8, 214+8, 214+8);
    public static final Color OS_X_BOTTOM_FOCUSED_GRADIENT = new Color(217, 217, 217);
    public static final Color OS_X_TOP_UNFOCUSED_GRADIENT = new Color(240+3, 240+3, 240+3);
    public static final Color OS_X_BOTTOM_UNFOCUSED_GRADIENT = new Color(219, 219, 219);


    public UnifiedToolbarPanel() {
        // make the component transparent
        setOpaque(true);
        Window window = SwingUtilities.getWindowAncestor(this);
        // create an empty border around the panel
        // note the border below is created using JGoodies Forms
        setBorder(Borders.createEmptyBorder("3dlu, 3dlu, 1dlu, 3dlu"));
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Window window = SwingUtilities.getWindowAncestor(this);
        Color color1  = window.isFocused() ? OS_X_TOP_FOCUSED_GRADIENT
                : OS_X_TOP_UNFOCUSED_GRADIENT;
        Color color2 = window.isFocused() ? color1.darker()
                : OS_X_BOTTOM_UNFOCUSED_GRADIENT;
        int w = getWidth();
        int h = getHeight();
        GradientPaint gp = new GradientPaint(
            0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }

    @Override
    public Border getBorder() {
        Window window = SwingUtilities.getWindowAncestor(this);
        return window != null && window.isFocused()
                ? BorderFactory.createMatteBorder(0,0,1,0,
                        OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR)
                : BorderFactory.createMatteBorder(0,0,1,0,
                       OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR);
    }


    @Override
    public void windowGainedFocus(WindowEvent e) {
        repaint();
    }


    @Override
    public void windowLostFocus(WindowEvent e) {
        repaint();
    }
}
4

2 回答 2

4

如何允许用户单击并拖动 JPanel 以移动窗口
这是方法:

   private int x;
    private int y;
    //.....
    //On mouse pressed:
    jpanel.addMouseListener(new MouseAdapter(){
       public void mousePressed(MouseEvent ev){
        x = ev.getX ();
        y = ev.getY();
       }
    });
    //....
    //on mouse dragged
    jpanel.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent evt) {
                    int x = evt.getXOnScreen()-this.x;
                    int y = evt.getYOnScreen -this.y;
                    this.setLocation(x,y); 

                }
            });

this.setLocation(x,y)将移动框架而不是面板,我认为您的课程扩展了JFrame
但是,您可以创建一个返回点 (x,y) 的方法并将其设置为window

于 2013-05-24T15:58:09.370 回答
0

课堂上没有真正的答案,Point所以我将添加我的贡献而不是存储x, yMouseEvent 的绳索,我们存储Point

这是展示你可以做到的,定义类的全局变量java.awt.Point

private Point currentLocation;

然后我们将点存储到currentLocation一旦按下鼠标使用MouseListener

panel.addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
       currentLocation = e.getPoint();
    }
});

我们使用拖动鼠标时设置 JFrame 位置MouseMotionListener

panel.addMouseMotionListener(new MouseAdapter() {
    public void mouseDragged(MouseEvent e) {
        Point currentScreenLocation = e.getLocationOnScreen();
        setLocation(currentScreenLocation.x - currentLocation.x, currentScreenLocation.y - currentLocation.y);
    }
});

我们将所有代码放在一个 HelperMethod 中,以便在任何地方使用

public void setDraggable(JPanel panel) {
    panel.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            currentLocation = e.getPoint();
        }
    });

    panel.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            Point currentScreenLocation = e.getLocationOnScreen();
            setLocation(currentScreenLocation.x - currentLocation.x, currentScreenLocation.y - currentLocation.y);
        }
    });
}

我将我的 HelperMethod 存储在一个扩展的类中,JFrame因此为什么我可以访问setLocation属于 JFrame 而没有变量。

于 2018-11-15T01:45:30.813 回答