我有一个像这样的简单代码:
update-alternatives
当我按下按钮时,我可以看到闪烁(桌面背景)。在将另一个JFrame的可见性设置为false之前,我怎么知道JFrame已完全绘制在屏幕上?
当我按下按钮时,我可以看到闪烁(桌面背景)。在将另一个JFrame的可见性设置为false之前,如何知道JFrame已完全绘制在屏幕上?
我无法使用计时器解决方案!
非常感谢
答案 0 :(得分:2)
我已经重写了您的示例,没有第二帧和相应的闪烁。正如我在评论中已经提到的那样,您应该使用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);
}
}
答案 1 :(得分:0)
@Medvynskyy:非常感谢您的答复;但我想我不能说出我的真实意图。其实我想 根据以下代码最大化JFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
Rectangle maximizeRectangle=GraphicsEnvironment.getLocalGraphicsEnvironment().
getMaximumWindowBounds();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.getRootPane().setBorder(new LineBorder(Color.BLACK, 10));
JButton but1 = new JButton("Maximize");
ActionListener ac1 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setBounds(maximizeRectangle);
}
};
but1.addActionListener(ac1);
JButton but2 = new JButton("Minimize");
ActionListener ac2 = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
}
};
but2.addActionListener(ac2);
frame.add(but1);
frame.add(but2);
frame.setVisible(true);
}
}
但是当我按下最大化按钮时,我可以在左上角看到闪烁。我当然知道为什么会这样: 因为首先将大小为(500,500)的JFrame转换为位置(0,0),然后扩展为全屏显示。 当我按最大化时,如何只看到全屏JFrame? 我之前的问题是针对此问题的想法。