编辑:当我拖动边框时,正在发送某种刷新,我需要弄清楚并手动发送相同的刷新。
当使用 JFrame 和 JPanel 显示框架时,我试图使其框架的大小很容易可变。
我想做的是进行切换,以便我可以在“小视图”和“大视图”之间切换。
但是,当尝试执行以下操作时,面板内容上会出现一个白条:
此外,值得注意的是,当您拖动边框时,白条会消失并正确重新加载/刷新内容(即使您只拖动少量)
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.
}
}
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);
}
既然Frame被添加到Panel中,尝试在Panel上调用validate()。