java - 我试图了解如何使用 FocusListener 控制 JInternalFrames

标签 java focuslistener

我一直在研究有关 JInternalFrames 的各种问题,并尝试按照建议实现代码,但我似乎做错了一些事情。我在这里包含的代码将演示我的问题。三个框架已创建,但当您单击并移动它们时,它们不会按预期到达顶部,并且通常看起来像某种埃舍尔打印品。

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

如果能帮助解释我做错了什么,我们将不胜感激。

最佳答案

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

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

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

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

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

关于java - 我试图了解如何使用 FocusListener 控制 JInternalFrames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56033482/

相关文章:

java - 静态初始化 block 的设计问题

java - 将 spring bean Autowiring 到自定义 slf4j appender

java - 当用户单击 JTextField 时如何显示“打开文件”对话框?

Java 在按回车键时将焦点设置在 JButton 上

java - 如何在 Tomcat 中获取加载的应用程序类列表?

java - 使用谓词验证搜索参数

java - Swing JRadioButton 获得焦点后自动选择

java - 失去焦点时无法隐藏 SystemTray JPopupMenu

java - 概括几个文本字段,以便在获得焦点时选择整个文本