我正在尝试使用主窗口输入创建一个GUI,并在主窗口下方创建一个单独的框以显示输出。但是,输出框的宽度将与主窗口不同,我不希望宽度差异成为框架的一部分(意味着差异应该是不可见的,用户可以单击通过空白区域到应用程序的后面。)
但我也希望输出框显示为主窗口的1个窗口。我可以使用监听器设置输出框的位置,使其粘贴到主窗口,但我不希望它在Windows(Microsoft)任务栏中显示为单独的窗口。我也不希望输出框可以选择 由于是一个单独的窗口。
因此,是否有任何方法可以在JFrame之外创建JPanel,或者我是否可以使用JFrame执行此操作并使其与我的约束一起使用?还有其他方法吗?
答案 0 :(得分:3)
在窗口中将单独的JPanel显示为非模态JDialog。如果您不想要右上角的菜单按钮,可以将其修改为未修饰。
我不确定你的意思:
(意味着差异应该是不可见的,用户可以点击应用程序背面的空白区域。)
关于
我也不希望输出框可以作为单独窗口的选择。
确保对话框中没有可聚焦组件,或者如果可聚焦,则将focusable属性设置为false。
修改强>
要防止对焦窗口被聚焦,请添加一行:
dialog.setFocusableWindowState(false);
例如:
import java.awt.Dimension;
import javax.swing.*;
public class DialogNoFocus {
public DialogNoFocus() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
DialogNoFocus mainPanel = new DialogNoFocus();
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(Box.createRigidArea(new Dimension(400, 300)));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "JDialog", false);
dialog.getContentPane().add(Box.createRigidArea(new Dimension(500, 100)));
int x = frame.getLocation().x;
int y = frame.getLocation().y + frame.getHeight();
dialog.setLocation(x, y);
dialog.pack();
dialog.setFocusableWindowState(false);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:0)
的JDialog
如何将2个内部框架(一个装饰,另一个装饰 - 未装饰且不可选)或1个内部框架和面板放在一个大的半透明未装饰框架中?
首先,您将在Windows(Microsoft)任务栏中只获得一个图标。内部框架不会显示在任务栏中。
主框架应该是半透明的:how to set JFrame background transparent but JPanel or JLabel Background opaque?