views:

360

answers:

4

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);
    }

}
+4  A: 

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.

Vincent Briard
Thanks. I get what you mean. And that's what I'm thinking too. But I may need to explore the IDE that i'm using if I might do that. And it would take time. Is there a code for that? So I could alter it or something and learn?
Leyla
As said in another answer, I would not use NetBeans GUI Builder, it won't help you to understand how Gui are made and how Java manage Gui components. Try some very simple how to about Gui, and the day you'll own in this area, use NetBean GUI Builder if you have really no time for one of your app (or if you are not ashamed to see the automatic generated code)
Vincent Briard
+2  A: 

The GUI editor in NetBeans is actually not bad for getting started quickly creating a GUI for a small application. Knowing only a little about creating GUIs (and then only AWT, not Swing) I made my first Swing application in about ten minutes.

Since you're new to Java I'm guessing you haven't chosen an IDE yet. NetBeans is a good place to start.

Tenner
I am using netbeans and I'm learning it now. But it might take time.:)
Leyla
+2  A: 

Start by reading the Swing Tutorial. There are plenty of example programs to learn from. Then you can ask specific questions if you encounter problems.

camickr
A: 

I don't recommend you to use NetBeans GUI Builder, it generates a lot of unnecessary code. Here is some example that I wrote to help you start with Swing. This is simple example of one JFrame creation with two JButtons and one JTextField. You may be also interested in MVC pattern, you can read more about that specific topic here (http://pclc.pace.edu/~bergin/mvc/mvcgui.html) Also if you want to show results maybe you should try with JTextPane control, but that's just my opinion

public class MainWindowClient implements ActionListener {

    JFrame frame;
    JTextField jtxInput;

    JButton btnConnect;
    JButton btnDisconnect;

    public MainWindowClient() {
     SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       init();
      }
     }); 
    }

    public void init() {
     try {
      UIManager
        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
     } 
     catch (ClassNotFoundException e) {}
     catch (InstantiationException e) {} 
     catch (IllegalAccessException e) {}
     catch (UnsupportedLookAndFeelException e) {}

     frame = new JFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setLayout(new BorderLayout());
     frame.setTitle("Client");
     frame.setSize(800, 600);

     final JPanel title = new JPanel(new FlowLayout(FlowLayout.LEFT));
     title.setBackground(new Color(255, 255, 255));
     final JLabel lblAppName = new JLabel("Client Application");
     lblAppName.setFont(new Font("sansserif", Font.PLAIN, 22));
     title.add(lblAppName);
     title.setBorder(BorderFactory.createTitledBorder(""));

     final JPanel panelInputBoard = new JPanel(new GridLayout());
     panelLogArea.setBorder(BorderFactory.createTitledBorder("Input"));
     jtxInput = new JTextField("");

     panelLogArea.add(jtxInput);

     final JPanel panelCommandBoard = new JPanel(new FlowLayout(FlowLayout.LEFT));
     panelCommandBoard.setBorder(BorderFactory.createTitledBorder("Client Commands"));

     btnConnect = new JButton("Connect");
     btnConnect.addActionListener(this);

     btnDisconnect = new JButton("Disconnect");
     btnDisconnect.addActionListener(this);

     panelCommandBoard.add(btnConnect);
     panelCommandBoard.add(btnDisconnect);

     frame.add(title, BorderLayout.NORTH);
     frame.add(panelCommandBoard, BorderLayout.SOUTH);
     frame.add(panelInputBoard, BorderLayout.NORTH);

     frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
     JButton eventSource = (JButton) event.getSource();
     if(eventSource.getText().equals("Connect")) {
      // Do some stuff
     }
     if(eventSource.getText().equals("Disconnect")) {
      // Do some stuff
     }  
    }


    public static void main(String[] args) {
     MainWindowClient m = new MainWindowClient(); 
    }
}
svlada