将JInternalFrame置于其他JPanel之上

时间:2014-08-19 04:46:18

标签: java swing jpanel jinternalframe

我有2个JPanel,名为“panelMenu”和“panelTable”。它们都被添加到JDesktopPane中,命名为“desktop”。我在“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);

2 个答案:

答案 0 :(得分:1)

如何使用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);
  }
}

答案 1 :(得分:0)

以下代码可以帮助您

//yourDesktopPane
//panelMenu
//panelTable
//buttonMenu
//buttonTable

private void buttonMenuMouseClicked(java.awt.event.MouseEvent evt) {                                    

    panelMenu obj = new panelMenu ();
    BasicInternalFrameUI bi = (BasicInternalFrameUI) obj.getUI();
    bi.setNorthPane(null);
    obj.setBounds(0, 0, 1220, 700);//your desired values
    obj.setVisible(true);
    yourDesktopPane.add(obj);
    obj.toFront();
}

private void buttonTableMouseClicked(java.awt.event.MouseEvent evt) {                                    

    panelTableobj = new panelTable();
    BasicInternalFrameUI bi = (BasicInternalFrameUI) obj.getUI();
    bi.setNorthPane(null);
    obj.setBounds(0, 0, 1220, 700);//your desired values
    obj.setVisible(true);
    yourDesktopPane.add(obj);
    obj.toFront();
}