Я бы хотел добавить JPanel из другого класса в JPanel:
class FirstPanel extends JPanel
{
private JButton button;
FirstPanel()
{
setLayout(null);
setVisible(true);
button = new JButton();
button.setBounds(x, y, width, height);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(SecondPanel.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
add(button);
ButtonActionHandler buttonActionHandler = new ButtonActionHandler();
}
public class ButtonActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(true);
add(new SecondJPanel());
new SecondJPanel().setVisible(true);
}
} }
А это мой второй JPanel:
class SecondPanel extends JPanel
{
private JButton button;
private JLabel titleLabel;
SecondPanel()
{
setLayout(null);
setVisible(true);
button = new JButton();
button.setBounds(100, 200, 100, 200);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(SecondPanel.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
add(button);
}
}
Запуск первой панели с помощью JFrame (из другого класса) работает, однако добавление второй JPanel к первой - нет.
Любая помощь очень ценится