java statusbar resize corner(handle)

时间:2011-08-18 18:22:55

标签: java swing

我尝试在右下角创建一个带调整大小手柄的窗口。 到目前为止,我设法检测到鼠标悬停和拖动。

鼠标光标正在更改以成功调整光标大小。

但实际调整大小的操作我不确定如何解决。 我在第一个位置测试的想法是在拖动调整大小句柄时仅设置父级。 它可以工作,但随后窗口会立即调整大小,这不是标准调整大小的样子。 标准尺寸调整是一个带有白色边框的透明窗口(在不同的系统和外观上可能会有所不同)。

是否可以触发/使用内置的调整大小机制? 下面是一个示例代码。

谢谢!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class main extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                main frame = new main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public class StatusBar extends JPanel implements MouseListener, MouseMotionListener {

    private Polygon resizeCorner = new Polygon();
    private int offsetX;
    private int offsetY;
    private Dimension offsetSize;
    private Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
    private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);

    public StatusBar() {
        super();
        setPreferredSize(new Dimension(200,40));
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    private void createResizeHandle() {
        resizeCorner.reset();
        resizeCorner.addPoint(getWidth()-2, getHeight()-2);
        resizeCorner.addPoint(getWidth()-40, getHeight()-2);
        resizeCorner.addPoint(getWidth()-2, getHeight()-40);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2;
        g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g2.setColor(Color.red);
        createResizeHandle();
        g2.drawPolygon(resizeCorner);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
            int width = (int) (this.offsetSize.getWidth() - this.offsetX + e.getXOnScreen());
            int height = (int) (this.offsetSize.getHeight() - this.offsetY + e.getYOnScreen());
            this.getRootPane().getParent().setSize(width, height);
            createResizeHandle();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (resizeCorner.contains(e.getX(), e.getY())) {
            setCursor(resizeCursor);
        } else {
            setCursor(defaultCursor);
        }
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (resizeCorner.contains(e.getX(), e.getY())) {
            this.offsetX = e.getXOnScreen();
            this.offsetY = e.getYOnScreen();
            this.offsetSize = this.getRootPane().getParent().getSize();
        }
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}


public main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JLabel lblNewLabel = new JLabel("New label");
    StatusBar bar = new StatusBar();
    contentPane.add(lblNewLabel, BorderLayout.NORTH);
    contentPane.add(bar, BorderLayout.SOUTH);
}
}

2 个答案:

答案 0 :(得分:3)

许多小错误,从ClassName的正确命名开始...结束使用java保留字类/ void /方法的名称

enter image description here

微小的变化(现在它适用于我),一个我的错误反对Swing规则,我设置setPreferredSize(new Dimension(400, 300));,让孩子们返回PreferredSize for Container

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MyToolBar extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public class StatusBarX extends JPanel implements MouseListener, MouseMotionListener {

        private static final long serialVersionUID = 1L;
        private Polygon resizeCorner = new Polygon();
        private int offsetX;
        private int offsetY;
        private Dimension offsetSize;
        private Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
        private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);

        public StatusBarX() {
            super();
            setPreferredSize(new Dimension(200, 40));
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }

        private void createResizeHandle() {
            resizeCorner.reset();
            resizeCorner.addPoint(getWidth() - 2, getHeight() - 2);
            resizeCorner.addPoint(getWidth() - 40, getHeight() - 2);
            resizeCorner.addPoint(getWidth() - 2, getHeight() - 40);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2;
            g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2.setColor(Color.red);
            createResizeHandle();
            g2.drawPolygon(resizeCorner);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                int width = (int) (this.offsetSize.getWidth() - this.offsetX + e.getXOnScreen());
                int height = (int) (this.offsetSize.getHeight() - this.offsetY + e.getYOnScreen());
                this.getRootPane().getParent().setSize(width, height);
                createResizeHandle();
            }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            if (resizeCorner.contains(e.getX(), e.getY())) {
                setCursor(resizeCursor);
            } else {
                setCursor(defaultCursor);
            }
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (resizeCorner.contains(e.getX(), e.getY())) {
                this.offsetX = e.getXOnScreen();
                this.offsetY = e.getYOnScreen();
                this.offsetSize = this.getRootPane().getParent().getSize();
            }
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    }

    public MyToolBar() {
        JLabel lblNewLabel = new JLabel("New label");
        StatusBarX bar = new StatusBarX();
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        contentPane.add(lblNewLabel, BorderLayout.NORTH);
        contentPane.add(bar, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100, 100);
        setPreferredSize(new Dimension(400, 300));
        add(contentPane);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    MyToolBar frame = new MyToolBar();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

答案 1 :(得分:3)

  

标准尺寸调整是一个带有白色边框的透明窗口(在不同的系统和外观上可能会有所不同)。

使用Windows XP时,标准是立即调整顶级组件(框架,对话框等)的大小。 LAF是什么并不重要。

通常,其他组件没有内置调整大小功能。我能想到的唯一例外是JInternalFrame。在这种情况下,确实支持调整组件的“轮廓”大小。

因此,如果您要将此类功能添加到组件中,则需要查看内部框架UI以查找调整大小代码。

我猜测代码会在拖动开始时显示一个Glass Pane,然后在Glass Plane上进行轮廓自定义绘制。然后在mouseReleased上更改帧大小。

如果您有兴趣,Component Resizing会显示我如何为任何组件调整大小。它会立即调整大小,而不是概述调整大小。