我正在尝试了解如何使用FocusListener控制JinternalFrames



我一直在研究有关jinternalframes的各种问题,并试图按建议实现代码,但是我似乎做错了什么。我在这里包括的代码将证明我的问题。创建了这三个帧,但是当您单击并移动它们时,它们不会按预期到达顶部,并且通常看起来像某种escher印刷。

public class IntFrmTest extends JFrame {
  private JPanel contentPane;
  private JDesktopPane desktop;
  private int formHeight=675;
  private int formWidth=950;
  public IntFrmTest() {
    this.setTitle("Desktop");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(10, 10, formWidth, formHeight);
    this.setMinimumSize(new Dimension(640,480));
    this.setResizable(true);
    desktop = new JDesktopPane();
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);
    this.add(BorderLayout.CENTER, desktop); 
    this.setVisible(true);
    this.setLocationRelativeTo(null); 
    frmTesting frmTest1=new frmTesting(1);
    frmTest1.setVisible(true);
    contentPane.add(frmTest1);
    frmTest1.setLocation(10,10);
    frmTest1.addFocusListener(focusListener);
    frmTesting frmTest2=new frmTesting(2);
    frmTest2.setVisible(true);
    contentPane.add(frmTest2);
    frmTest2.setLocation(40,40);
    frmTest2.addFocusListener(focusListener);
    frmTesting frmTest3=new frmTesting(3);
    frmTest3.setVisible(true);
    contentPane.add(frmTest3);
    frmTest3.setLocation(80,80);
    frmTest3.addFocusListener(focusListener);
  }
  FocusListener focusListener=(new FocusListener(){
    @Override
    public void focusGained(FocusEvent arg0) {
      JInternalFrame f = (JInternalFrame) arg0.getSource();
      try {
        f.setSelected(true);
        f.moveToFront();
      } catch (PropertyVetoException e) {
        e.printStackTrace();
      }
      System.out.println("Got Focus "+f.getName());
    }
    @Override
    public void focusLost(FocusEvent arg0) {
      JInternalFrame f = (JInternalFrame) arg0.getSource();
      try {
        f.moveToBack();
        f.setSelected(false);
      } catch (PropertyVetoException e) {
        e.printStackTrace();
      }
      System.out.println("Lost Focus "+f.getName());
    }
  });
  public class frmTesting extends JInternalFrame {
    private int formHeight=375;
    private int formWidth=450;
    public frmTesting(int frameNo) {
      this.setTitle("Internal Frame "+frameNo);
      this.setName("frmTest"+frameNo);
      this.setClosable(true);
      this.setResizable(false);
      this.setMaximizable(false);
      this.setIconifiable(false);
      this.setFocusable(true);
      this.setSize(formWidth,formHeight); 
      JPanel pnlDisplay = new JPanel(); 
      this.setContentPane(pnlDisplay);
      pnlDisplay.setLayout(null);
      this.setVisible(true); 
    }
  }
  public static void main(String[] args) {
    IntFrmTest ift=new IntFrmTest();
  }
}

我希望窗户在选择时简单地进入前面,但在大多数情况下都不会发生。单击时,它们可能不会显示为选定的显示,并且通常不会出现在顶部。

这是一个外观的示例:视觉上不正确的示例

任何帮助解释我做错了什么的帮助将不胜感激。

像往常一样,我整天都在试图找出问题并在我寻求帮助后找到答案...

也许有人可以填写更多详细信息,但是我一直在使用桌面/ContentPane组合创建单个表单,并且一直在按预期工作。我没有从jinternalframes的文档中求助的是,它看起来是jinternalframe,位于桌面上,主窗口上没有contentpane。

当我将内部框架放在桌面/ContentPane上时,选择框架时不允许框架进入顶部。这使我相信我需要手动控制内部框架,因此使用了FocusListener。

从混音中删除contentpane可以解决问题,一切都按预期工作,这是一种解脱,因为我不明白为什么需要手动进行。校正的代码如下。

public class IntFrmTest extends JFrame {
  private JPanel contentPane;
  private JDesktopPane desktop;
  private int formHeight=675;
  private int formWidth=950;
  public IntFrmTest() {
    this.setTitle("Desktop");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(10, 10, formWidth, formHeight);
    this.setMinimumSize(new Dimension(640,480));
    this.setResizable(true);
    desktop = new JDesktopPane();
    setContentPane(desktop);
    this.setVisible(true);
    this.setLocationRelativeTo(null); 
    frmTesting frmTest1=new frmTesting(1);
    frmTest1.setVisible(true);
    desktop.add(frmTest1);
    frmTest1.setVisible(true);

    frmTesting frmTest2=new frmTesting(2);
    frmTest2.setVisible(true);
    desktop.add(frmTest2);
    frmTesting frmTest3=new frmTesting(3);
    frmTest3.setVisible(true);
    desktop.add(frmTest3);
  }

  public class frmTesting extends JInternalFrame {
    private int formHeight=375;
    private int formWidth=450;
    public frmTesting(int frameNo) {
      super("Internal Frame "+frameNo,false,true,false,false);
      this.setName("frmTest"+frameNo);
      this.setSize(formWidth,formHeight); 
      JPanel pnlDisplay = new JPanel(); 
      this.setContentPane(pnlDisplay);
      pnlDisplay.setLayout(null);
      this.setLocation(30*frameNo,30*frameNo);
      this.setVisible(true); 
    }
  }
  public static void main(String[] args) {
    IntFrmTest ift=new IntFrmTest();
  }
}

我开始研究rootpane时偶然发现了答案。

相关内容

最新更新