【问题标题】:I'm trying to understand how to control JInternalFrames with a FocusListener我试图了解如何使用 FocusListener 控制 JInternalFrames
【发布时间】:2019-05-08 04:18:55
【问题描述】:

我一直在查看有关 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();
  }
}

我希望窗户在被选中时会简单地出现在前面,但大多数情况下不会发生这种情况。单击时,它们可能不会显示为已选中,并且通常不会出现在顶部。

以下是它的外观示例: Visually Incorrect Example

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

【问题讨论】:

    标签: java focuslistener


    【解决方案1】:

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

    也许有人可以填写更多详细信息,但我一直在使用桌面/内容窗格组合创建单个表单,并且它按预期工作。我对 JInternalFrames 的文档没有怀疑的是,它似乎是一个 JInternalFrame 驻留在桌面上,并且主窗口上没有内容窗格。

    当我将内部框架放在桌面/内容窗格上时,它不允许框架在被选中时到达顶部。这让我相信我需要手动控制内部框架,因此使用了 FocusListener。

    从组合中删除内容窗格可以解决问题,并且一切都按预期工作,这让我松了一口气,因为我不明白为什么需要手动完成。修正后的代码如下。

    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 时,我偶然发现了答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2016-04-11
      相关资源
      最近更新 更多