JFrame JPanel 刷新/更新/重画

问题描述 投票:0回答:2

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

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

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

我想做的是进行切换,以便我可以在“小视图”和“大视图”之间切换。

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

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

这是出现在框架内容底部的白色条的图片:

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

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.
    }


}
java swing jframe jpanel
2个回答
0
投票

SwingUtilities 有一个刷新组件的方法,它应该适用于任何 JComponent,

SwingUtilities.updateComponentTreeUI(Component componentToRefresh);

上面的代码片段显示了如何使用该方法,要将其插入到您的代码中,请执行以下操作:

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

-1
投票

既然Frame被添加到Panel中,尝试在Panel上调用validate()。

© www.soinside.com 2019 - 2024. All rights reserved.