Showing posts from December, 2012

Simple Calculator

//Imports are listed in full to show what's being used //could just import javax.swing.* and java.awt.* etc.. import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.Container; public class SimpleCalc implements ActionListener{ JFrame guiFrame; JPanel buttonPanel; JTextField numberCalc; int calcOperation = 0; int currentCalc; //Note: Typically the main method will be in a //separate class. As this is a simple one class //example it's all in the one class. public static void main(String[] args) { new SimpleCalc(); } public SimpleCalc() { guiFrame = new JFrame(); //make sure the program exits when the f

Event Handling in Java - Assignment 2

The objective of this assignment is to understand events handling in abstract way. We have used events with GUI components, but events are not only associated with GUI components. You can define events and fire them even when there is no GUI of your application. In this assignment you are supposed to create ResultAnnouncedEvent which is fired by ExaminationCenter. Different entities (or classes) can register themselves as listeners so that when result is announced they are notified, for example Student, Teacher and Administrator. So these classes are event listeners. These classes should just receive the event and print the event attributes. The ResultAnnouncedEvent class should be composed of class, section, and date attributes. Also create ExaminationCenterTest class that fires the event. Before doing this assignment, it is recommended you read API docs of java.util.EventObject, java.util.EventListener. Also See: Due: Next Lab.

Introduction to Java Swing import java.awt.FlowLayout; // loads images import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants;  public class LabelFrame extends JFrame  {  private JLabel label1; // JLabel with just text  private JLabel label2; // JLabel constructed with text and icon  private JLabel label3; // JLabel with added text and icon  // LabelFrame constructor adds JLabels to JFrame  public LabelFrame()  { super( "Testing JLabel" );  setLayout( new FlowLayout() );  label1 = new JLabel( "Label with text" ); label1.setToolTipText( "This is label1" ); add( label1 ); Icon bug = new ImageIcon( getClass().getResource( "bug.jpg" ) ); label2 = new JLabel( "Label with text and icon", bug, SwingConstants.LEFT ); label2.setToolTipText( "This is label2" ); add( label2 ); label3 = new JLabel(); // JLabel constructor no arguments label3.setText( "Label