Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,232 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,281 people online right now. Registration is fast and FREE... Join Now!




Gribbag layout for java GUI development

 
Reply to this topicStart new topic

Gribbag layout for java GUI development, how to use the Gribbag layout to a java based GUI for calculating simp

kalonji
14 Oct, 2008 - 09:08 AM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 2

CODE

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

    public class Calc_simpleInterest extends JFrame
   {
      private static final int WIDTH = 400;
      private static final int HEIGHT = 200;
    
      private JLabel principalamountL, investmenttimeL, interestrateL,simpleinterestL;
      private JTextField principalamountTF, investmenttimeTF, interestrateTF,simpleinterestTF;
      private JButton calculateB, resetB, exitB;
        
       private CalculateButtonHandler cbHandler;
      private ResetButtonHandler rbHandler;
      private ExitButtonHandler ebHandler;
        
         public Calc_simpleInterest()
      {
         principalamountL = new JLabel("Principal Amount: ", SwingConstants.LEFT);
         investmenttimeL = new JLabel("Investment Time(in years): ", SwingConstants.LEFT);
         interestrateL = new JLabel("Interest Rate(%): ", SwingConstants.LEFT);
         simpleinterestL = new JLabel("Simple interest",SwingConstants.LEFT);
            
         principalamountTF = new JTextField(10);
         investmenttimeTF = new JTextField(10);
         interestrateTF = new JTextField(10);
            simpleinterestTF = new JTextField(10);
        
        //SPecify handlers for each button and add (register) ActionListeners to each button.
         calculateB = new JButton("Calculate Interest");
         cbHandler = new CalculateButtonHandler();
         calculateB.addActionListener(cbHandler);
         resetB = new JButton("Reset(Clear)");
         rbHandler = new ResetButtonHandler();
         resetB.addActionListener(rbHandler);
         exitB = new JButton("Close Program");
         ebHandler = new ExitButtonHandler();
         exitB.addActionListener(ebHandler);
        
         setTitle("Calculation of Simple Interest");
         Container pane = getContentPane();
         pane.setLayout(new GridLayout(6,2));
        
        //Add things to the pane in the order you want them to appear (left to right, top to bottom)
         pane.add(principalamountL);
            pane.add(principalamountTF);
            
            pane.add(investmenttimeL);
         pane.add(investmenttimeTF);
                
         pane.add(interestrateL);
         pane.add(interestrateTF);
            
            pane.add(simpleinterestL);
            pane.add(simpleinterestTF);

         pane.add(calculateB);
         pane.add(resetB);
         pane.add(exitB);
        
         setSize(WIDTH, HEIGHT);
         setVisible(true);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
    
       private class CalculateButtonHandler implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
            double principalamount;
                double investmenttime;
                double interestrate;
                double simpleInterest;
            
            principalamount = Double.parseDouble(principalamountTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
            investmenttime = Double.parseDouble(investmenttimeTF.getText());
                interestrate = Double.parseDouble(interestrateTF.getText());
           simpleInterest = (principalamount * interestrate * investmenttime) / 100.0;

            
            simpleinterestTF.setText("" + simpleInterest);
         }
      }
  
       public class ResetButtonHandler implements ActionListener
      
      {
          public void actionPerformed(ActionEvent e)
        
         {
        
                     interestrateTF.setText("");
                     investmenttimeTF.setText("");
                     principalamountTF.setText("");
                     simpleinterestTF.setText("");
        
         }
      }
       public class ExitButtonHandler implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
          
            System.exit(0);
        
         }
      }
    
       public static void main(String[] args)
      {
         Calc_simpleInterest simpObj  = new Calc_simpleInterest();
      }
    
   }


*Edited to add the [ code] tags. Please code.gif

This post has been edited by pbl: 14 Oct, 2008 - 08:37 PM
User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Gribbag Layout For Java GUI Development
14 Oct, 2008 - 11:43 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 186



Thanked: 9 times
Dream Kudos: 100
My Contributions
What is your question?

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:25AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month