Java 卡片布局没有';t显示JPanel

Java 卡片布局没有';t显示JPanel,java,swing,awt,layout-manager,cardlayout,Java,Swing,Awt,Layout Manager,Cardlayout,我正在为数据库做一个表单,我想使用CardLayout。 我解释了这种情况:用户有两种选择:要么他连接到他的帐户,要么他是新的,他需要创建一个帐户。 我有一个带有表单的第一个面板和一个用于测试的另一个面板(其中我将背景设置为黑色,以便在用户单击“新建”按钮时看到更改)。当我启动主程序时,我只在窗口中显示标题,没有其他内容。或者这不是我想要的,我的应用程序的第一步是显示表单。因此,如果有人能解释我的错误在哪里,我认为:) 您在哪里使用JPanel、main将CardLayout添加到任何内容 回答

我正在为数据库做一个表单,我想使用CardLayout。 我解释了这种情况:用户有两种选择:要么他连接到他的帐户,要么他是新的,他需要创建一个帐户。 我有一个带有表单的第一个面板和一个用于测试的另一个面板(其中我将背景设置为黑色,以便在用户单击“新建”按钮时看到更改)。当我启动主程序时,我只在窗口中显示标题,没有其他内容。或者这不是我想要的,我的应用程序的第一步是显示表单。因此,如果有人能解释我的错误在哪里,我认为:)


您在哪里使用JPanel、main将CardLayout添加到任何内容


回答:你没有。要使其工作,必须将其添加到最终通向顶层窗口的组件中。必须显示它才能显示其“卡”。

您在哪里使用JPanel、main将卡布局添加到任何内容


回答:你没有。要使其工作,必须将其添加到最终通向顶层窗口的组件中。必须显示它才能显示其“卡片”。

@user1086267:不客气。遇到这样的问题时,尽量将代码简化到最低限度,以了解问题的本质。通过这种方式,您可能一眼就能看到问题及其解决方案,或者如果没有,您将能够发布编译和运行的简单小代码,我们可以使用这些代码帮助您解决问题。@user1086267:不客气。遇到这样的问题时,尽量将代码简化到最低限度,以了解问题的本质。通过这种方式,您可能会一目了然地看到问题及其解决方案,如果没有,您将能够发布编译和运行的简单小代码,我们可以使用这些代码帮助您解决问题。
public class Accueil extends JFrame implements ActionListener {

    private CardLayout l = new CardLayout();
    private JPanel main = new JPanel(l);
    private JPanel change = new JPanel();
    private JPanel home = new JPanel(new BorderLayout());
    private JPanel title = new JPanel();
    private JPanel login = new JPanel(new BorderLayout());
    private JPanel login_title = new JPanel();
    private JPanel login_info = new JPanel();
    private JPanel copyright = new JPanel();
    private JLabel welcome = new JLabel("TEST");
    private JLabel connexion = new JLabel("Connexion");
    private JLabel mail = new JLabel("Mail : "); 
    private JTextField input_mail = new JTextField();
    private JLabel mdp = new JLabel("Password : ");
    private JTextField input_mdp = new JTextField();
    private JLabel who = new JLabel ("Are you a  : ");
    private JRadioButton client = new JRadioButton("Client", true);
    private JRadioButton developpeur = new JRadioButton("Developer", false);
    private ButtonGroup bg = new ButtonGroup();
    private JButton ok = new JButton("Login");
    private JButton annule = new JButton("Cancel");
    private JButton inscription = new JButton("New ?");
    private JLabel align = new JLabel("");

    public Accueil() {

        init_title();
        init_login();
        add_button();
        organize_frame();
        inscription.addActionListener(this);
        main.add(home, "Home");
        main.add(change, "Change");
        l.show(main, "Home");
        change.setBackground(Color.black);
        this.setTitle("test");
        this.setSize(600,600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent arg0) {
        l.show(main, "Change");
    }

    private void organize_frame() { 
        home.add(title, BorderLayout.NORTH);
        home.add(login, BorderLayout.CENTER);
        home.add(copyright, BorderLayout.SOUTH);
    }

    private void init_title() { 
        welcome.setFont(build_font(35));
        welcome.setBorder(new EmptyBorder(20,0,0,0));
        title.setPreferredSize(new Dimension(600,80));
        title.add(welcome);
    }

    private void init_login() {
        setConnexion();
        user_input();
        align.setPreferredSize(new Dimension(215,50));
        organize_button();
        login.add(login_title, BorderLayout.NORTH);
        login.add(login_info, BorderLayout.CENTER);
    }

    private void setConnexion() {
        connexion.setFont(build_font(30));
        connexion.setBorder(new EmptyBorder(20,0,0,0));
        login_title.add(connexion);
        login_title.setPreferredSize(new Dimension(600,100));
    }

    private void user_input() {
        who.setFont(build_font(25));
        mail.setFont(build_font(25));
        mail.setBorder(new EmptyBorder(0,120,0,0));
        mdp.setFont(build_font(25));
        input_mdp.setFont(build_font(25));
        input_mail.setFont(build_font(25));
        input_mail.setPreferredSize(new Dimension(300,40));
        input_mdp.setPreferredSize(new Dimension(300,40));
        who.setFont(build_font(20));
        client.setFont(build_font(20));
        developpeur.setFont(build_font(21));
        login_info.add(who);
        login_info.add(client);
        login_info.add(developpeur);
        login_info.add(mail);
        login_info.add(input_mail);
        login_info.add(mdp);
        login_info.add(input_mdp);
        login_info.add(align);
    }