我一直在研究关于JInternalFrames的各种问题,并尝试按照建议实现代码,但我似乎做错了什么。我在这里包含的代码将演示我的问题。这三个框架已创建,但当您单击并移动它们时,它们将不会像预期的那样出现在顶部,并且通常看起来像某种Escher打印。
public class IntFrmTest extends JFrame {
private JPanel contentPane;
private JDesktopPane desktop;
private int formHeight=675;
private int formWidth=950;
public IntFrmTest() {
this.setTitle("Desktop");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10, 10, formWidth, formHeight);
this.setMinimumSize(new Dimension(640,480));
this.setResizable(true);
desktop = new JDesktopPane();
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
this.add(BorderLayout.CENTER, desktop);
this.setVisible(true);
this.setLocationRelativeTo(null);
frmTesting frmTest1=new frmTesting(1);
frmTest1.setVisible(true);
contentPane.add(frmTest1);
frmTest1.setLocation(10,10);
frmTest1.addFocusListener(focusListener);
frmTesting frmTest2=new frmTesting(2);
frmTest2.setVisible(true);
contentPane.add(frmTest2);
frmTest2.setLocation(40,40);
frmTest2.addFocusListener(focusListener);
frmTesting frmTest3=new frmTesting(3);
frmTest3.setVisible(true);
contentPane.add(frmTest3);
frmTest3.setLocation(80,80);
frmTest3.addFocusListener(focusListener);
}
FocusListener focusListener=(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
JInternalFrame f = (JInternalFrame) arg0.getSource();
try {
f.setSelected(true);
f.moveToFront();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
System.out.println("Got Focus "+f.getName());
}
@Override
public void focusLost(FocusEvent arg0) {
JInternalFrame f = (JInternalFrame) arg0.getSource();
try {
f.moveToBack();
f.setSelected(false);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
System.out.println("Lost Focus "+f.getName());
}
});
public class frmTesting extends JInternalFrame {
private int formHeight=375;
private int formWidth=450;
public frmTesting(int frameNo) {
this.setTitle("Internal Frame "+frameNo);
this.setName("frmTest"+frameNo);
this.setClosable(true);
this.setResizable(false);
this.setMaximizable(false);
this.setIconifiable(false);
this.setFocusable(true);
this.setSize(formWidth,formHeight);
JPanel pnlDisplay = new JPanel();
this.setContentPane(pnlDisplay);
pnlDisplay.setLayout(null);
this.setVisible(true);
}
}
public static void main(String[] args) {
IntFrmTest ift=new IntFrmTest();
}
}我希望窗口在被选中时会简单地出现在前面,但大多数情况下不会发生这种情况。单击时,它们可能不会显示为选中状态,并且通常不会显示在顶部。
下面是一个示例,展示了它的外观:Visually Incorrect Example
如果能帮助你解释我做错了什么,我将不胜感激。
发布于 2019-05-08 13:29:57
像往常一样,我花了一整天的时间试图找出一个问题,并在我寻求帮助后找到答案……
也许有人可以填写更多细节,但我一直在创建具有桌面/内容窗格组合的单个表单,它一直在按预期工作。我没有从JInternalFrames文档中了解到的是,它看起来像是一个JInternalFrame驻留在桌面上,而主窗口上没有内容窗格。
当我将内部框架放到桌面/内容窗格上时,它不允许框架在被选中时出现在顶部。这让我相信我需要手动控制内部框架,因此使用了FocusListener。
从混搭中移除contentpane窗格可以修复问题,一切都可以按预期运行,这让我松了一口气,因为我不明白为什么需要手动完成。修正后的代码如下。
public class IntFrmTest extends JFrame {
private JPanel contentPane;
private JDesktopPane desktop;
private int formHeight=675;
private int formWidth=950;
public IntFrmTest() {
this.setTitle("Desktop");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10, 10, formWidth, formHeight);
this.setMinimumSize(new Dimension(640,480));
this.setResizable(true);
desktop = new JDesktopPane();
setContentPane(desktop);
this.setVisible(true);
this.setLocationRelativeTo(null);
frmTesting frmTest1=new frmTesting(1);
frmTest1.setVisible(true);
desktop.add(frmTest1);
frmTest1.setVisible(true);
frmTesting frmTest2=new frmTesting(2);
frmTest2.setVisible(true);
desktop.add(frmTest2);
frmTesting frmTest3=new frmTesting(3);
frmTest3.setVisible(true);
desktop.add(frmTest3);
}
public class frmTesting extends JInternalFrame {
private int formHeight=375;
private int formWidth=450;
public frmTesting(int frameNo) {
super("Internal Frame "+frameNo,false,true,false,false);
this.setName("frmTest"+frameNo);
this.setSize(formWidth,formHeight);
JPanel pnlDisplay = new JPanel();
this.setContentPane(pnlDisplay);
pnlDisplay.setLayout(null);
this.setLocation(30*frameNo,30*frameNo);
this.setVisible(true);
}
}
public static void main(String[] args) {
IntFrmTest ift=new IntFrmTest();
}
}当我开始研究rootpane的时候,我偶然发现了答案。
https://stackoverflow.com/questions/56033482
复制相似问题