[英]how to display panel on click of button on different panel in another panel present in jframe in netbean java
[英]How to display different JPanels in the same region of the panel with clicking a button
我希望在按下按鈕時顯示不同的JPanel。 面板中顯示的其他兩個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.