gpt4 book ai didi

java - 将 JInternalFrame 置于其他 JPanel 之上

转载 作者:行者123 更新时间:2023-12-01 12:38:13 25 4
gpt4 key购买 nike

我有 2 个 JPanel,分别命名为“panelMenu”和“panelTable”。两者都添加到JDesktopPane中,命名为“桌面”。我在“panelMenu”中放置了一个按钮,单击它时,它将弹出一个 JInternalFrame。

两个面板都在“桌面”中并排设置...问题来了...当我单击按钮时...JInternalFrame 将显示,但它最初位于面板的背面“panelTable”...如何使 JInternalFrame 始终位于任何其他组件之上?

//将面板添加到桌面

panelMenu.setBackground(Color.yellow);
panelMenu.setBounds(0,0,200,800);
panelMenu.setLayout(null);

panelTable.setBackground(Color.gray);
panelTable.setBounds(250,50,700,700);
panelTable.setLayout(null);

desktop.setLayout(null);
desktop.setSize(width, height);
desktop.setBackground(Color.gray);

desktop.add(panelMenu);
desktop.add(panelTable);
this.add(desktop);

最佳答案

使用 JOptionPane.showInternalXXXDialog(...) 怎么样? :

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InternalMessageDialogTest {
private final JDesktopPane desktop = new JDesktopPane();
public JComponent makeUI() {
JButton button = new JButton(new AbstractAction("open") {
@Override public void actionPerformed(ActionEvent e) {
JOptionPane.showInternalMessageDialog(
desktop, "information", "modal",
JOptionPane.INFORMATION_MESSAGE);
}
});
JPanel panelMenu = new JPanel();
panelMenu.setBackground(Color.YELLOW);
panelMenu.add(button);
panelMenu.setBounds(0, 0, 100, 100);

JInternalFrame panelTable = new JInternalFrame("Table");
panelTable.add(new JScrollPane(new JTable(30, 3)));
panelTable.setBounds(100, 0, 200, 100);

desktop.add(panelMenu);
desktop.add(panelTable);
panelMenu.setVisible(true);
panelTable.setVisible(true);

JPanel p = new JPanel(new BorderLayout());
p.add(desktop);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new InternalMessageDialogTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

关于java - 将 JInternalFrame 置于其他 JPanel 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25375813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com