【发布时间】:2019-07-10 03:55:00
【问题描述】:
我有一个 JFrame,它创建了一个 JInternalFrame,而 JInternalFrame 又在其内部创建了 JInternalFrames。 “外部”JIF 有一个“添加框架”按钮和一个复选框菜单,因此每个“内部”JIF 类型只能创建一次。最多可以有 6 个“内部”JIF(代码示例限制为 2 个,FRAME A 和 B)。
创建内部 JIF 工作正常,但是当用户取消选中复选框时,我如何找到要关闭的正确内部 JIF? 如果用户关闭内部 JIF,我如何将其链接回取消选中正确的复选框?
我尝试过的方法最终会关闭所有内部 JIF,或者如果我尝试搜索打开的 JIF 列表并将其标题与复选框字段匹配,编译器会说该信息目前不可用。
外部和内部 JIF 创建的简化代码如图所示。不要告诉我我需要一个布局管理器 - JIF 必须是用户可移动的并且可以不受限制地调整大小。
class OUTJIF extends JInternalFrame {
OUTJIF() {
JInternalFrame outerJIF = new JInternalFrame("Outer JInternalFrame", true, true, true, true);
outerJIF.setBounds(50, 50, 600, 400);
outerJIF.getContentPane().setLayout(null);
JButton btnAddFrames = new JButton("Add Frames");
btnAddFrames.setBounds(10, 11, 125, 23);
outerJIF.getContentPane().add(btnAddFrames);
JPopupMenu popMenu = new JPopupMenu();
JCheckBoxMenuItem boxFrameA = new JCheckBoxMenuItem("Frame A");
JCheckBoxMenuItem boxFrameB = new JCheckBoxMenuItem("Frame B");
popMenu.add(boxFrameA);
popMenu.add(boxFrameB);
btnAddFrames.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
Demo.mainPane.add(outerJIF); // add to invoking JFrame
outerJIF.setVisible(true);
// Class for internal JIF
class intJIF extends JInternalFrame {
intJIF(String intType, int x, int y, int h, int w) {
JInternalFrame innerJIF = new JInternalFrame(intType, true, true, true, true) ;
innerJIF.setBounds(new Rectangle(x, y, h, w));
outerJIF.getContentPane().add(innerJIF);
innerJIF.setVisible(true);
// ISSUE #2 - IF USER CLOSES ONE OF THESE, HOW TO CHANGE CHECKBOX MENU?
}
};
// LISTENERS FOR outerJIF MENU ITEMS
ActionListener listFrameA = new ActionListener() {
public void actionPerformed(ActionEvent event) {
AbstractButton boxFrameA = (AbstractButton) event.getSource();
boolean selected = boxFrameA.getModel().isSelected();
if (selected) { new intJIF("Inner Frame A", 0, 100, 250, 250); }
else { // ISSUE #1 - HOW TO FIND THE RIGHT INTERNAL JIF TO CLOSE?
}
} };
boxFrameA.addActionListener(listFrameA);
ActionListener listFrameB = new ActionListener() {
public void actionPerformed(ActionEvent event) {
AbstractButton boxFrameB = (AbstractButton) event.getSource();
boolean selected = boxFrameB.getModel().isSelected();
if (selected) { new intJIF("Inner Frame B", 50, 50, 250, 250); }
else { // ISSUE #1 - HOW TO FIND THE RIGHT INTERNAL JIF TO CLOSE?
}
} };
boxFrameB.addActionListener(listFrameB);
}
}
【问题讨论】:
标签: java swing jcheckbox jinternalframe