gpt4 book ai didi

java - 如何强制 JFrame 在移动时留在后台?

转载 作者:行者123 更新时间:2023-12-01 11:21:04 25 4
gpt4 key购买 nike

我制作了一个滑动的 JFrame“应用程序”。代码简单而简短,因为在我将代码放入我的主项目之前,这只是一个测试。

它对我来说几乎完美工作。只有一个问题,当框架滑出时,它会在顶部,但我希望它留在背景中,在主窗口后面。 (上面会有按钮,所以出来后我必须使用这个框架)

这是主窗口的代码:

public class Window {

private JFrame frame;
static Slider s;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
s = new Slider();
s.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Window() {
initialize();
}

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 501, 414);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton(">>>>");
frame.getContentPane().add(button, BorderLayout.EAST);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
float x = frame.getX();
float y = frame.getY()+55;
for(int i = (int) x; i < x+500; i++){
s.setVisible(true);
try {
Thread.sleep(1);
s.setBounds(i, (int) y, 450, 250);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});


}

}

//And here is the code of the slider:

public class Slider extends JFrame {

private JPanel contentPane;
static Slider frame;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new Slider();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Slider() {
setAutoRequestFocus(false);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

}

}

(主应用程序不可调整大小,这就是高度和宽度恒定的原因。)

如有任何帮助,我们将不胜感激。

最佳答案

您还可以使用:

frame.setAlwaysOnTop(true);

如果你不希望frame一直在最上面,滑出后更改即可。

关于java - 如何强制 JFrame 在移动时留在后台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31224685/

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