在JDialog上调用pack()不会水平缩小它

时间:2016-09-25 11:32:32

标签: java swing jdialog

我有一个JDialog,在borderlayout中设置了其他组件。 ComponentListeners被添加到JTextAreas中,以便JDialog在文本区域增长时增大。

我增加或减少组件的字体大小,两个按钮增加字体大小并减小字体大小。

每当我更改字体大小时,我都会调用pack()来打包组件。在字体增加一切正常。所有组件都布置得很好。但是,当我将字体大小减小到可能的最低值时,宽度保持不变,只有高度发生变化。我希望高度和宽度都能改变。我怀疑我对布局做错了什么。

我只设置了Jdialog的minimumSize。

public class MyDialog implements ActionListener{

    private  int itsFontSize = 15;

    JPanel itsTitlePanel;

    JPanel itsMainPanel;

    JPanel itsInnerPanel;

    JPanel itsButtonPanel;

    JDialog myDialog;

    public static void main(String[] args) {

        JFrame dialogOwner = new JFrame();
        MyDialog myyDialogInstance = new MyDialog();
        myyDialogInstance.createDialog(dialogOwner);

        dialogOwner.setVisible(true);


        // TODO Auto-generated method stub

    }

    public JDialog createDialog(JFrame dialogOwner)
    {
        myDialog = new JDialog();

        myDialog.setAlwaysOnTop(true);
        myDialog.setModal(true);
        myDialog.setUndecorated(true);
        myDialog.setResizable(false);
        myDialog.setOpacity(0.9F);

        myDialog.setMinimumSize(new Dimension(300, 200));

        myDialog.setLayout(new BorderLayout());

        // title panel
        JLabel titleLabel = new JLabel("My Title");
        titleLabel.setFont(new Font("Arial", Font.BOLD, itsFontSize));

        itsTitlePanel = new JPanel();
        itsTitlePanel.setLayout(new BorderLayout());
        itsTitlePanel.setOpaque(true);
        itsTitlePanel.add(titleLabel, BorderLayout.CENTER);

        myDialog.add(itsTitlePanel, BorderLayout.NORTH);



        createMainPanel();
        myDialog.add(itsMainPanel, BorderLayout.CENTER);


        createButtonPanel();
        myDialog.add(itsButtonPanel, BorderLayout.SOUTH);


        myDialog.setVisible(true);
        dialogOwner.add(myDialog);



        return myDialog; 
    }

    public void createMainPanel()
    {
        createInnerPanel();

        itsMainPanel = new JPanel();
        itsMainPanel.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridheight =1;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;

        itsMainPanel.add(itsInnerPanel,c);

        for(int i =1 ; i<3;i++)
        {
            JTextArea textArea = new JTextArea();
            textArea.setOpaque(true);
            textArea.setBackground(Color.gray);
            textArea.addComponentListener(new ComponentListener() {

                @Override
                public void componentShown(ComponentEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void componentResized(ComponentEvent e) {
                    myDialog.revalidate();
                    myDialog.pack();
                }

                @Override
                public void componentMoved(ComponentEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void componentHidden(ComponentEvent e) {
                    // TODO Auto-generated method stub

                }
            });

            textArea.setWrapStyleWord(true);
            textArea.setFont(new Font("Arial", Font.PLAIN, itsFontSize));
            textArea.setLineWrap(true);
            textArea.setText("Blue mooonnnnn dayyyy apart frommm chalengeee boww bowww coww owww dogggyyy");

            c.gridy = i;
            c.insets = new Insets(5, 10, 5, 10);
            itsMainPanel.add(textArea, c);
        }

    }

    public void createInnerPanel()
    {

        itsInnerPanel = new JPanel();
        itsInnerPanel.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridheight =1;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(5, 10, 5, 10);

        for(int i = 0; i<3; i++)
        {
            JButton button = new JButton();
            button.setText("Inner " + (i+1));
            button.setOpaque(true);
            button.setFont(new Font("Arial", Font.PLAIN, itsFontSize));

            c.gridx = i;

            itsInnerPanel.add(button, c);
        }
    }

    public void createButtonPanel()
    {
        itsButtonPanel = new JPanel();
        itsButtonPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));

        for(int i = 0 ; i <3 ; i ++)
        {
            JButton button = new JButton();
            if(i==0)
            {
                button.setText("Increase font size");
            }
            else if(i==1)
            {
                button.setText("Decrease font size");
            }
            else
            {
                button.setText("Close Dialog");
            }
            button.setOpaque(true);
            button.setFont(new Font("Arial", Font.PLAIN, itsFontSize));
            button.addActionListener(this);
            button.setActionCommand("Button " + (i+1));
            itsButtonPanel.add(button);
        }

    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        // TODO Auto-generated method stub
        if(actionEvent.getActionCommand().equals("Button 1"))
        {
            if(itsFontSize <= 48)
            {
                itsFontSize +=2;
                setComponentsWithLatestFontSize();
            }
        }
        else if(actionEvent.getActionCommand().equals("Button 2"))
        {
            if(itsFontSize >= 17)
            {
                itsFontSize -= 2;
                setComponentsWithLatestFontSize();
            }
        }
        else
        {
            myDialog.dispose();;
        }
    }

    public void setComponentsWithLatestFontSize()
    {
        if(itsTitlePanel != null)
        {
            for(Component comp : itsTitlePanel.getComponents())
            {
                Font oldFont = comp.getFont();
                if(oldFont != null)
                {
                    comp.setFont(new Font(oldFont.getName(), oldFont.getStyle(), itsFontSize));
                }
            }
        }

        if(itsMainPanel != null)
        {
            for(Component comp : itsMainPanel.getComponents())
            {
                Font oldFont = comp.getFont();
                if(oldFont != null)
                {
                    comp.setFont(new Font(oldFont.getName(), oldFont.getStyle(), itsFontSize));
                }
            }
        }

        if(itsButtonPanel != null)
        {
            for(Component comp : itsButtonPanel.getComponents())
            {
                Font oldFont = comp.getFont();
                if(oldFont != null)
                {
                    comp.setFont(new Font(oldFont.getName(), oldFont.getStyle(), itsFontSize));
                }
            }
        }

        if(itsInnerPanel != null)
        {
            for(Component comp : itsInnerPanel.getComponents())
            {
                Font oldFont = comp.getFont();
                if(oldFont != null)
                {
                    comp.setFont(new Font(oldFont.getName(), oldFont.getStyle(), itsFontSize));
                }
            }
        }
        myDialog.revalidate();

    }

0 个答案:

没有答案