我目前的问题是我有一个 JFrame(上面有一些按钮)...当我单击“New”按钮时,它将调用屏幕上的 InternalFrame...内部框架屏幕出现并且一切都很好,直到我移动内部框架并发现它设置在所有东西的后面......
我已经尝试过 .toFront() 、 .setAlwaysOnTop() 和一切...直到我遇到 JLayeredPane 我认为这是我正在寻找的东西...但我无法让它工作 ><其他人可以指导我完成它吗?谢谢!
请告知您需要的额外信息。我们将尽快提供
WindowConstruct wconstruct;
JDesktopPane desktop = new JDesktopPane();
JInternalFrame InternalWindows = new JInternalFrame();
public MainUser(){
wconstruct = new WindowConstruct("..:: User's Helpdesk Main Page ::..", 1500, 800, false, null, "user");
wconstruct.add(desktop);
wconstruct.btnNew.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src == wconstruct.btnNew){
InternalWindows.setSize(500, 300);
InternalWindows.setTitle("New task");
InternalWindows.setLayout(null);
InternalWindows.setLocation(100,50);
InternalWindows.setClosable(true);
desktop.add(InternalWindows);
InternalWindows.setVisible(true);
}
}
最佳答案
我似乎没有遇到任何异常,尝试使用您显示的代码创建 MCVE。这不是最好的代码,但我尝试尽可能保持代码的外观。查看它并让我知道我的代码有何不同。再次强调,为了更好地帮助我们找到问题的根源,您应该始终发布 MCVE。这意味着代码应该是可运行的,即复制、粘贴、编译、运行。
还请考虑我上面的所有评论(即,如果您不打算将此应用程序设为 MDI(请参阅评论中的链接)类型应用程序,则可能使用 JDialog。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
public class MainUser implements ActionListener {
WindowConstruct wconstruct;
JDesktopPane desktop = new JDesktopPane();
JInternalFrame InternalWindows = new JInternalFrame();
public MainUser() {
wconstruct = new WindowConstruct("..:: User's Helpdesk Main Page ::..",
500, 500);
wconstruct.add(desktop);
wconstruct.btnNew.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == wconstruct.btnNew) {
InternalWindows.setSize(500, 300);
InternalWindows.setTitle("New task");
InternalWindows.setLayout(null);
InternalWindows.setClosable(true);
InternalWindows.setLocation(100, 50);
desktop.add(InternalWindows);
InternalWindows.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainUser();
}
});
}
class WindowConstruct extends JFrame {
JButton btnNew = new JButton("Add New");
public WindowConstruct(String title, int width, int height) {
super(title);
setSize(width, height);
add(btnNew, BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
}
关于java - 用于 Java 的 JLayeredPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362409/