Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add GUI to this java program?

I know only basic stuff in java. And I need to create a GUI for this type of program. It shows your credit card info. It has some other classes and makes use of the rmiregistry. This works fine in console but I need to show it in a GUI. The first thing that promps here is to enter your name (java Shopper localhost my name). Then it shows you your credit card info. Can anyone help me? Please and thank you

import java.rmi.*;
import javax.swing.*;

public class Shopper {
    public static void main(String args[])
    {
        CreditManager cm = null;
        CreditCard account = null;

        if(args.length<2)
        {
            System.err.println("Usage:");
            System.err.println("java Shopper <server> <accountname>");
            System.exit(1);
        }
        try
        {
            String url = new String("//"+args[0]+"/cardManager");
            System.out.println("Shopper: lookup cardManager, url="+url);
            cm = (CreditManager) Naming.lookup(url);
        }catch(Exception e)
        {
            System.out.println("Error in getting Card Manager "+e);
            System.exit(1);
        }

        try
        {
            account = cm.findCreditAccount(args[1]);
            System.out.println("Found account for "+args[1]);
        }catch(Exception e)
        {
            System.out.println("Error in getting acocunt for "+args[1]);
            System.exit(1);
        }

        try
        {

             System.out.println("Available credit is "+account.getCreditLine());
            System.out.println("Changing pin number for account");
            account.setSignature(1234);
            System.out.println("Buying a new watch for $100");
            account.makePurchase(100.0f, 1234);
            System.out.println("Available credit is now "+account.getCreditLine());
            System.out.println("Buying a new pair of shoes for $160");
            account.makePurchase(160.0f, 1234);
            System.out.println("Cardholder: Paying off $136 of balance");
            account.payTowardsBalance(136.0f);
            System.out.println("Available credit is now "+account.getCreditLine());

        }catch(Exception e)
        {
            System.out.println("Transaction error for "+args[1]);
        }

        System.exit(0);
    }

}

1 Answers

First things first, have a quick look at Awt/Swing in the Javadoc

According to what you need to do, you can add a gui very quickly in a first time using a JFrame and some TextArea (The text area will be your "console output"), this is the quickest way to have something visual out of your console.

After maybe you'll use some input for the account name in a popup window (See PopupFactory).

You can in a first time have a quick look at the various gui sample on sun website to understand how it works before designing a more complete one for your application.

like image 188
Vincent B. Avatar answered May 26 '26 04:05

Vincent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!