gpt4 book ai didi

java - JFrame 总是在最前面

转载 作者:可可西里 更新时间:2023-11-01 11:27:50 24 4
gpt4 key购买 nike

我正在用 Java 编写一个程序,我将 JPanel 用于 界面,但我有一个问题总是最重要的,我不想要这个。我希望当我恢复另一个窗口时,它会在顶部。

我试过:

this.setAlwaysOnTop(false);

但它不起作用,框架始终在顶部继续。

这是一个解决我的问题的简单程序:

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class BorrarRegistro extends JPanel
{

public BorrarRegistro () {
super(new BorderLayout());

JLabel insertar= new JLabel("Registro");
JTextField borrar= new JTextField();
JButton borrar1= new JButton("Borrar Registro");

JPanel borrarRegistro= new JPanel(new GridLayout(4,1));
borrarRegistro.add(insertar);
borrarRegistro.add(borrar);
borrarRegistro.add(borrar1);

JPanel images= new JPanel(new GridLayout(1,3));


add(images,BorderLayout.NORTH);
add(borrarRegistro, BorderLayout.SOUTH);

}

private static void createAndShowGUI() {

//Create and set up the window.
JFrame frame = new JFrame("ClaseBase ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.
BorrarRegistro newContentPane = new BorrarRegistro ();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

// Display the window.
frame.pack();
frame.setVisible(true);

}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowGUI();
}

});
}
}

我该如何解决这个问题?

最佳答案

您应该在创建框架时调用 setAlwaysOnTop(false)

private static void createAndShowGUI() {

//Create and set up the window.
JFrame frame = new JFrame("ClaseBase ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.
Test newContentPane = new Test();
newContentPane.setOpaque(true); //con
frame.setContentPane(newContentPane);// tent panes must be opaque
frame.setAlwaysOnTop(false);

// Display the window.
frame.pack();
frame.setVisible(true);

}

关于java - JFrame 总是在最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com