在切换按钮上将JPanel添加到其他Jpanel上单击 [英] JPanel is added onto other Jpanel on togglebutton Click
问题描述
我正在尝试制作一个弹出面板,该面板在JToggleButton的帮助下被激活. 我希望在选择ToggleButton时将JPanel添加到另一个Jpanel上,而在取消选择ToggleButton时将其隐藏.
I am trying to make a popup panel which is activated with the help of JToggleButton. I want the JPanel to be added onto the other Jpanel when ToggleButton is selected and hides when the ToggleButton is deselected.
我已经声明了JToggleButton并使用了ItemListener.但是发生的是,当我选择ToggleButton时,如果我取消选择并再次选择它,则会创建一个面板,然后再次添加另一个JPanel,依此类推,然后单击5次,则什么也不会出现.
I have declared the JToggleButton and used ItemListener. But What is happening is when i select the ToggleButton a panel is created if i deselect and Select it again another JPanel is added again and so on and After 5 clicks , Nothing appears.
public static JPanel createDesignButtons(){
designButtonsPanel.setOpaque(false);
BoxLayout boxLayout = new BoxLayout(designButtonsPanel, BoxLayout.LINE_AXIS);
designButtonsPanel.setLayout(boxLayout);
mainButton.setIcon(Icons.venueIcon);
mainButton.setBorderPainted(false);
mainButton.setPreferredSize(new Dimension(40,40));
mainButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if(ev.getStateChange()==ItemEvent.SELECTED){
designButtonsPanel.add(createButtonsDialog());
designButtonsPanel.validate();
} else if(ev.getStateChange()==ItemEvent.DESELECTED){
System.out.println("button is not selected");
}
}
});
designButtonsPanel.add(mainButton);
JLabel padding = new JLabel(" ");
padding.setPreferredSize(null);
JLabel divider = new JLabel("", Icons.dividerIcon, JLabel.CENTER);
divider.setPreferredSize(new Dimension(3,45));
designButtonsPanel.add(divider);
SwingUtilities.updateComponentTreeUI(designButtonsPanel);
return(designButtonsPanel);
}
上面的代码显示了mainButton是要对其进行操作的切换按钮,而DesignButtonPanel是作为父项的Panel.
The above code is showing the mainButton is togglebutton that i want action on and the DesignButtonPanel is the Panel which is parent.
public static JPanel createButtonsDialog(){
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBorder(new LineBorder(Color.gray,1));
return buttonsPanel;
}
此类显示我要添加到父面板的面板
This class show the panel i would like to add onto parent panel
当JtoggleButton被选中时,如何才能仅添加面板一次,而取消选中该面板时又如何隐藏呢?
How can i get the Panel added only once when the JtoggleButton is Selected and hides when its Deselected?
推荐答案
问题是您一直在创建JPanel
的实例.
如果未选择JToggleButton
,则可以remove
JPanel
;如果选择了按钮,则可以添加已经创建的JPanel
实例.
看这个简单的例子:
The problem is you keep creating instances of your JPanel
.
You could remove
the JPanel
if your JToggleButton
is not selected and add an already created instance of your JPanel
if the button is selected.
See this simple example:
public class MainFrame extends JFrame {
private JPanel topPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JToggleButton toggleButton = new JToggleButton("Toggle");
public MainFrame() {
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.topPanel.setPreferredSize(new Dimension(100, 100));
this.centerPanel.setPreferredSize(new Dimension(100, 100));
this.toggleButton.setPreferredSize(new Dimension(100, 100));
this.add(topPanel, BorderLayout.NORTH);
this.add(centerPanel, BorderLayout.CENTER);
this.add(toggleButton, BorderLayout.SOUTH);
this.toggleButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
add(centerPanel, BorderLayout.CENTER);
} else {
remove(centerPanel);
}
pack();
}
});
this.pack();
this.setVisible(true);
}
}
您可以看到centerPanel
仅实例化一次.
You can see that centerPanel
is only instantiated once.
这篇关于在切换按钮上将JPanel添加到其他Jpanel上单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!