/***************************************************************/ /* Program Name: Lab 5 Experiment Code the CONTROL */ /* */ /* */ /* Program Overview: */ /* Sample code for Lab 5 */ /* */ /* Input: */ /* There is no user input to this program. */ /* */ /* */ /***************************************************************/ import java.awt.*; import javax.swing.*; import java.awt.event.*; // import classes for event handling public class Lab5ExperimentControl extends Lab5ExperimentView implements ActionListener { JPanel pDisplay; JTextField textDisplay; JFrame popUpFrame; // new JFrame public Lab5ExperimentControl() { System.out.println(" At Method constructor in Control"); //add listeners cancel.addActionListener(this); display.addActionListener(this); setTitle("5 Experiment Control"); setVisible(true); } // remember to have the main method public static void main(String args[]) { new Lab5ExperimentControl(); } public void actionPerformed(ActionEvent e) { System.out.println(" At Method action Performed"); String whichButton = e.getActionCommand(); if (whichButton.equals("Display")) createFrame(); else if (whichButton.equals("Cancel")) clearMessage(); validate(); } public void createFrame() { System.out.println(" At Method create Frame"); String sport,course, probationString; // local variables boolean inProbation; // local boolean popUpFrame = new JFrame("Extra Frame"); popUpFrame.setSize(100,100); popUpFrame.setBounds(150,150,300,50); displayMessage(); popUpFrame.add(pDisplay); popUpFrame.setVisible(true); } public void displayMessage() { System.out.println(" At Method display Message"); String sport,course, probationString; // local variables boolean inProbation; // local boolean pDisplay = new JPanel(new BorderLayout()); // get values from JCheckBox,JList and JComboBox course = (String) myList.getSelectedValue(); sport = (String) myChoice.getSelectedItem(); inProbation = probation.isSelected(); if(inProbation) probationString = " study hard "; else { probationString = " your OK "; ohSound.play(); } textDisplay = new JTextField (course+ " and " + sport + probationString,JLabel.CENTER) ; textDisplay.setFont(myFontBold); pDisplay.add(textDisplay,BorderLayout.CENTER); } public void clearMessage() { System.out.println(" At Method clear Message"); if(ohSound != null ) ohSound.stop( ); // stop playing myChoice.setSelectedIndex(0); myList.setSelectedIndex(0); probation.setSelected(false); panelCenter.remove(pDisplay); if(popUpFrame!=null) popUpFrame.dispose(); // dispose panel repaint(); } }