简体   繁体   English

Java Swing JFrame最小化/最大化调整大小错误

[英]Java Swing JFrame minimize/maximize resize bug

The problem is that when I try to maximize the JFrame the frame does get maximized, but the content size is preserved. 问题是,当我尝试最大化JFrame时,框架确实被最大化,但内容大小被保留。

http://i.imgur.com/7q5Yh9F.png http://i.imgur.com/7q5Yh9F.png

My main frame class: 我的主要框架类:

private CardLayout layout;

private SettingsFrame settingsFrame;

private Container contentPane;
Frame frame = this;

private ApplicationFrame() {       
    contentPane = getContentPane();
    layout = new CardLayout();
    contentPane.setLayout(layout);
    initializeFrame();
    settingsFrame = new SettingsFrame(model);
    initializeMenuBar();
    initializePanels();                
    showPage(FIRST_PAGE);
    this.addWindowStateListener(listener);
}

public SettingsFrame getSettingsFrame() {
    return settingsFrame;
}

private void initializeFrame() {
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setTitle(APPLICATION_TITLE);
    this.settingsFrame = new SettingsFrame(model);
    this.setSize(1400, 900);
    this.setLocationRelativeTo(null);
}    

private void initializePanels() {
    add(new Panel(), FIRST_PAGE);
    add(new Panel(), SECOND_PAGE);
    add(new Panel(), THIRD_PAGE);
}      

I tried to make a listener, catch the maximize event and there i tried: 我试图创建一个监听器,捕获最大化事件,并尝试:

revalidate();
repaint();

also

invalidate();
validate();

but with no succes. 但没有成功。

I found out that: 我发现:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

is corectly maximizing the Frame, but i cannot use that is the listener because the maximize event already happened. 是核心最大化框架,但我不能使用它是监听器,因为最大化事件已经发生。

You're using a CardLayout which unfortunately won't allow you to resize the JComponent . 你正在使用CardLayout ,遗憾的是它不允许你调整JComponent大小。 Because the CardLayout can hold/manage one or more components that share the same display space. 因为CardLayout可以保存/管理共享相同显示空间的一个或多个组件。
You should add a JPanel and set that panel's layout to the CardLayout if you wish to use it. 如果要使用它,则应添加JPanel并将该面板的布局设置为CardLayout

I strongly recommend reading the following documentation: A Visual Guide to Layout Managers 我强烈建议您阅读以下文档: 布局管理器的可视指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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