java jinternalpanel,在容器中选择多个JInternalFrame(JPanel)

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.

解决方案

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.

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

L6mjE.png

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 list = new ArrayList();

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

}

}

}

}

});

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值