如何默认选择 JDesktopPane 上的 InternalFrame?

发布于 2024-09-30 07:27:56 字数 505 浏览 4 评论 0原文

我有一个带有 JDesktopPane 的 JFrame,在 JDesktopPane 内部,我使用构造函数启动 JInternalFrame。 (它是一个类似于身份验证用户的应用程序,具有文本框用户和文本框通行证)

我像这样启动内部:

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

我尝试:

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

如何启动 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)

I lauch the internal like this:

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

I try with:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-10-07 07:27:56

查看如何使用内部框架 java 教程。它给出了一个很好的例子并使用以下内容;

protected void createFrame() {
    MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true);
    desktop.add(frame);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {}
}

Have a look at the How To Use Internal Frames java tutorial. It gives a nice example and uses the following;

protected void createFrame() {
    MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true);
    desktop.add(frame);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {}
}
初心未许 2024-10-07 07:27:56

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

因此,您可以执行以下操作的一个示例:

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

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

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

这是您可以使用的示例:

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();
    }
}

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. Create main frame
  2. Create desktop
  3. Create internal frame but don't make it visible
  4. Start thread that sets visible to true on the internal frame, but the thread can wait until the desktop is shown
  5. Make the main frame visible
  6. In the thread call internalFrame.setVisible(true) and exit from the thread.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文