拖动和调整未修饰的JFrame [英] Drag and Resize undecorated JFrame

查看:131
本文介绍了拖动和调整未修饰的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用以下代码来拖动和移动未声明的JFrame。

  private void initialiseGUI(Component component) {
//< editor-fold defaultstate =collapseddesc =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){
//设置鼠标拖动时的帧位置
Rectangle rectangle = getBounds( );
getGUI()。setBounds(evt.getXOnScreen() - posX,evt.getYOnScreen() - posY,rectangle.width,rectangle.height);
}
});
//< / editor-fold>
}

我必须写什么才能让用户像装饰窗口一样调整大小,通过拖动一边?

解决方案

您可以查看Rob Camick先生的

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

public class UndecoratedExample {

private JFrame frame = new JFrame();

类MainPanel扩展JPanel {

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

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

类BorderPanel扩展JPanel {

私人JLabel标签;
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){
//获取x,y并存储它们
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扩展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();
}
});
}
}


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

这篇关于拖动和调整未修饰的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆