英文:
JAVA - Creating and showing a transparent window with a button
问题
我正在尝试创建一个带有一堆按钮的透明全屏覆盖层。
我已经使用了JFrame来创建覆盖层,但按钮没有显示。
```java
package com.Flickr.firstApp;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class MainClass {
public static void main(String... args) throws Exception {
Robot robot = new Robot();
Random random = new Random();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("Flow Layout");
frame.setUndecorated(true);
frame.setOpacity(0.2f);
frame.setSize(screenSize);
Panel panel = new Panel();
panel.setPreferredSize(new Dimension(100, 100));
panel.setBackground(Color.MAGENTA);
JButton b1 = new JButton("hello");
panel.add(b1);
frame.add(panel);
frame.setVisible(true);
}
}
作为一个新手,有一些问题可以帮助加深我的理解:
- 为什么按钮没有显示出来?
- 为什么Panel覆盖了整个框架,即使我已经将大小设置为200x200?它默认会尝试填充整个框架吗?
- 内容是否必须放在面板中,还是可以直接将按钮等放在框架上?
如果你能回答其中任何一个问题,或者指引我去找能够回答的资源,那就太棒了。谢谢!
<details>
<summary>英文:</summary>
I'm trying to create a transparent fullscreen overlay with a bunch of buttons.
I've used JFrame to create the overlay, however, but the button does not show.
package com.Flickr.firstApp;
import javax.swing.;
import java.awt.;
import java.util.Random;
public class MainClass {
public static void main(String... args) throws Exception {
Robot robot = new Robot();
Random random = new Random();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("Flow Layout");
frame.setUndecorated(true);
frame.setOpacity(0.2f);
frame.setSize(screenSize);
Panel panel = new Panel();
panel.setPreferredSize(new Dimension(100,100));
panel.setBackground(Color.MAGENTA);
JButton b1 = new JButton("hello");
panel.add(b1);
frame.add(panel);
frame.setVisible(true);
}
}
As a newbie, there's a few questions that would help deepen my understanding:
1. Why doesn't the button show?
2. Why does the Panel cover the whole frame even though I've set the size to 200x200? Does it attempt to fill the frame by default?
3. Does content have to go in a panel or can we put buttons, etc directly onto the frame?
If you could answer any one of these questions, or point me towards a resource that can, that would be awesome. Thanks!
</details>
# 答案1
**得分**: 1
1. 这个按钮对我来说是可见的。它的透明度使其难以看清。
2. 框架内容窗格的默认布局管理器是边界布局(BorderLayout)。当您不指定约束时,组件将被添加到 `CENTER`。位于 CENTER 的任何组件都会被调整大小以填充框架中的可用空间。
3. 您可以将任何组件添加到框架中。如果直接添加按钮,则按钮的大小将调整以填充框架。您需要理解布局管理器的工作原理。
不要使用 Panel,那是 AWT 组件。对于 Swing,请使用 `JPanel`。
指向资源:
阅读 [Swing 教程](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) 以了解许多 Swing 基础知识。其中有以下章节:
1. 布局管理器视觉指南
2. 如何创建半透明和自定义形状的窗口
还有其他基本演示程序,可以帮助您入门。
<details>
<summary>英文:</summary>
1. The button shows for me. Its opacity makes it hard to see.
2. The default layout manager for the content pane of the frame is a BorderLayout. When you don't specify a constraint it is added the the `CENTER`. Any component in the CENTER is resized to fill the space available in the frame.
3. You can add any component to the frame. If you add the button directly, then it will be resized to fill the frame. You need to understand how layout managers work.
Don't use a Panel, that is an AWT component. Use a `JPanel` for Swing.
> point me towards a resource
Read the [Swing Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for many Swing basics. There are sections on:
1. A Visual Guide to Layout Managers
2. How to Create Translucent and Shaped Windows
Along with other basic demo programs to get you started.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论