/* Written by Mahshid mzeinaly@sfu.ca for IAT351 lab2 Note that this class only works with the valid input from the user! (numbers) */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RandomColorSum implements MouseListener, ActionListener { JLabel EnterLabel; JButton EnterButt, ResetButt; int sum; JLabel SumTextLabel; JLabel SumResultLabel; JTextField textBox; public RandomColorSum(){ JFrame window = new JFrame("Random Color Sum!"); JPanel content = new JPanel(); content.addMouseListener( this ); // Register mouse listener. window.setContentPane(content); window.addWindowListener(new WindowHandler()); EnterLabel=new JLabel("Enter a new number:"); SumTextLabel=new JLabel("Sum is"); SumResultLabel=new JLabel(Integer.toString(sum)); textBox=new JTextField(); textBox.setPreferredSize(new Dimension(100,30)); EnterButt=new JButton("Enter"); ResetButt=new JButton("Reset"); EnterButt.addActionListener(this); ResetButt.addActionListener(this); content.add(EnterLabel); content.add(textBox); content.add(EnterButt); content.add(SumTextLabel); content.add(SumResultLabel); content.add(ResetButt); sum=0; window.setSize(350,200); window.setVisible(true); } public static void main(String[] args) { RandomColorSum myMainApp=new RandomColorSum(); //creating a new instance of our main class } public void mousePressed(MouseEvent evt) { //changing the color of all the texts (JLabels) EnterLabel.setOpaque(true); float hue = (float)Math.random(); // randomly changes the hue EnterLabel.setForeground(Color.getHSBColor(hue, 1.0F, 1.0F) ); SumResultLabel.setForeground(Color.getHSBColor(hue, 1.0F, 1.0F) ); SumTextLabel.setForeground(Color.getHSBColor(hue, 1.0F, 1.0F) ); } public void mouseClicked(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } @Override public void actionPerformed(ActionEvent arg0) { if(((JButton)arg0.getSource()).equals(EnterButt)){ // Case1: "Enter" button is pressed sum+=Integer.parseInt(textBox.getText()); SumResultLabel.setText(Integer.toString(sum)); } if(((JButton)arg0.getSource()).equals(ResetButt)){ Case2: "Reset" button is pressed sum=0; textBox.setText(""); SumResultLabel.setText(Integer.toString(sum)); } } private static class WindowHandler implements WindowListener // a seperate class handles the Window Events { @Override public void windowActivated(WindowEvent arg0) { System.out.println("Activated"); } @Override public void windowClosed(WindowEvent arg0) { System.out.println("Closed"); JFrame EndFrame=new JFrame("End"); // a new window apears when the program is closed JLabel Enter2=new JLabel("Good Bye!"); JPanel content = new JPanel(); content.add(Enter2); EndFrame.setContentPane(content); EndFrame.setSize(new Dimension(200,100)); EndFrame.setLocation(400, 200); EndFrame.setVisible(true); } @Override public void windowClosing(WindowEvent arg0) { arg0.getWindow().dispose(); // comment this line and see the differnce! System.out.println(arg0.getWindow().isVisible()); } @Override public void windowDeactivated(WindowEvent arg0) { System.out.println("Deactivated"); } @Override public void windowDeiconified(WindowEvent arg0) { System.out.println("Deiconified"); } @Override public void windowIconified(WindowEvent arg0) { System.out.println("Iconified"); } @Override public void windowOpened(WindowEvent arg0) { System.out.println("Opened"); } }// private class }