Java 设置未装饰(真实)与toFront()的混乱
我有两个jframe,当setUndercorated设置为false时,它们工作得很好,但当它设置为true时,它们的工作并不一致Java 设置未装饰(真实)与toFront()的混乱,java,swing,jframe,Java,Swing,Jframe,我有两个jframe,当setUndercorated设置为false时,它们工作得很好,但当它设置为true时,它们的工作并不一致 frame = new JFrame("Name of the frame"); // main frame frame.setAlwaysOnTop(false); frame.setSize(width, height); frame.add(canvas); frame.setUndecorated(true); frame.setVisible(true)
frame = new JFrame("Name of the frame"); // main frame
frame.setAlwaysOnTop(false);
frame.setSize(width, height);
frame.add(canvas);
frame.setUndecorated(true);
frame.setVisible(true);
menu = new MenuUI(); // this is also a undecorated JFrame
菜单由鼠标事件调用,但它有时显示在主框架的前面,有时显示在主框架的后面
public static void checkMove(int action, MouseEvent e) {
int x = e.getX();
if(x == 0){
menu.setVisible(true);
menu.toFront();
}else{
menu.setVisible(false);
}
我试过做frame.toBack()代码>但它也会在其他东西后面发送帧。。。
如何将未装饰的JFrame放在另一个未装饰的JFrame之前,并使其保持一致
编辑:鼠标事件时菜单框消失
contentPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
setVisible(false);
}
});
EDIT2:在mKorbel的回答之后,我尝试创建一个JDialog,但是我得到了与JFrame相同的结果。如果扩展JDialog,如何设置父级
package menu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import openGL.main.openGLMain;
@SuppressWarnings("serial")
public class menuTest extends JDialog {
public menuTest() {
setUndecorated(true);
setBounds(0, 0, 250, frame.getHeight());
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 450, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 700, Short.MAX_VALUE)
);
getContentPane().setLayout(groupLayout);
getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
setVisible(false);
}
});
}
}
- 默认情况下,永远不要对toFront或toflont做出正确的反应,这不是primary r的主要功能(没有图标化和去图标化,但随后在屏幕上闪烁)
- 你必须用它来做这项工作,而且
默认情况下,JFrame不会始终正确响应Top或toFront,这不是主要顶级容器的主要功能(没有图标化和去图标化,但随后在屏幕上闪烁)
您必须为此作业使用JDialog,设置为父级和模式(如果需要)
确切地说,你也可以使用JinternalFrame,使你的主Jframe中有一些子框架,你可以图标化、去实体化或关闭不需要的框架,如果你需要,还可以调整它们的大小。
如果你想让他们在前面或后面的其他人,你可以很容易地通过拖放框架儿童 我已经根据你的答案编辑了这个问题,但是我在设置父框架时仍然遇到一些问题好的,所以我想出来了。。。谢谢你为我指明了正确的方向