問題描述
我希望有一個內部(非窗口)對話框來詢問成員輸入.我希望將對話框集中放置在現有的 JPanel 上.
我查看了
編輯
分層窗格不合適的原因是由于缺少每層的布局管理器.面板可調整大小,面板 A 應保持在 100% 的區域,面板 B 應保持集中.
我認為 LayeredPane 是你最好的選擇.您將需要第三個面板來包含 A 和 B.這第三個面板將是 layeredPane,然后面板 A 和 B 仍然可以有一個不錯的 LayoutManagers.您所要做的就是使 B 居中于 A 之上,在 Swing 路徑中??有很多關于如何做到這一點的示例.不使用 LayoutManager 的定位教程.
公共類 Main {私有 JFrame 框架 = new JFrame();私人 JLayeredPane lpane = new JLayeredPane();私人JPanel panelBlue = new JPanel();私人JPanel panelGreen = new JPanel();公共主要(){frame.setPreferredSize(新維度(600, 400));frame.setLayout(new BorderLayout());frame.add(lpane, BorderLayout.CENTER);lpane.setBounds(0, 0, 600, 400);panelBlue.setBackground(Color.BLUE);panelBlue.setBounds(0, 0, 600, 400);panelBlue.setOpaque(true);panelGreen.setBackground(Color.GREEN);panelGreen.setBounds(200, 100, 100, 100);panelGreen.setOpaque(true);lpane.add(panelBlue, new Integer(0), 0);lpane.add(panelGreen, new Integer(1), 0);框架.pack();frame.setVisible(true);}/*** @param args 命令行參數*/公共靜態無效主要(字符串[]參數){新的主要();}}
您可以使用 setBounds 將面板定位在分層窗格內并設置它們的大小.
編輯以反映對原始帖子的更改您將需要添加組件偵聽器,以檢測何時調整父容器的大小,然后動態更改面板 A 和 B 的邊界.
I wish to have an internal (non window) dialog to ask for member input. I would like the dialog to be placed centrally on an existing JPanel.
I have looked at layeredpanes and these seem unusable due to only having a single layout manager (or no layout manager) across all the panes. I guess I could try to override JLayeredPane and provide a custom layout but this seems extreme.
Glass panes don't seem to be appropriate either.
How can this be done? Is there no usable concept of z-indexes in Swing?
EDIT
The reason Layered Panes weren't appropriate was due to the lack of a layout manager per layer. The panel is resizeable, Panel A should stay at 100% of area and Panel B should stay centralized.
I think LayeredPane is your best bet here. You would need a third panel though to contain A and B. This third panel would be the layeredPane and then panel A and B could still have a nice LayoutManagers. All you would have to do is center B over A and there is quite a lot of examples in the Swing trail on how to do this. Tutorial for positioning without a LayoutManager.
public class Main {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
private JPanel panelBlue = new JPanel();
private JPanel panelGreen = new JPanel();
public Main()
{
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
panelBlue.setBackground(Color.BLUE);
panelBlue.setBounds(0, 0, 600, 400);
panelBlue.setOpaque(true);
panelGreen.setBackground(Color.GREEN);
panelGreen.setBounds(200, 100, 100, 100);
panelGreen.setOpaque(true);
lpane.add(panelBlue, new Integer(0), 0);
lpane.add(panelGreen, new Integer(1), 0);
frame.pack();
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
}
You use setBounds to position the panels inside the layered pane and also to set their sizes.
Edit to reflect changes to original post You will need to add component listeners that detect when the parent container is being resized and then dynamically change the bounds of panel A and B.
這篇關于Java Swing - 如何在另一個面板上顯示一個面板?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!