Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [TimerDemo.java]
- Exploiting anomalies:
- The code implemented here is proof that no two programmers think alike.
- It is coding convention to have an exception report an error (or anomaly, to be more precise), and
- have you program take relative actions.
- This piece of code is written to reverse the aforementioned-trait however (portray the exception
- as 'normal' operations), and (the catch block) is used to do the 'contemporary' work.
- (c) Damion 'Phr0z3n.dev' Tapper, 2015.
- Email: [email protected]
- Website: http://L337Stuff.Blogspot.com
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- import javax.swing.Timer;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class TimerDemo extends JFrame implements ActionListener
- {
- public static void main(String arg[])
- {
- new TimerDemo();
- }
- public TimerDemo()
- {
- this.addWindowListener(new WinAdapt());
- new Timer(1000 * 1, this).start(); // constructs a timer to pulse at 1 second intervals, and to report the timer pulses to the ActionListener
- buildMenu();
- buildFrame();
- this.setLocation(250, 150);
- this.setVisible(true);
- }
- private void buildFrame() // for aesthetics
- {
- JButton exitButton = new JButton("Exit");
- exitButton.addActionListener(this);
- this.add(exitButton);
- this.setSize(300, 200);
- }
- private void buildMenu()
- {
- JMenuBar menuBar = new JMenuBar();
- JMenu fileMenu = new JMenu("File");
- menuBar.add(fileMenu);
- JMenuItem exitMenuItem = new JMenuItem("Exit");
- exitMenuItem.addActionListener(this);
- fileMenu.add(exitMenuItem);
- this.setJMenuBar(menuBar);
- }
- @Override
- public void actionPerformed(ActionEvent e) // called with each expiration of the timer
- {
- try
- {
- /**
- * NOTE:
- * The command string is null if no user input is done (one was not set).
- *
- * This is the magic bullet that triggers the exception (no pun intended).
- */
- switch(e.getActionCommand())
- {
- case "Exit":
- System.exit(0);
- break;
- }
- }
- catch(NullPointerException ex) // a NullPointerException is thrown if 'switch' tries to operate on a null string...
- {
- /**
- * ...and when an exception should be a 'negative thing'...
- */
- System.out.println("Timer fired!"); // I did this cause I can, Yes I did! Yes I did!
- }
- }
- }
- class WinAdapt extends WindowAdapter
- {
- @Override
- public void windowClosing(WindowEvent e)
- {
- super.windowClosing(e);
- if(e.getID() == WindowEvent.WINDOW_CLOSING)
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment