将JInternalFrame带到前面

时间:2011-02-22 17:23:42

标签: java swing

我有JDesktopPane,其中包含许多JInternalFrame个。我希望能够将任何JInternalFrame带到前面,覆盖任何其他活动帧。我找到了许多代码示例来执行此操作,但似乎都没有工作 - 框架不会在其他活动JInternalFrame之上。 E.g。

public static void moveToFront(final JInternalFrame fr) {
        if (fr != null) {
            processOnSwingEventThread(new Runnable() {
                public void run() {
                    fr.moveToFront();
                    fr.setVisible(true);
                    try {
                        fr.setSelected(true);
                        if (fr.isIcon()) {
                            fr.setIcon(false);
                        }
                        fr.setSelected(true);
                    } catch (PropertyVetoException ex) {
                        ex.printStackTrace();
                    }
                    fr.requestFocus();
                    fr.toFront();
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:5)

根据API toFront或moveToFront应该工作(虽然从我阅读API到toFront看起来是两个中更好的)。这些JInternalFrames是否位于JDesktopPane中?根据你的帖子,它们似乎是。我想知道错误是否在其他地方。

考虑创建并发布SSCCE(请点击链接),这是一个小型的可编辑,可运行的程序,可以展示您解决此问题的最佳尝试。然后我们可以检查您的代码,运行它,修改它并最好能够帮助您解决它。

以下是我的SSCCE示例:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Random;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class InternalFrameToFront extends JPanel {
   private static final int FRAME_MAX = 21;
   private static final int DT_WIDTH = 700;
   private static final int DT_HEIGHT = 500;
   private static final Dimension DESKTOP_SIZE = new Dimension(DT_WIDTH, DT_HEIGHT);
   private static final int IF_WIDTH = 150;
   private static final int IF_HEIGHT = 100;
   private static final Dimension INT_FRAME_SIZE = new Dimension(IF_WIDTH, IF_HEIGHT);
   private Random random = new Random();
   private JInternalFrame[] internalFrames = new JInternalFrame[FRAME_MAX];

   public InternalFrameToFront() {
      JDesktopPane desktop = new JDesktopPane();
      desktop.setPreferredSize(DESKTOP_SIZE);
      for (int i = 0; i < internalFrames.length; i++) {
         JInternalFrame intFrame = new JInternalFrame("Frame Number " + i);
         intFrame.setSize(INT_FRAME_SIZE);
         int x = random.nextInt(DT_WIDTH - IF_WIDTH);
         int y = random.nextInt(DT_HEIGHT - IF_HEIGHT);
         intFrame.setLocation(x, y);
         intFrame.setVisible(true);
         desktop.add(intFrame);

         internalFrames[i] = intFrame;
      }

      JSlider slider = new JSlider(0, FRAME_MAX - 1, 0);
      slider.setMajorTickSpacing(5);
      slider.setMinorTickSpacing(1);
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);
      JPanel sliderPanel = new JPanel();
      sliderPanel.add(slider);
      slider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent ce) {
            JSlider slider = (JSlider) ce.getSource();
            if (!slider.getValueIsAdjusting()) {
               int value = slider.getValue();
               internalFrames[value].toFront();
            }
         }
      });

      setLayout(new BorderLayout());
      add(desktop, BorderLayout.CENTER);
      add(sliderPanel, BorderLayout.SOUTH);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("InternalFrameToFront");
      frame.getContentPane().add(new InternalFrameToFront());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}