简体   繁体   中英

How to display different JPanels in the same region of the panel with clicking a button

I want a different JPanel to be displayed when I press a button. The other two JPanels that are displayed in the panel are not to be moved. I've tried, but when pressing the button, nothing happens. When pressing the button even times, many textfields must be displayed, else a simple scrollpane. Here is the code:

final JPanel choseTypeOfAnswer = new JPanel();

    choseTypeOfAnswer.add(radioBox);
    radioBox.setToolTipText("Answer in form of radiobox");
    radioBox.setIcon(GUI.createImageIcon("check.png"));          
    radioBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    radioBox.setPreferredSize(new Dimension(30, 20));  
    radioBox.setVisible(true);
    radioBox.addActionListener(new ActionListener(){

        private int clicked;
        public void actionPerformed(ActionEvent e){
            clicked++;
            //reset contents of the image preview field
            if((clicked % 2) == 0){

            add(new JLabel("<html><b>Answer:</b>"));

            JTextField textField = new JTextField(20);
            JTextField textField1 = new JTextField(20);
            JTextField textField2 = new JTextField(20);
            JTextField textField3 = new JTextField(20);

            choseTypeOfAnswer.add(textField);
            choseTypeOfAnswer.add(textField1);
            choseTypeOfAnswer.add(textField2);
            choseTypeOfAnswer.add(textField3);

            add(BorderLayout.NORTH,choseTypeOfAnswer);
            }else{
            add(new JLabel("<html><b>Answer:</b>"));
            //now a scroll pane for the answer area
            JScrollPane answerScroller = new JScrollPane(answerArea);
            answerScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
            answerScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            choseTypeOfAnswer.add(answerScroller);
            //add(answerScroller);
            add(choseTypeOfAnswer);

            }
        }
        });//den emfanizei tpt me to patima tou koympiou

    add(choseTypeOfAnswer);

    //holds the bottom two components: "important" checkbox, "create card" button
    JPanel bottomContainer = new JPanel();
    //bottomContainer.setLayout(new BoxLayout(bottomContainer,BoxLayout.PAGE_AXIS));
    //important.setAlignmentX(Component.CENTER_ALIGNMENT);
    //bottomContainer.add(important);

    createCard.setText("Finish and add card");
    createCard.setIcon(GUI.createImageIcon("check.png"));          
    createCard.setAlignmentX(Component.CENTER_ALIGNMENT);
    createCard.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    createCard.setPreferredSize(new Dimension(180, 45));           
    createCard.addActionListener(new cardListener());
    bottomContainer.add(createCard);

    //now add the bottom container
    add(bottomContainer, BorderLayout.SOUTH);

使用Component#setVisible(...)使一个面板可见,然后隐藏另一个面板。

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