默认情况下,如何在JDesktopPane上选择InternalFrame? [英] How I can selected by default an InternalFrame on a JDesktopPane?

查看:54
本文介绍了默认情况下,如何在JDesktopPane上选择InternalFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有JDesktopPane的JFrame,并且在JDesktopPane内部,我与构造函数一起启动了JInternalFrame. (它的应用程序类似于身份验证用户,并带有文本框用户和文本框密码)

I have a JFrame with the JDesktopPane and inside the JDesktopPane, I launch with the constructor a JInternalFrame. (Its a app like the authentification users, with the textbox user and textbox pass)

我喜欢这样的内部:

MyInternalFrame internalF = new MyInternalFrame();
desktopPane.add(internalF);

我尝试:

internalF.setVisible(true);
internalF.setSelected(true);
desktopPane.getDesktopManager().activateFrame(internal);
desktopPane.setSelectedFrame(internal);

我该如何取消JInternalFrame及其默认选择的内容? 当我运行应用程序时,internalframe就像在背景中一样,未选中,未聚焦.

How I can lauch the JInternalFrame and Its selected by default? When I run the aplication, internalframe is like in background, Its not selected, Its not focused.

推荐答案

您可以在创建桌面并显示主框架后使内部框架可见.在这种情况下,默认情况下将选择框架.

You can make the internal frame visible AFTER the desktop is created and the main frame is visible. In that case the frame will be selected by default.

因此,您可以做一个例子:

So, one example of what you can do:

  1. 创建主框架
  2. 创建桌面
  3. 创建内部框架但不使其可见
  4. 在内部框架上将visible设置为true的启动线程,但是该线程可以等待直到显示桌面为止
  5. 使主机架可见
  6. 在线程中调用internalFrame.setVisible(true)并退出线程.

在这种情况下,内部框架将出现在桌面上,并根据需要选择它.

In such case the internal frame will appear on the desktop and it will be selected as you want it.

您可能会想到其他解决方案而不使用线程,而是将处理程序写入主机框架的事件.无论如何,要使内部框架在显示后可见,您必须在显示带有主框架的桌面后对其进行显示.

You might think of other solution without using threads, but writing handlers to the main frame's events. In any case, to make the internal frame visible after it shows, you have to show it AFTER desktop with main frame is displayed.

以下是示例,您可以使用:

Here is the example, that you can use:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;


public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    private Internal internalFrame;

    public Main() throws HeadlessException {
        super("Internal Frame Test");

        setBounds(10, 10, 600, 400);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setLayout(new BorderLayout());

        add(createDesktop(), BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
            public void windowOpened(WindowEvent e) {
                internalFrame.setVisible(true);
            }
        });

        setVisible(true);
    }

    private Component createDesktop() {
        JDesktopPane d = new JDesktopPane();

        internalFrame = new Internal("first");
        d.add(internalFrame);

        return d;
    }

    public static class Internal extends JInternalFrame {

        private static final long serialVersionUID = 1L;

        public Internal(String title) {
            super(title);
            setBounds(10, 10, 300, 100);
        }
    }

    public static void main(String[] a) {
        new Main();
    }
}

这篇关于默认情况下,如何在JDesktopPane上选择InternalFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆