【问题标题】:How to remove window box from any java gui如何从任何 java gui 中删除窗口框
【发布时间】:2023-03-07 19:52:01
【问题描述】:

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

提前致谢

这是我想要做的照片

【问题讨论】:

  • 我想知道更多,因为我的应用程序将具有许多功能 [例如从 Web 访问数据库、用户登录、从应用程序更新信息到服务器] 我对外观不是很紧张,任何简单gui对我来说没问题。但是在查看 javafx 之后,在我看来,我实际上可以同时拥有功能和酷炫的界面。如果想使用 javfx 而不是标准的 swing 对复杂性的影响有多大。

标签: java swing jframe desktop-application


【解决方案1】:

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

【讨论】:

  • 感谢 jwindow 的建议,我认为我需要添加一些控件。但是当我施加 jinternalframe 时,我看到 jinternal 框架在该 jwindow 内是可移动的,但 jwindow 是刚性的。我不能动它。我该怎么办。
  • 这里是一个示例代码:package jwindow;导入 javax.swing.*;公共类 Jwindow { public static void main(String[] args) { javax.swing.JInternalFrame jInternalFrame1 = new javax.swing.JInternalFrame();JWindow w = new JWindow(); w.setSize(300, 300); w.setLocation(500, 100); w.add(jInternalFrame1); w.setVisible(true); jInternalFrame1.setVisible(true); } }
  • 没有框架,您将负责调整大小和移动等事情
  • 你想做什么?你想完成什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
相关资源
最近更新 更多