gpt4 book ai didi

java - 我如何知道未修饰的 JFrame 已完全绘制在屏幕上?

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

我有一个像这样的简单代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Rectangle maximizeRectangle=GraphicsEnvironment.getLocalGraphicsEnvironment().
getMaximumWindowBounds();
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();

//frame1
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setUndecorated(true);
frame1.setBounds(maximizeRectangle);
JPanel cp1 = new JPanel();
cp1.setBackground(new Color(200, 200, 200));
JButton but1 = new JButton("I'm frame 1");
cp1.add(but1);
ActionListener ac1 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame2.setVisible(true);
//here is my question:
//how can I know when frame2 has been fully painted? and then I can
//set visibility of frame1 to false to avoid flickering
frame1.setVisible(false);
}
};
but1.addActionListener(ac1);
frame1.setContentPane(cp1);

//frame2
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setUndecorated(true);
frame2.setBounds(maximizeRectangle);
JPanel cp2 = new JPanel();
cp2.setBackground(new Color(200, 200, 200));
JButton but2 = new JButton("I'm frame 2");
cp2.add(but2);
ActionListener ac2 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame1.setVisible(true);
//here is my question:
//how can I know when frame1 has been fully painted? and then I can
//set visibility of frame2 to false to avoid flickering
frame2.setVisible(false);
}
};
but2.addActionListener(ac2);
frame2.setContentPane(cp2);

frame1.setVisible(true);
}
}

当我按下按钮时,我可以看到闪烁(桌面背景)。在将另一个 JFrame 的可见性设置为 false 之前,如何知道 JFrame 已完全绘制在屏幕上?

当我按下按钮时,我可以看到闪烁(桌面背景)。在将另一个 JFrame 的可见性设置为 false 之前,如何知道 JFrame 已完全绘制在屏幕上?

我无法使用计时器解决方案!

非常感谢

最佳答案

我重写了您的示例,没有第二帧和相应的闪烁。正如我在评论中已经提到的,您应该使用 CardLayout 而不是第二帧。

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
private static final String FRAME1 = "frame1";

private static final String FRAME2 = "frame2";

public static void main(String[] args) {
SwingUtilities.invokeLater(Test::startUp);
}

private static void startUp() {
Rectangle maximizeRectangle = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
JFrame frame1 = new JFrame();
CardLayout cl = new CardLayout();
frame1.setLayout(cl);

// frame1
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setUndecorated(true);
frame1.setBounds(maximizeRectangle);
JPanel cp1 = new JPanel();
cp1.setBackground(new Color(200, 200, 200));
JButton but1 = new JButton("I'm frame 1");
cp1.add(but1);
ActionListener ac1 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(frame1.getContentPane(), FRAME2);
}
};
but1.addActionListener(ac1);
frame1.add(cp1, FRAME1);

// frame2
JPanel cp2 = new JPanel();
cp2.setBackground(new Color(200, 200, 200));
JButton but2 = new JButton("I'm frame 2");
cp2.add(but2);
ActionListener ac2 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(frame1.getContentPane(), FRAME1);
}
};
but2.addActionListener(ac2);
frame1.add(cp2, FRAME2);

frame1.setVisible(true);
}
}

关于java - 我如何知道未修饰的 JFrame 已完全绘制在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60166431/

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