在容器中选择多个JInternalFrame(JPanel) [英] Selection of among multiple JInternalFrames in a container(JPanel)

查看:155
本文介绍了在容器中选择多个JInternalFrame(JPanel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jpanel 中有一些 JInternalFrame 。并且 JPanel 是gridlayout。我只需要在容器中设置一个 JInternalFrame JPanel )。我动态创建了 JInternalFrame 实例并添加到面板中。我仍然有 JInternalFrame 的列表,但是如何只设置一个选择。

I have some set of JInternalFrame in a Jpanel. and the JPanel is gridlayout. I need to set only one JInternalFrame to be selected in the container (JPanel). I created the JInternalFrame instance dynamically and add to the panel. I still have the list of JInternalFrame but how to make only one to set selected.

推荐答案

如何使用内部框架所示 ,通常,您将内部框架添加到桌面窗格。这允许你使用 activateFrame() 表示框架具有焦点。在此示例中, javax.swing.Action 用于通过 setSelected() 。可以在此 Q& A 中找到其他讨论。

As suggested in How to Use Internal Frames, "Usually, you add internal frames to a desktop pane." This allows you to use activateFrame() to indicate that a frame has focus. In this example, a javax.swing.Action is used to select frames from a menu via setSelected(). Additional discussion may be found in this Q&A.

附录:如果你想使用 JPanel ,或许可以获得一个不错的 GridLayout ,一个方法是使用 InternalFrameListener ,如下所示。

Addendum: If you want to use a JPanel, perhaps to get a nice GridLayout, one aproach is to use an InternalFrameListener as shown below.

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

/** @see https://stackoverflow.com/questions/8389640 */
public class InternalGrid extends JPanel {

    private final List<MyFrame> list = new ArrayList<MyFrame>();

    public InternalGrid() {
        super(new GridLayout(2, 2));
        for (int i = 0; i < 4; i++) {
            MyFrame f = new MyFrame("Frame", i);
            list.add(f);
            this.add(f);
        }
    }

    private void display() {
        JFrame f = new JFrame("InternalGrid");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setSize(320, 240);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new InternalGrid().display();
            }
        });
    }

    class MyFrame extends JInternalFrame {

        MyFrame(String name, int i) {
            super(name + String.valueOf(i), true, true, true, false);
            this.pack();
            this.setVisible(true);
            this.setLayout(new FlowLayout());
            this.add(new JLabel("Hi, I'm " + this.getTitle()));
            this.addInternalFrameListener(new InternalFrameAdapter() {

                @Override
                public void internalFrameActivated(InternalFrameEvent e) {
                    for (MyFrame f : list) {
                        if (f != MyFrame.this) {
                            try {
                                f.setSelected(false);
                            } catch (PropertyVetoException ex) {
                                System.out.println(ex);
                            }
                        }
                    }
                }
            });
        }
    }
}

这篇关于在容器中选择多个JInternalFrame(JPanel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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