/** ButtonPanel.java -- the input to the program will be in * the form of the user pressing buttons. */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ButtonPanel extends JPanel { private PicturePanel pixPanel; private JButton dayButton; private JButton nightButton; public ButtonPanel(PicturePanel p) { pixPanel = p; // Create buttons, and initialize their special features. // Even though setMnemonic would work with 'd' as well as 'D', we'd // also like the 'D' in Daytime to be underlined. // To use a mnemonic, hit ALT-D or ALT-N as appropriate. dayButton = new JButton("Daytime"); dayButton.setEnabled(false); dayButton.setMnemonic('D'); dayButton.setToolTipText("See it during the day"); dayButton.addActionListener(new DayListener()); nightButton = new JButton("Nighttime"); nightButton.setEnabled(true); nightButton.setMnemonic('N'); nightButton.setToolTipText("See it at night!"); nightButton.addActionListener(new NightListener()); add(dayButton); add(nightButton); //setBorder(BorderFactory.createLineBorder(Color.yellow, 4)); //setBorder(BorderFactory.createTitledBorder("Button Controls")); } // Need separate listeners for the buttons, because they behave // somewhat differently (although analogously). private class DayListener implements ActionListener { public void actionPerformed(ActionEvent e) { pixPanel.setDaytime(true); dayButton.setEnabled(false); nightButton.setEnabled(true); pixPanel.repaint(); } } private class NightListener implements ActionListener { public void actionPerformed(ActionEvent e) { pixPanel.setDaytime(false); dayButton.setEnabled(true); nightButton.setEnabled(false); pixPanel.repaint(); } } }