如何在多个JPanel中使用CardLayout?

时间:2018-12-06 01:55:50

标签: java user-interface cardlayout

第一个屏幕是使用JSplitPane分割的2个JPanels。如何切换到另一个设置相同的屏幕。我尝试将前两个JPanels放入面板容器中,但这不起作用。然后,我试图将它们分开放置而不装容器,但随后只显示了第二个底部面板。如何使用cardLayout在两个格式化的面板之间切换。如果要做到这一点的另一种方法是三种,请告诉我,因为当必须以某种方式格式化所有面板时,CardLayout有点难以使用。

public class Quizlet extends JFrame implements ActionListener  {

public JPanel Container, FTop, FBottom,QTop,QBottom;
public JScrollPane Scroll;
public JSplitPane FirstSplit,QuesSplit,FinalSplit;
public JTextArea Text;
public JButton Start, Next, Finish;
public JTextField Answer;
public JLabel Hello,Ans;
CardLayout Switch = new CardLayout();


public Quizlet () {

    Container = new JPanel();   
    Container.setLayout(Switch);
    Container.add(FTop, null);
    Container.add(FBottom,null);
    Switch.show(FTop,null);
    Switch.show(QBottom, null);


    FTop = new JPanel(new BorderLayout());


    Hello = new JLabel("Please Click start to begin");
    FTop.add(Hello, BorderLayout.CENTER);

    FBottom = new JPanel(new BorderLayout());


    Start = new JButton("Start Session");
    Start.addActionListener(this);

    FBottom.add(Start,BorderLayout.CENTER);

    FirstSplit = new JSplitPane();
    FirstSplit.setTopComponent(FTop);                  // at the top we want our "topPanel"
    FirstSplit.setBottomComponent(FBottom); 

    add(FirstSplit);
    FirstSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);  
    FirstSplit.setDividerLocation(200);                    
    FirstSplit.setTopComponent(FTop);                  
    FirstSplit.setBottomComponent(FBottom); 

    //Second Screen top component

    QTop = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    Ans = new JLabel("Answer");
    c.gridx=0;
    c.gridy=0;
    QTop.add(Ans, c);


    Answer = new JTextField(12);
    c.gridx=0;
    c.gridy=1;
    QTop.add(Answer, c);
    //Second Screen Bottom Component
    QBottom = new JPanel(new BorderLayout());
    Next = new JButton("Next Question");
    QBottom.add(Next,BorderLayout.CENTER);

    //Second screen Split
    QuesSplit = new JSplitPane();
    QuesSplit.setTopComponent(QTop);                  
    QuesSplit.setBottomComponent(QBottom); 

    add(QuesSplit);
    QuesSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);  
    QuesSplit.setDividerLocation(200);                    
    QuesSplit.setTopComponent(QTop);                  
    QuesSplit.setBottomComponent(QBottom); 



    setSize(500,500);
    setVisible(true);
    setTitle("The Quiz");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();


}
public void actionPerformed (ActionEvent e) {
    if(e.getSource()==Start) {
        Switch.show(QTop, null);
        Switch.show(QBottom, null);
    }
}

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Quizlet show = new Quizlet();
            }
        });
    }
}

0 个答案:

没有答案