简体   繁体   中英

JFrame JPanel refreshing/updating/repainting

Edit: some sort of refresh is being sent when I drag the borders, I need to figure that out and manually send that same refresh.

  • Please note I have tried using revalidate() and repaint().

When using JFrame and JPanel to display a frame, I'm trying to make it so the size of the frame is easily mutable.

What I'm trying to do is make a switch so I can toggle between a "small view" and a "big view".

However, there is a white bar appearing on the panel's content when trying to:

  • open frame... (by default is big size)
  • set to small size
  • set to big size again
  • set to small size again (this is when the white bar appears) (refer to the comments in the main method)

Here is a picture of the white bar that appears at the bottom of the contents on the frame: 白色酒吧游戏的底部

Also, a big note is that the white bar disappears and reloads/refreshes the content properly when you drag the border (even if you drag it just a small amount)

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,请尝试在面板上调用validate()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM