拖动和调整未修饰的 JFrame

目前,我正在使用以下代码来拖动和移动未修饰的 JFrame.

private void initialiseGUI(Component component){//<editor-fold defaultstate="collapsed" desc="code">component.addMouseListener(new MouseAdapter() {公共无效鼠标按下(鼠标事件e){posX = e.getX();posY = e.getY();}});component.addMouseMotionListener(new MouseAdapter() {公共无效鼠标拖动(鼠标事件EVT){//设置鼠标拖动时的帧位置矩形矩形 = getBounds();getGUI().setBounds(evt.getXOnScreen() - posX, evt.getYOnScreen() - posY, rectangle.width, rectangle.height);}});//</编辑器折叠>}

我必须写什么,以便用户可以像装饰窗口一样通过拖动边来调整它的大小?

解决方案

您可以查看 Rob Camick 先生的

import java.awt.*;导入 java.awt.event.*;导入 javax.swing.*;导入 javax.swing.border.LineBorder;公共类 UndecoratedExample {私有 JFrame 框架 = new JFrame();类 MainPanel 扩展 JPanel {公共主面板(){设置背景(颜色.灰色);}@覆盖公共维度 getPreferredSize() {返回新维度(400, 400);}}类 BorderPanel 扩展 JPanel {私人 JLabel 标签;整数 pX, pY;公共边框面板(){标签 = 新的 JLabel("X");标签.setOpaque(true);label.setBackground(Color.RED);label.setForeground(Color.WHITE);设置背景(颜色.黑色);setLayout(new FlowLayout(FlowLayout.RIGHT));添加(标签);label.addMouseListener(new MouseAdapter() {公共无效鼠标释放(鼠标事件e){System.exit(0);}});addMouseListener(new MouseAdapter() {公共无效鼠标按下(鼠标事件我){//获取 x,y 并存储它们pX = me.getX();pY = me.getY();}公共无效鼠标拖动(鼠标事件我){frame.setLocation(frame.getLocation().x + me.getX() - pX,frame.getLocation().y + me.getY() - pY);}});addMouseMotionListener(new MouseMotionAdapter() {公共无效鼠标拖动(鼠标事件我){frame.setLocation(frame.getLocation().x + me.getX() - pX,frame.getLocation().y + me.getY() - pY);}});}}类 OutsidePanel 扩展 JPanel {公共外部面板(){设置布局(新边框布局());添加(新 MainPanel(),BorderLayout.CENTER);添加(新的 BorderPanel(),BorderLayout.PAGE_START);setBorder(new LineBorder(Color.BLACK, 5));}}私人无效createAnsShowGui(){ComponentResizer cr = new ComponentResizer();cr.setMinimumSize(新维度(300, 300));cr.setMaximumSize(新维度(800, 600));cr.registerComponent(frame);cr.setSnapSize(新维度(10, 10));frame.setUndecorated(true);frame.add(new OutsidePanel());frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}公共静态无效主要(字符串[]参数){SwingUtilities.invokeLater(new Runnable() {公共无效运行(){新的 UndecoratedExample().createAnsShowGui();}});}}

Currently, I am using the following code to drag and move my undecordated JFrames.

private void initialiseGUI(Component component){
    //<editor-fold defaultstate="collapsed" desc="code">
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });

    component.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent evt) {
            //sets frame position when mouse dragged            
            Rectangle rectangle = getBounds();
            getGUI().setBounds(evt.getXOnScreen() - posX, evt.getYOnScreen() - posY, rectangle.width, rectangle.height);
        }
    });
    //</editor-fold>
}

What must I write so that the user can resize it like a decorated window, by dragging the side?

解决方案

You can check out mr Rob Camick's ComponentResizer class. Pretty simple and straight forward to use.

Just instantiate the ComponentResizer and register the frame with something like:

JFrame frame = new JFrame();
ComponentResizer cr = new ComponentResizer();
cr.registerComponent(frame);
cr.setSnapSize(new Dimension(10, 10));
cr.setMaximumSize(new Dimension(...));
cr.setMinimumSize(new Dimension(...));

Here's a complete example of using the class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class UndecoratedExample {

    private JFrame frame = new JFrame();

    class MainPanel extends JPanel {

        public MainPanel() {
            setBackground(Color.gray);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }

    class BorderPanel extends JPanel {

        private JLabel label;
        int pX, pY;

        public BorderPanel() {
            label = new JLabel(" X ");
            label.setOpaque(true);
            label.setBackground(Color.RED);
            label.setForeground(Color.WHITE);

            setBackground(Color.black);
            setLayout(new FlowLayout(FlowLayout.RIGHT));

            add(label);

            label.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                    System.exit(0);
                }
            });
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent me) {
                    // Get x,y and store them
                    pX = me.getX();
                    pY = me.getY();

                }

                 public void mouseDragged(MouseEvent me) {

                    frame.setLocation(frame.getLocation().x + me.getX() - pX,
                            frame.getLocation().y + me.getY() - pY);
                }
            });

            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent me) {

                    frame.setLocation(frame.getLocation().x + me.getX() - pX,
                            frame.getLocation().y + me.getY() - pY);
                }
            });
        }
    }

    class OutsidePanel extends JPanel {

        public OutsidePanel() {
            setLayout(new BorderLayout());
            add(new MainPanel(), BorderLayout.CENTER);
            add(new BorderPanel(), BorderLayout.PAGE_START);
            setBorder(new LineBorder(Color.BLACK, 5));
        }
    }

    private void createAnsShowGui() {
        ComponentResizer cr = new ComponentResizer();
        cr.setMinimumSize(new Dimension(300, 300));
        cr.setMaximumSize(new Dimension(800, 600));
        cr.registerComponent(frame);
        cr.setSnapSize(new Dimension(10, 10));
        frame.setUndecorated(true);
        frame.add(new OutsidePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new UndecoratedExample().createAnsShowGui();
            }
        });
    }
}

相关文章