Проблема: когда кнопка скрыта, фоновая панель не перерисовывается должным образом.
Упрощенный код:
Код: Выделить всё
public class MyPanel extends JPanel { // This is where I draw game-related stuff in the background
public MyPanel() {
JPanel toolPanel = new JPanel();
this.add(toolPanel, BorderLayout.SOUTH);
}
}
public class ToolPanel extends JPanel { // This panel holds the buttons overlaid onto MyPanel
Color transparent = new Color(255, 255, 255, 0);
toolPanel.setForeground(transparent);
toolPanel.setBackground(transparent);
JButton mainButton = new JButton("main button");
toolPanel.add(mainButton);
JButton subButton = new JButton("sub-button");
toolPanel.add(subButton);
subButton.setVisible(false);
mainButton.addActionListener((e) -> {
subButton.setVisible(!subButton.isVisible());
System.out.println("Setting sub-button to " + subButton.isVisible());
// repaint();
// invalidate(); // None of these can force the parent JPanel to repaint...
});
}
нормальное состояние
Что я получаю, когда показываю вторую кнопку:
фон подпанель нарисована неправильно
И когда я сделал вторую кнопку невидимой (я ожидаю вернуться к первому изображению):
основная кнопка в порядке, панель кнопок фон нокаут
Подробнее здесь: https://stackoverflow.com/questions/790 ... nt-resizes