操作完成后调整窗口大小

时间:2015-10-21 06:59:24

标签: java swing

以下是对我的问题的描述(在一张图片中!):enter image description here

我想知道:

1。)为什么会发生这种情况(我甚至试过setMaximumSize()setPreferredSize()setMinimumSize()!)?

2。)我该如何解决这个问题? (我使用GridLayout。它可能是也可能不是超级有效的)

这是设置代码:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
import java.awt.Dimension;

public class IntroScreen extends JFrame implements ActionListener
{   
    private boolean sandboxed = true;
    private final String SELECTION = "selection", CLEAN = "clean";

    private JPanel container;
    private DirectorySelectionPanel selectionPanel;
    private DirectoryCleanerPanel cleanerPanel;
    public IntroScreen()
    {
        super("Directory Cleaner");
        add((container = setupContent()));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        JOptionPane.showMessageDialog(this, this.getSize());
    }

    private JPanel setupContent()
    {

        JPanel content = new JPanel(new CardLayout());
        this.selectionPanel = new DirectorySelectionPanel(this.sandboxed);
        this.selectionPanel.getButton().addActionListener(this);
        content.add(this.selectionPanel, this.SELECTION);
        this.cleanerPanel = new DirectoryCleanerPanel(false);
        content.add(this.cleanerPanel, this.CLEAN);
        return content;
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
        // get the source of the event
        Object source = event.getSource();
        // if it was the selectionPanel's button
        if (source.equals(this.selectionPanel.getButton()))
        {
            if (this.selectionPanel.validateSelections())
            {
                System.out.println("Test line");
                ((CardLayout)this.container.getLayout()).show(this.container, this.CLEAN);
                // must figure out the correct size for this
                // TODO: Find a way to force the size of this to be around 450-by-90 pixels
                this.setSize(450,90);
            }
        }
        // otherwise
        else
        {
            // show the selectionPanel
            this.setSize(312,253);  // revert back to the original size
        }
    }

    public static void main(String[] args)
    {
        new IntroScreen();
    }
}

0 个答案:

没有答案