ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何从任何java gui中删除窗口框

2019-09-27 08:59:59  阅读:300  来源: 互联网

标签:java swing jframe desktop-application


如何从任何Java程序中删除窗口框.因为我想让它看起来没有边框.我知道在jre上运行的任何jar文件会自动获得这样的窗口.所以我想知道是否有解决方法.

提前致谢

这是一张我想要做的照片

解决方法:

Frame#setUndecorated

您也可以使用默认情况下未修饰的JWindow.

检查thisthis以获取示例用途

更新

如果删除边框,则负责移动和调整窗口大小…

这个“基本”示例演示了如何使用鼠标移动JWindow.这使得窗口周围的“拖动区域”宽10像素.

调整大小将是类似的过程,但您需要决定调整大小的方向(即,它可能需要您在调整大小时移动窗口;))

import java.awt.Component;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestMoveWindow {

    public static void main(String[] args) {
        new TestMoveWindow();
    }

    public TestMoveWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JWindow window = new JWindow();
                window.setSize(200, 200);
                window.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }

                });

                MouseAdapter mouseHandler = new MouseAdapter() {

                    private Point offset;

                    protected boolean isWithinBorder(MouseEvent e) {
                        Point p = e.getPoint();
                        Component comp = e.getComponent();
                        return p.x < 10 || p.y < 10 || p.x > comp.getWidth() - 10 || p.y > comp.getHeight()  - 10;
                    }

                    @Override
                    public void mouseMoved(MouseEvent e) {
                        Component comp = e.getComponent();
                        if (isWithinBorder(e)) {
                            System.out.println("Move");
                            comp.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                        } else {
                            System.out.println("Default");
                            comp.setCursor(Cursor.getDefaultCursor());
                        }
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (offset != null) {
                            Point pos = e.getLocationOnScreen();

                            int x = pos.x - offset.x;
                            int y = pos.y - offset.y;

                            System.out.println(x + "x" + y);

                            SwingUtilities.getWindowAncestor(e.getComponent()).setLocation(x, y);
                        }
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        if (isWithinBorder(e)) {
                            Point pos = e.getComponent().getLocationOnScreen();
                            offset = new Point(e.getLocationOnScreen());
                            offset.x -= pos.x;
                            offset.y -= pos.y;
                        }
                    }

                };

                window.getContentPane().addMouseListener(mouseHandler);
                window.getContentPane().addMouseMotionListener(mouseHandler);

                window.setLocationRelativeTo(null);
                window.setVisible(true);
            }
        });
    }
}

标签:java,swing,jframe,desktop-application
来源: https://codeday.me/bug/20190927/1823379.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有