简体   繁体   English

Java如何在窗口或JFrame的边框/周围放置按钮

[英]Java how to put a button on border/surround of window or JFrame

How do I put a button on the border that surrounds the frame like this cog: 我如何在围绕框的边框上放置一个按钮,如下所示:

在此处输入图片说明

_"Is the a short example anywhere? " _“这是任何地方的简短示例吗?”

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. 您会注意到我必须在充当顶部框架边框的JPanel上添加MouseMotionListener ,因为当您从框架中删除装饰时,您还将取消该功能。 So the MouseMotionListener makes the frame draggable again. 因此, MouseMotionListener使框架再次可拖动。

You would also have to implement resizing if you wished. 如果需要,您还必须实现调整大小。 I already implemented the System exit()` when you press the image. 当您按下图像时,我已经实现了System exit()`。 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);
            }
        });
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM