java - JFrame JPanel 刷新/更新/重绘

标签 java swing jframe jpanel

编辑:当我拖动边框时正在发送某种刷新,我需要弄清楚并手动发送相同的刷新。

  • 请注意,我已尝试使用 revalidate() 和 repaint()。

当使用 JFrame 和 JPanel 显示框架时,我试图使框架的大小易于可变。

我想做的是进行切换,这样我就可以在“小 View ”和“大 View ”之间切换。

但是,在尝试执行以下操作时,面板的内容上会出现一个白条:

  • 开放式框架...(默认为大尺寸)
  • 设置为小尺寸
  • 再次设置为大尺寸
  • 再次设置为小尺寸(这是出现白条的时候) (引用main方法中的注释)

这是出现在框架内容底部的白色条的图片:white bar bottom of game

此外,值得注意的是,当您拖动边框时(即使您拖动的只是少量),白色条会消失并正确地重新加载/刷新内容

public class FrameTest {
    private static FrameTest instance;

    JFrame clientFrame

    JPanel client_panel;

    private void openFrames() {
            JclientFrame = new JFrame("727 Deob");
            clientFrame.setLayout(new BorderLayout());
            client_panel = new JPanel();
            client_panel.setLayout(new BorderLayout());
            client_panel.add(new Applet());
            client_panel.setPreferredSize(new Dimension(765, 555));
            clientFrame.getContentPane().add(client_panel, BorderLayout.PAGE_END);
            clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            clientFrame.pack();
            clientFrame.setVisible(true);
    }

    public static void main(String[] args) {
       instance = new FrameTest();
       instance.openFrames();

       instance.setDimensions(true); //works absolutely fine!
       instance.setDimensions(false); //continues to work absolutely fine!
       instance.setDimensions(true); //now the white bar at the bottom appears


    }


    public void setDimensions(boolean smallType) {
        int width = 765;
        int height = smallType ? 530 : 577;
        clientFrame.setSize(width, height);

        //tried using revalidate() and repaint() here on the frame, the frame contents AND the panel... NO LUCK, white bars still aren't fixed.
    }


}

最佳答案

由于Frame被添加到Panel,尝试调用panel的validate()

关于java - JFrame JPanel 刷新/更新/重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52405303/

相关文章:

java - 在 Jersey 生命周期中如何拦截请求?

java - Swing:DefaultBoundedRangeModel 是否合并多个事件?

java - 将 JFrame 添加到 JApplet

java - 向 JFrame 添加标签

java - 如何 git-checkout 在 OpenShift 上创建的第一个应用程序?

java - 使用 smartgwt 防止用户留下未保存的表单

java - impl.ServerClient:无法在服务器列表中找到可用的服务器:

java - 为什么我的 GUI 没有加载到构造函数上?

java - 使用垂直和水平粘合的 BoxLayout

java - 将多个 JPanel 添加到 JFrame