【发布时间】:2019-05-08 04:18:55
【问题描述】:
我一直在查看有关 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
任何帮助解释我做错了什么将不胜感激。
【问题讨论】:
标签: java focuslistener