简体   繁体   中英

Not adding Card Layout in JFrame

Can anyone see the code ? I want to make a page that has a banner and a pannel in which cards will change on the requirement. I added the Banner in JFrame (That is working fine) but The problem is that " CardLayout Panel is not adding in the JFrame".

Actually, I need this.

在此处输入图片说明

When button is pressed only card1 change to card2 but banner will remain same.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class gui extends JFrame{

    private static final long serialVersionUID = 1L;

    JPanel  
    basic_panel,
    card_Layout_panel,
    banner_panel,
    welcome_authenticaion_panel_card1;

    CardLayout basic2;

    JLabel 
    logo_label,
    name_label;


    public gui(){

        server_login_gui();
        add(basic_panel);
        standard_gui(); 
    }

    public void server_login_gui(){


        basic_panel = new JPanel();
        basic_panel.setLayout(null);
        basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));

        banner_panel = new JPanel();
        banner_panel.setLayout(null);
        banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
        banner_panel.setSize(680, 200);//(400,100,400,100);


        //////Banner inner things//////////////////////////////////////////////////
        logo_label = new JLabel("Logo");
        logo_label.setBounds(30,40,100,100);
        logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(logo_label);

        name_label = new JLabel("        Name.....   ");
        name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC,25));
        name_label.setBounds(200,80,400,50);
        name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(name_label);
        ////////////////////////////////////////////////////////////////////////        

//          basic_panel.add(banner_panel,BorderLayout.NORTH);


        ///////// Card Layout//////////////
        basic2 = new CardLayout();
        card_Layout_panel = new JPanel(basic2);
        card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
        basic_panel.add(card_Layout_panel,BorderLayout.CENTER);

        welcome_authenticaion_panel_card1 = new JPanel();
        welcome_authenticaion_panel_card1.setLayout(null);
        welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
        welcome_authenticaion_panel_card1.setBounds(0,200,680,460);

        card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");

        basic_panel.add(card_Layout_panel,BorderLayout.CENTER);


            /////////////////////////////////////////////////////////////////////////
    }

    public void standard_gui(){
        setSize(700,700);
        setTitle("System");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

I want to make a page that has a banner and a pannel in which cards will change on the requirement.

  1. your component aren't focusable, there is required some event (JButton, Swing Timer) for switching the view by using CardLayout

  2. for more info about CardLayout to read Oracle tutorial , for working code exampes, tons code examples are here

  3. you code works without NullLayout (by set BorderLayout to parent JPanel), default LayoutManager for Jpanel is FlowLayout (accepts only getPreferredSize, childs aren't resizable with its parent/s)

  4. my question is for why reason is there code line basic_panel.add(card_Layout_panel, BorderLayout.CENTER); twice, and another ...

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel basic_panel, card_Layout_panel,
            banner_panel, welcome_authenticaion_panel_card1;
    private CardLayout basic2;
    private JLabel logo_label, name_label;

    public Gui() {
        server_login_gui();
        add(basic_panel);
        standard_gui();
    }

    public void server_login_gui() {

        basic_panel = new JPanel();
        basic_panel.setLayout(new BorderLayout(10, 10));
        basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        banner_panel = new JPanel();
        //banner_panel.setLayout(null);
        banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
        banner_panel.setSize(680, 200);//(400,100,400,100);
        //////Banner inner things//////////////////////////////////////////////////
        logo_label = new JLabel("Logo");
        //logo_label.setBounds(30, 40, 100, 100);
        logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(logo_label);
        name_label = new JLabel("        Name.....   ");
        name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
        //name_label.setBounds(200, 80, 400, 50);
        name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(name_label);
        ////////////////////////////////////////////////////////////////////////      
        basic_panel.add(banner_panel, BorderLayout.NORTH);
        ///////// Card Layout//////////////
        basic2 = new CardLayout();
        card_Layout_panel = new JPanel(basic2);
        card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
        basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
        welcome_authenticaion_panel_card1 = new JPanel();
        welcome_authenticaion_panel_card1.setLayout(null);
        welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
        //welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
        card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
        basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
        /////////////////////////////////////////////////////////////////////////
    }

    public void standard_gui() {
        setSize(700, 700);
        setTitle("System");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Gui();
            }
        });
    }
}

you're doing this basic_panel.add(card_Layout_panel,BorderLayout.CENTER); twice, hence the error. ( check before and after the welcome_authentication_panel_card )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM