CardLayout有不同的大小

时间:2014-05-27 05:30:57

标签: java swing layout layout-manager cardlayout

我通过在每张卡片上制作不同尺寸的CardLayout来解决问题。

在阅读The Use of Multiple JFrames: Good or Bad Practice?之后,我注意到我的做事方式并不合适,所以我决定开始学习如何使用CardLayout

我从@HovercraftFullOfEels

尝试了this answer 来自@mKorbel的

This one

最后是@Kleopatra的answer

所有这些都是基于气垫船的答案,但有一些变化。我能够创建CardLayout(并且仍然对它感到困惑),并且我(在某种程度上)可以实现Kleopatra的方法,它调整帧的大小但不应该如此,当复制和粘贴代码时,你将请注意,它会减小帧的大小,只需几毫米(或像素)。

我不确定这是关于我使用的布局管理器,还是因为我没有以正确的方式使用Kleopatra的方法,因为我也没有#39;知道@Override方法是如何工作的,何时使用它,何时不工作。

这是一个MCVE(我能做的最短)。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class PageViewer extends CardLayout{ 
    private static final String loginCard = "login";
    private static final String userCard = "createUser";

    JFrame frame;
    JPanel contentPane;
    CardLayout cardLayout;

    @Override
    public Dimension preferredLayoutSize(Container parent) {
        Component current = findCurrentComponent(parent);
        if (current != null) {
            Insets insets = parent.getInsets();
            Dimension pref = current.getPreferredSize();
            pref.width += insets.left + insets.right;
            pref.height += insets.top + insets.bottom;
            return pref;
        }
        return super.preferredLayoutSize(parent);
    }

    public Component findCurrentComponent(Container parent) {
        for (Component comp : parent.getComponents()) {
            if (comp.isVisible()) {
                return comp;
            }
        }
        return null;
    }

    public void createAndShowUI() {
        frame = new JFrame("Welcome");
        cardLayout = new CardLayout();
        contentPane = new JPanel(cardLayout);

        LoginPage lp = new LoginPage();
        CreateUser cu = new CreateUser();

        lp.register.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                createNewUser();
                frame.pack();
            }
        });

        contentPane.add(lp, loginCard);
        contentPane.add(cu, userCard);

        frame.add(contentPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }

    PageViewer() {
        createAndShowUI();
    }

    public void createNewUser() {
        cardLayout.show(contentPane, userCard);
    }

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

    public class LoginPage extends JPanel {
        Image image;
        ImageIcon imageIcon;

        JPanel userPanel;
        JPanel passwordPanel;
        JPanel buttonsPanel;
        JPanel fieldsPanel;

        JFrame frame;

        JLabel usernameLabel;
        JLabel passwordLabel;
        JLabel logo;

        JTextField usernameField;
        JPasswordField passwordField;

        JButton login;
        JButton register;

        public LoginPage() {
            image = Toolkit.getDefaultToolkit().getImage("image.jpg");
            imageIcon = new ImageIcon("image.jpg");

            userPanel = new JPanel();
            passwordPanel = new JPanel();
            buttonsPanel = new JPanel();
            fieldsPanel = new JPanel();

            usernameLabel = new JLabel("username:");
            passwordLabel = new JLabel("password:");
            logo = new JLabel(imageIcon);

            usernameField = new JTextField();
            passwordField = new JPasswordField();

            login = new JButton("Login");
            register = new JButton("Register");

            this.setLayout(new BorderLayout(10, 15));
            this.setBorder(new EmptyBorder(10, 10, 10, 10));
            fieldsPanel.setLayout(new BorderLayout());

            usernameField.setColumns(8);
            passwordField.setColumns(8);

            userPanel.add(usernameLabel);
            userPanel.add(usernameField);

            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);

            fieldsPanel.add(userPanel, BorderLayout.CENTER);
            fieldsPanel.add(passwordPanel, BorderLayout.SOUTH);

            buttonsPanel.add(login);
            buttonsPanel.add(register);

            this.add(logo, BorderLayout.NORTH);
            this.add(fieldsPanel, BorderLayout.CENTER);
            this.add(buttonsPanel, BorderLayout.SOUTH);
        }
    }
    public class CreateUser extends JPanel {
        JPanel userPanel;
        JPanel passPanel;
        JPanel repPassPanel;
        JPanel buttonsPanel;
        JPanel fieldsPanel;

        JLabel username;
        JLabel password;
        JLabel repPassword;

        JTextField userField;
        JPasswordField passField;
        JPasswordField repPassField;

        JButton acceptButton;
        JButton cancelButton;

        public CreateUser() {

            userPanel = new JPanel();
            passPanel = new JPanel();
            repPassPanel = new JPanel();
            buttonsPanel = new JPanel();
            fieldsPanel = new JPanel();

            username = new JLabel("username: ");
            password = new JLabel("password: ");
            repPassword = new JLabel("repeat password: ");

            userField = new JTextField();
            passField = new JPasswordField();
            repPassField = new JPasswordField();

            acceptButton = new JButton("Accept");
            cancelButton = new JButton("Cancel");

            userField.setColumns(8);
            passField.setColumns(8);
            repPassField.setColumns(8);

            userPanel.add(username);
            userPanel.add(userField);

            passPanel.add(password);
            passPanel.add(passField);

            repPassPanel.add(repPassword);
            repPassPanel.add(repPassField);

            buttonsPanel.add(acceptButton);
            buttonsPanel.add(cancelButton);

            fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.Y_AXIS));

            this.setLayout(new BorderLayout(10, 15));
            this.setBorder(new EmptyBorder(10, 10, 10, 10));

            fieldsPanel.add(userPanel);
            fieldsPanel.add(passPanel);
            fieldsPanel.add(repPassPanel);

            this.add(fieldsPanel, BorderLayout.CENTER);
            this.add(buttonsPanel, BorderLayout.SOUTH);
        }
    }
}

所以,恢复(并在执行MCVE时添加了一些我想到的问题):

  1. 为什么框架没有按预期调整大小?
  2. 我使用的是正确的布局管理器吗?
  3. 是我的 lp.register.addActionListener(... 还是我这样做了 困难吗
  4. 提前致谢:)

1 个答案:

答案 0 :(得分:4)

切换packsetResizable来电。 setResizable会在某些系统上更改框架的边框大小,因此不会使用

frame.setResizable(false);
frame.pack();

你应该使用......

frame.pack();
frame.setResizable(false);

调用之间差异的一个例子......

Resizebale

您可能还想实际使用PageViewer布局管理器...

cardLayout = new PageViewer();

现在,UI不应该像你拥有的那样被包裹在布局管理器中,它们应该分开,例如...

CardLayout

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class CardLayoutTest {

    private static final String loginCard = "login";
    private static final String userCard = "createUser";

    private JFrame frame;
    private CardLayout cardLayout;
    private JPanel contentPane;

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

    public CardLayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                createAndShowUI();
            }
        });
    }

    public void createAndShowUI() {
        frame = new JFrame("Welcome");
        cardLayout = new PageViewer();
        contentPane = new JPanel(cardLayout);

        LoginPage lp = new LoginPage();
        CreateUser cu = new CreateUser();

        lp.register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createNewUser();
                frame.pack();
            }
        });

        contentPane.add(lp, loginCard);
        contentPane.add(cu, userCard);

        frame.add(contentPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    public void createNewUser() {
        cardLayout.show(contentPane, userCard);
    }

    public class LoginPage extends JPanel {

        Image image;
        ImageIcon imageIcon;

        JPanel userPanel;
        JPanel passwordPanel;
        JPanel buttonsPanel;
        JPanel fieldsPanel;

        JFrame frame;

        JLabel usernameLabel;
        JLabel passwordLabel;
        JLabel logo;

        JTextField usernameField;
        JPasswordField passwordField;

        JButton login;
        JButton register;

        public LoginPage() {
            image = Toolkit.getDefaultToolkit().getImage("image.jpg");
            imageIcon = new ImageIcon("image.jpg");

            userPanel = new JPanel();
            passwordPanel = new JPanel();
            buttonsPanel = new JPanel();
            fieldsPanel = new JPanel();

            usernameLabel = new JLabel("username:");
            passwordLabel = new JLabel("password:");
            logo = new JLabel(imageIcon);

            usernameField = new JTextField();
            passwordField = new JPasswordField();

            login = new JButton("Login");
            register = new JButton("Register");

            this.setLayout(new BorderLayout(10, 15));
            this.setBorder(new EmptyBorder(10, 10, 10, 10));
            fieldsPanel.setLayout(new BorderLayout());

            usernameField.setColumns(8);
            passwordField.setColumns(8);

            userPanel.add(usernameLabel);
            userPanel.add(usernameField);

            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);

            fieldsPanel.add(userPanel, BorderLayout.CENTER);
            fieldsPanel.add(passwordPanel, BorderLayout.SOUTH);

            buttonsPanel.add(login);
            buttonsPanel.add(register);

            this.add(logo, BorderLayout.NORTH);
            this.add(fieldsPanel, BorderLayout.CENTER);
            this.add(buttonsPanel, BorderLayout.SOUTH);
        }
    }

    public class CreateUser extends JPanel {

        JPanel userPanel;
        JPanel passPanel;
        JPanel repPassPanel;
        JPanel buttonsPanel;
        JPanel fieldsPanel;

        JLabel username;
        JLabel password;
        JLabel repPassword;

        JTextField userField;
        JPasswordField passField;
        JPasswordField repPassField;

        JButton acceptButton;
        JButton cancelButton;

        public CreateUser() {

            userPanel = new JPanel();
            passPanel = new JPanel();
            repPassPanel = new JPanel();
            buttonsPanel = new JPanel();
            fieldsPanel = new JPanel();

            username = new JLabel("username: ");
            password = new JLabel("password: ");
            repPassword = new JLabel("repeat password: ");

            userField = new JTextField();
            passField = new JPasswordField();
            repPassField = new JPasswordField();

            acceptButton = new JButton("Accept");
            cancelButton = new JButton("Cancel");

            userField.setColumns(8);
            passField.setColumns(8);
            repPassField.setColumns(8);

            userPanel.add(username);
            userPanel.add(userField);

            passPanel.add(password);
            passPanel.add(passField);

            repPassPanel.add(repPassword);
            repPassPanel.add(repPassField);

            buttonsPanel.add(acceptButton);
            buttonsPanel.add(cancelButton);

            fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.Y_AXIS));

            this.setLayout(new BorderLayout(10, 15));
            this.setBorder(new EmptyBorder(10, 10, 10, 10));

            fieldsPanel.add(userPanel);
            fieldsPanel.add(passPanel);
            fieldsPanel.add(repPassPanel);

            this.add(fieldsPanel, BorderLayout.CENTER);
            this.add(buttonsPanel, BorderLayout.SOUTH);
        }
    }

    public class PageViewer extends CardLayout {

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            Component current = findCurrentComponent(parent);
            if (current != null) {
                Insets insets = parent.getInsets();
                Dimension pref = current.getPreferredSize();
                pref.width += insets.left + insets.right;
                pref.height += insets.top + insets.bottom;
                return pref;
            }
            return super.preferredLayoutSize(parent);
        }

        public Component findCurrentComponent(Container parent) {
            for (Component comp : parent.getComponents()) {
                if (comp.isVisible()) {
                    return comp;
                }
            }
            return null;
        }
    }
}