如何使用cardLayout将面板分为2个面板?

时间:2019-05-01 15:58:15

标签: java swing awt layout-manager cardlayout

我试图将一个大面板分成2个面板,并在大面板中显示它们,但是没有用。但是,当我播放程序时,即使两个面板都设置为不同的位置。

public class PaperScissorRock extends JFrame implements Runnable {


ImageIcon[] gameIcons= {new ImageIcon("C:\\Jsp\\HelloJava\\src\\Images\\r.png"),new ImageIcon("C:\\Jsp\\HelloJava\\src\\Images\\s.png"),new ImageIcon("C:\\Jsp\\HelloJava\\src\\Images\\p.png")};
JLabel me=new JLabel("Me");
JLabel com=new JLabel("Computer");
JButton[] jb=new JButton[3];//
CardLayout psrLayout=new CardLayout();/


PaperScissorRock()
{


    JPanel bigPanel=new JPanel(psrLayout);//A panel to include 2 panels(northP/southP)
    JPanel northP=new JPanel();
    northP.setLayout(null);
    northP.setBackground(Color.YELLOW);
    northP.setBounds(400, 150, 400, 50);//
    for(int i=0; i<3; i++)
    {
        jb[i]=new JButton(gameIcons[i]);
        jb[i].setSize(10, 10);
        northP.add(jb[i], BorderLayout.NORTH);
    }


    JPanel southP=new JPanel();
    southP.setLayout(null);
    southP.setLayout(new FlowLayout());
    southP.setBackground(Color.GRAY);
    southP.setBounds(400, 100, 400, 100);
    southP.add(me, BorderLayout.SOUTH);southP.add(com, BorderLayout.SOUTH);

    bigPanel.setBounds(400, 150, 400, 450);
    bigPanel.add(northP, "nbp");bigPanel.add(southP, "sbp");
    psrLayout.show(bigPanel, "nbp");//this is not called(?)
    psrLayout.show(bigPanel, "sbp");


    Game.Gframe.add(bigPanel);



}

在bigPanel中,我想同时显示两个面板(northP / southP)。

0 个答案:

没有答案