Java如何在窗口或JFrame的边框/周围放置按钮 [英] Java how to put a button on border/surround of window or JFrame

查看:78
本文介绍了Java如何在窗口或JFrame的边框/周围放置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在像嵌齿轮这样的框架周围的边框上放置一个按钮:

How do I put a button on the border that surrounds the frame like this cog:

推荐答案

_在任何地方都有一个简短的例子吗?"

_"Is the a short example anywhere? "

是的,这里...这很基本.您需要做更多的事情.您会注意到我必须在 JPanel 中添加一个 MouseMotionListener 作为顶部框架边框,因为当您从框架中删除装饰时,离开该功能.因此, MouseMotionListener 使框架再次可拖动.

Yea, here... This very basic. You need to do alot more to it. You'll notice I have to add a MouseMotionListener to the JPanel that acts as the top frame border, because when you remove the decoration from the frame, you're also taking away that functionality. So the MouseMotionListener makes the frame draggable again.

如果需要,还必须实现调整大小.按下图像时,我已经实现了 System exit()`.测试一下.您需要提供自己的图像.

You would also have to implement resizing if you wished. I already implemented the Systemexit()` when you press the image. Test it out. You need to provide your own image.

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

public class UndecoratedExample {
    static JFrame frame = new JFrame();
    static class MainPanel extends JPanel {

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

    static class BorderPanel extends JPanel {

        JLabel stackLabel;
        int pX, pY;

        public BorderPanel() {
            ImageIcon icon = new ImageIcon(getClass().getResource(
                    "/resources/stackoverflow1.png"));
            stackLabel = new JLabel();
            stackLabel.setIcon(icon);

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

            add(stackLabel);

            stackLabel.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();
                }
            });
            addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent me) {
                    frame.setLocation(frame.getLocation().x + me.getX() - pX, 
                            frame.getLocation().y + me.getY() - pY);
                }
            });
        }
    }

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

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

                frame.setUndecorated(true);
                frame.add(new OutsidePanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

这篇关于Java如何在窗口或JFrame的边框/周围放置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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