How to remove window box from any java gui(如何从任何 java gui 中删除窗口框)
问题描述
如何从任何 java 程序中删除窗口框.因为我想让它看起来没有边界.我知道在 jre 上运行的任何 jar 文件都会自动获得这样的窗口.所以我想知道是否有解决方法.
How do i remove the window box from any java program. Because i want to make it look border-less. I know any jar files running on jre automatically gets a window like this. So i want to know if there is workaround about this.
提前致谢
这是我想要做的照片
推荐答案
参见Frame#setUndecorated
您也可以使用默认未装饰的 JWindow
.
You could also use a JWindow
which is undecorated by default.
检查 this 和 this 例如使用
Check this and this for example uses
更新
如果您移除边框,您将负责移动和调整窗口大小...
If you remove the border, you become responsible for moving and resizing of the window...
这个基本"示例演示了如何使用鼠标移动 JWindow
.这会在窗口周围形成一个 10 像素宽的拖动区域".
This "basic" example demonstrates how to move a JWindow
with the mouse. This makes a "drag zone" around window which is 10 pixels wide.
调整大小将是类似的过程,但您需要决定调整大小的方向(即在调整大小时可能需要您移动窗口;))
Resizing would be similar process, but you need to decide in which direction to resize (ie it might need you to move the window when it's resized ;))
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 gui 中删除窗口框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从任何 java gui 中删除窗口框


基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01