Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

java - Change Panel size on button click

I have the following code:

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?

Thanks and regards. Edit:

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(), 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));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After resizing the JFrame, use: yourframe.validate();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...