Хочу по нажатию Кнопки спрятать одну панель, что позволит увидеть другую У меня такой код
class MyWindow extends JFrame {
public MyWindow (){
setTitle("X/O");
setBounds(300,300,400, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon("icons.png");
setIconImage(icon.getImage());
JPanel mainPanel = new JPanel(); //Создаю панели
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
JPanel centerPanelWindowOne = new JPanel();
JPanel centerPanelWindowTwo = new JPanel();
mainPanel.setBackground(Color.blue); //Цвета основных панелей
centerPanel.setBackground(Color.white);
bottomPanel.setPreferredSize(new Dimension(1,40));//Расположение остальных панелей
centerPanel.add(centerPanelWindowOne);
centerPanel.add(centerPanelWindowTwo);
centerPanelWindowOne.setPreferredSize(new Dimension(400,40));
centerPanelWindowTwo.setPreferredSize(new Dimension(400,40));
add(mainPanel, BorderLayout.CENTER); //Указываю компановку
add(bottomPanel, BorderLayout.SOUTH);
add(centerPanel, BorderLayout.CENTER);
mainPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new FlowLayout());
bottomPanel.setLayout(new GridLayout());
centerPanelWindowOne.setLayout(new FlowLayout());
centerPanelWindowTwo.setLayout(new FlowLayout());
JButton start = new JButton("Старт");
bottomPanel.add(start);
JButton exit = new JButton("Выход");
bottomPanel.add(exit);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
centerPanel.setVisible(false);
}
});
setVisible(true);
}
}
Итого, после нажатия кнопки Start хочу закрыть видимости centrePanel, и увидеть mainPanel, по моей логике я сначала создаю и показываю mainPanel, а затем centrePanel, и когда кнопкой я указываю видимость false для centrePanel, то должна отображаться mainPanel, но после кнопки start, в панель centrePanel закрывается, а там ничего, как правильно реализовать?