不在JFrame中添加卡片布局

时间:2016-01-28 15:50:44

标签: java swing cardlayout

任何人都可以看到代码吗?我想创建一个页面,其中包含横幅和面板,其中卡片将根据要求进行更改。我在JFrame中添加了Banner(这很好)但问题是" CardLayout Panel没有添加JFrame"。

Actually, I need this.

enter image description here

按下按钮时,只有card1更改为card2,但横幅将保持不变。

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);
    }
}

2 个答案:

答案 0 :(得分:1)

  

我想制作一个包含横幅和面板卡片的页面   将改变要求。

  1. 您的组件无法集中精力,需要使用CardLayout来切换视图的某些事件(JButton,Swing Timer)

  2. 有关CardLayout to read Oracle tutorial的更多信息,有关工作代码示例,请参阅吨代码示例

  3. 您的代码在没有NullLayout的情况下工作(通过将BorderLayout设置为父JPanel),Jpanel的默认LayoutManager是FlowLayout(仅接受getPreferredSize,子项不能与其父/ s一起调整大小)

  4. 我的问题是为什么有两次代码行id,另一个......

  5. enter image description here

    basic_panel.add(card_Layout_panel, BorderLayout.CENTER);

答案 1 :(得分:0)

你正在做这个basic_panel.add(card_Layout_panel,BorderLayout.CENTER);两次,因此错误。 (在welcome_authentication_panel_card之前和之后检查)