单击按钮更改面板尺寸 [英] Change Panel size on button click

查看:57
本文介绍了单击按钮更改面板尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

package in.res.num.tapb.ui;

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class MainClass extends JPanel {
    public MainClass() {
        Registration registration = new Registration();
        ButtonPanel buttonPanel = new ButtonPanel();
        buttonPanel.setRegistration(registration);

        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
        registration.setBorder(BorderFactory.createTitledBorder("Registration Panel"));

        setLayout(new BorderLayout());
        add(registration, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Registration");
        frame.getContentPane().add(new MainClass());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }

    @SuppressWarnings("serial")
    private class ButtonPanel extends JPanel {
        private Registration registration;

        public ButtonPanel() {
            setLayout(new GridLayout(1, 0, 10, 0));     
            for (final String keyText : Registration.KEY_TEXTS) {
                JButton btn = new JButton(keyText);
                btn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if (registration != null) {
                            registration.swapView(keyText);
                        }
                    }
                });
                add(btn);
            }
        }

        public void setRegistration(Registration registration) {
            this.registration = registration;
        }
    }

    private static class Registration extends JPanel {
        private static final Dimension PREF_SIZE = new Dimension(450, 300);
        public static final String USER_AGREEMENT = "User Agreement";
        public static final String USER_INFO = "User Information";
        public static final String ENROLLMENT = "Enrollment";
        public static final String[] KEY_TEXTS = { USER_AGREEMENT, USER_INFO, ENROLLMENT };
        private CardLayout cardlayout = new CardLayout();
        private JPanel cards = new JPanel(cardlayout);

        public Registration() {
            cards.add(createUserAgreePanel(), USER_AGREEMENT);
            cards.add(createUserInfoPanel(), USER_INFO);
            cards.add(createEnrollmentPanel(), ENROLLMENT);
            setLayout(new BorderLayout());
            add(cards, BorderLayout.CENTER);
        }



        private JPanel createEnrollmentPanel() {
            JPanel enrol = new JPanel();
            enrol.setSize(new Dimension(400, 200));
            enrol.add(new JLabel("Enrollment"));
            return enrol;
        }

        private JPanel createUserAgreePanel() {
            JPanel userAgree = new JPanel();
            userAgree.setSize(new Dimension(200, 300));
            userAgree.add(new JLabel("User Agreement"));
            return userAgree;
        }

        private JPanel createUserInfoPanel() {
            JPanel userInfo = new JPanel();
            userInfo.setSize(new Dimension(300, 400));
            userInfo.add(new JLabel("User Information"));
            return userInfo;
        }

        public void swapView(String key) {
            cardlayout.show(cards, key);
        }

    }

}

如您所见,我想在单击按钮时更改大小.是否可以?上面的代码不起作用,我的意思是大小没有改变.如何即时更改尺寸?

As you can see I want to change the size on button click. Is it possible? The above code is not working, I mean the size is not changing. How can I change the size on fly?

感谢和问候.

交换有关选择JList行的面板.

swap the panel on selecting a row of JList.

    getChoicesList().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            getViewPanel().changeView(getChoicesList().getSelectedIndex());
            getChoicePanel().changeView(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(getChoicesList().getSelectedIndex()).getEnumName()).getDimensionForScrollPaneOfChoicePanel());
            ((MainFrame) getTopLevelAncestor()).pack();
        }
    });

ViewPanel#changeView(),这将交换面板:

ViewPanel#changeView(), this swaps the panel:

public void changeView(int index) {
    removeAll();
    getPanels().get(index).setPreferredSize(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(index).getEnumName()).getDimensionForViewPanel());
    add(getPanels().get(index));
}

推荐答案

使用布局管理器时,切勿使用setSize().布局管理器的工作是确定大小.您可以通过设置指定的大小或最小或最大大小来向布局管理器提供提示.但是,不建议您执行此操作,因为组件和面板应以其首选尺寸显示,该尺寸由您使用的布局管理器确定.如果您确实覆盖了大小,则代码应为:

You should never use setSize() when using a layout manager. It is the job of the layout manager to determine the size. You can provide hints to the layout manager by setting the peferred or minimum or maximum sizes. However it is not recommend that you do this since components and panels should be displayed at their preferred size which will be determined by the layout manager you are using. If you did override the size then the code should be:

// enrol.setSize(new Dimension(400, 200));
enrol.setPreferredSize(new Dimension(400, 200));

但是,这仍然无法按照您想要的方式工作,因为CardLayout的工作是确定使用CardLayout添加到面板中的所有面板的最大尺寸.因此,当您从一个面板交换到另一个面板时,您不会得到每个单独面板的大小.对于用户而言,这是更好的体验,因为用户不想每次单击按钮时都看到帧大小不断变化.

However, this still won't work the way you want because the job of the CardLayout is to determine the largest size of all panels added to the panel using a CardLayout. So when you swap from panel to panel you don't get the size of each individual panel. This is s better experience for the user because the user doesn't want to see the frame size keep changing every time they hit a button.

如果您确实希望每次单击按钮时都具有帧更改大小,则基本代码为:

If you did want to have the frame change size every time you click on a button then the basic code would be:

mainPanel.remove(oldPanel);
mainPanel.add(newPanel);
frame.pack();

然后,主面板的布局管理器将观察添加的newlay面板的首选大小.

Then the layout manager of the main panel will observe the preferred size of the newlay added panel.

这篇关于单击按钮更改面板尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆