+ 1
How can chang jpanel or replace the jpanel in java swing?
i want chang the by click a button if i click again jpanel come back?
3 ответов
+ 3
how explain more or write it code
0
Toggle the panel visibility using setVisible(true/false) method.
0
Example: if you had a panel variable called "myPanel" you can just do
myPanel.setVisible(true);
This will make you panel stay visible. If you need hide it use
myPanel.setVisible(false);
then your panel will get hidden.
To do do this by clicking a button you can create a button and write logic inside its "actionListener", such as
myButton.addActionListener(new ActionListener(){
//your logic of toggle visibility goes here...
});
You need look that you should check if your panel are visible or not before hiding/showing it up. You only will hide if panel are being shown and will only show if panel are hidden. To check this you can use the boolean method "isVisible()" such as:
if (myPanel.isVisible()){
myPanel.setVisible(false);
} else {
myPanel.setVisible(true);
}
or in a single line
myPanel.setVisible(!myPanel.isVisible());
Hope you understand. GL