07/12/12 08:34:31
>>542
それだと SubPanel が汎用性の低いクズパーツになるだろ。
SubPanel がボタンを持つパネルならそのボタンのアクションリスナーを
>>534のように SubPanel に委譲してやったほうがいい。
public class SubPanel extends JPanel {
private JButton button;
public SubPanel(String buttonText) {
button = new JButton(buttonText);
this.add(button);
}
public void addButtonActionListener(ActionListener listener) {
button.addActionListener(listener);
}
}
public class MainPanel extends JPanel implements ActionListener {
public MainPanel() {
SubPanel panel = new SubPanel("メインパネルの色を変更");
panel.addButtonActionListener(this);
this.add(panel);
}
public void actionPerformed(ActionEvent e) {
this.setBackground(Color.BLACK);
}
}