2

我希望在按下按钮时显示不同的 JPanel。面板中显示的其他两个 JPanel 不得移动。我试过了,但是当按下按钮时,什么也没有发生。甚至多次按下按钮时,必须显示许多文本字段,否则是一个简单的滚动窗格。这是代码:

最终 JPanel 选择TypeOfAnswer = 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);
4

1 回答 1

0

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

于 2012-11-02T19:39:57.507 回答