Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [source] Credit Limit w/ AWT GUI, neat code tricks used
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
the_short1




PostPosted: Thu Apr 13, 2006 10:37 am   Post subject: [source] Credit Limit w/ AWT GUI, neat code tricks used

Hey fellow compsciers, after a long vacation from compsci, I'm BACK! That is, now that I got the time (robotics season with FIRST is over)

Anyways, the purpose of this class assignment was to calculate the balance of a users creditcard, based on the purchases made, starting balance, and see if you went over limit. Being in gr12, we were told to do it with GUI, and I thought I would share it since I used some fun tricks to manipulate the data.

Please dont plagiarise, youll get caught by your teacher!!! <<students>>

Tell me what you think? any glitches (besides if you entered letters instead of numbers)? Comments?

-Kevin



CreditLimit.java
 Description:
the source code... exactly 200lines of code

Download
 Filename:  CreditLimit.java
 Filesize:  8.89 KB
 Downloaded:  312 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Apr 13, 2006 1:03 pm   Post subject: (No subject)

If I may ask... why not use Swing? I'm pretty sure even RTP should support it.

As for your actual UI: I would highly suggest you use layout managers. They'll seem harder, but they'll make your life easier. Ask the Win32 people, for instance. They have no layout managers. When exposed to one they act like they've seen a miracle.

I would also suggest you use labels.

I would suggest you create ActionListeners for each widget that needs one, rather than a single implementation that then has to differentiate between the different widgets. You'll want to understand anonymous inner classes for this.

Oh, and this:

code:
        public static int credit1 = 0; // a purchase
        public static int credit2 = 0; // a purchase
        public static int credit3 = 0; // a purchase
        public static int credit4 = 0; // a purchase
        public static int credit5 = 0; // a purchase
        public static int credit6 = 0; // a purchase
        public static int credit7 = 0; // a purchase
        public static int credit8 = 0; // a purchase


and this:

code:
        TextField taCredit1 = new TextField ();
        TextField taCredit2 = new TextField ();
        TextField taCredit3 = new TextField ();
        TextField taCredit4 = new TextField ();
        TextField taCredit5 = new TextField ();
        TextField taCredit6 = new TextField ();
        TextField taCredit7 = new TextField ();
        TextField taCredit8 = new TextField ();


Makes a pretty good case for using an array.

The fact that all of these variables are static... what do you think that means when more than one instance of the class CreditLimit (window) is instantiated? A hint: it isn't pretty. Wink

Also... I would get get rid of the "X" button, and let the window's close button do its job.

You've made a lot of common mistakes, but the fact that you're trying is good. Now, make it better. Smile
the_short1




PostPosted: Mon Apr 24, 2006 10:45 am   Post subject: ..

Thanks for the suggestions wtd, thanks to you, I took it upon myself to learn how to do arrays of objects, so in the game that im currently creating, i used an array of buttons and indeed it makes code neater Smile

Also i used a grid layout to make it easier, although for that credit limit assignment, i chose to make my own layout on purpose, i like having full control over the placement of my widgets.

"I would suggest you create ActionListeners for each widget that needs one" .. how would one go about doing that? our teacher is busy helping the gr11s so us gr12s have to learn GUI by ourselves :S

Swing/AWT both have their strongpoints, ill use swing if i cant do it with awt, but i prefer awt over swing for my own reasons Wink

yea .. i didnt know how to use WindowListener at the time, but i do now.. and smacking myself in the forehead for using an noobish close button.. lol

-kevin
wtd




PostPosted: Mon Apr 24, 2006 11:26 am   Post subject: (No subject)

Let's say you have a button in a window...

Java:

code:
import javax.swing.*;
import java.awt.event.*;

class MyWindow extends JFrame {
   private JButton sayHelloButton;

   public MyWindow() {
      super("MyWindow");

      sayHelloButton = new JButton("Say Hello");
      sayHelloButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Hello!");
         }
      });

      add(sayHelloButton);

      setSize(200, 200);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

public class Test {
   public static void main(String[] args) {
      MyWindow w = new MyWindow();
   }
}


Scala (just because I like Scala):

code:
import javax.swing._
import java.awt.event._
import javax.swing.JFrame.EXIT_ON_CLOSE

class MyWindow extends JFrame("MyWindow") {
   val sayHelloButton = new JButton("Say Hello") with ActionListener {
      addActionListener(this)

      def actionPerformed(e : ActionEvent) =
         Console println "Hello!"
   }

   add(sayHelloButton)

   setSize(200, 200)
   setVisible(true)
   setDefaultCloseOperation(EXIT_ON_CLOSE)
}

object Test extends Application {
   new MyWindow
}
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: