简体   繁体   English

如何通过单击按钮在面板的相同区域中显示不同的JPanels

[英]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. 我希望在按下按钮时显示不同的JPanel。 The other two JPanels that are displayed in the panel are not to be moved. 面板中显示的其他两个JPanel将不被移动。 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(); 最后的JPanel selectedTypeOfAnswer = 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(...)使一个面板可见,然后隐藏另一个面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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