Question regarding jLabel command

Java question - change font type

Page 1 of 1

1 Replies - 1590 Views - Last Post: 01 October 2006 - 08:38 PM Rate Topic: -----

#1 Turtle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-October 06

Question regarding jLabel command

Post icon  Posted 01 October 2006 - 04:58 PM

Once the Calculator button is selected, how can I change the font to Bold, 14?

My source is attached (just started to learn Java, BTW!)

/*
 First Java Pgm: GUI Loan Calculator
 Rookie Programmer: J. Tandarich
 Date: September 30, 2006
 Filename: gui.java
 Purpose: This program will use a GUI for the user to input mortgage amount and APR -
  ava program using a graphical user interface that calculates
  and displays the mortgage payment amount from user input of
  the amount of the mortgage, the term of the mortgage, and the
  interest rate of the mortgage.  Allow the user to loop back
  and enter new data or quit.
*/



import java.awt.*;
import java.io.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.*;
import java.awt.Font;
import java.lang.*;

	public class gui extends JApplet implements ActionListener


{

		//Create Frame regions.
		  JPanel jp = new JPanel();
		  JFrame frame;
		  JLabel lLoan,lAmt,lLen;
		  JTextField tLoan,tAmt;
		  JButton bSubmit,bReset,bExit;
		  JComboBox duration;
		  Container container;
		  public void init()
	  {
	  initComponent();

  }

	private void initComponent()
  {

	 //Create a new JFrane container.
		frame = new JFrame("John's EZ Loan Company");
		container = frame.getContentPane();

	//Create the layout
		GridBagLayout gbl = new GridBagLayout();


	//Set layout on container and Handle buttons
		container.setLayout(gbl);

	//Place a component at cell location (1,1)
		GridBagConstraints gbc = new GridBagConstraints();

		String[] lenloan = { "7 Years", "15 Years", "30 Years"};

		lLoan= new JLabel("Enter loan Amount");
		lAmt =  new JLabel("A Mortgage Payment in the amount of ");  //output in red

		lLen= new JLabel("Length of loan ");
		tLoan= new JTextField(10);
		tAmt =  new  JTextField(14);
		tAmt = new Font( "TimesRoman",Font.BOLD,12);
		tAmt.disable();
/*  testing these fields
				final JTextField textfield = new JTextField( "this is a TEST" );
				textfield.setBackground( Color.BLACK );
				textfield.setForeground( Color.YELLOW );
				textfield.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
				textfield.setHorizontalAlignment( JTextField.RIGHT );
				textfield.setEnabled( true );
				textfield.setEditable( true );
*/
		duration = new JComboBox(lenloan);
		duration.setSelectedIndex(0);
		bSubmit= new JButton("Calculate Payment");
		bReset= new JButton("Reset");
		bExit= new JButton("Exit");
		gbc.gridx = 0;
		gbc.gridy = 0;
		frame.add(lLoan,gbc);


		gbc.gridx = 1;
		gbc.gridy = 0;
		frame.add(tLoan,gbc);
		gbc.gridx = 0;
		gbc.gridy = 1;
		frame.add(lLen,gbc);
		gbc.gridx = 1;
		gbc.gridy = 1;
		frame.add(duration,gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		frame.add(lAmt,gbc);
		gbc.gridx = 1;
		gbc.gridy = 2;
		frame.add(tAmt,gbc);
		gbc.gridx = 0;
		gbc.gridy = 3;
		frame.add(bSubmit,gbc);
		bSubmit.addActionListener(this);

		gbc.gridx = 1;
		gbc.gridy = 3;
		frame.add(bReset,gbc);
		bReset.addActionListener(this);
		gbc.gridx = 0;
		gbc.gridy = 4;
		frame.add(bExit,gbc);
		bExit.addActionListener(this);

	//Show the frame
		frame.pack();
//		frame.setDefaultcloseoperatin( Frame.EXIT_ON_CLOSE );
		frame.setVisible(true);

  }

  public void actionPerformed(ActionEvent e)
	{

		if (e.getSource()==bSubmit)
		  {
			  System.out.println(duration.getSelectedIndex());
			  loancalc();
		  }

		  else  if (e.getSource()==bReset)
		  {
			  tLoan.setText("");
			  duration.setSelectedIndex(0);
			  tAmt.setText("");
		  }
		  else  if (e.getSource()==bExit)
			  System.exit(1);
	}

  public  void loancalc()
  {
	  DecimalFormat twodigits  = new DecimalFormat("$#,###.00");



	  //print heading (previous class; will not display in applet, nor do I want it to!)
			System.out.println ("\tIndividual  Assignment 5.2");
			System.out.println ("\t  John's EZ Loan Company ");
			System.out.println ();


	  //Array for length of loan (Term)
		  double loan = Double.parseDouble(tLoan.getText());
		  double interest=.0535;
		  double InterestRate=5.35;
		  double lengthTerm=7;
		  if(duration.getSelectedIndex()==0)
		  {
			  interest=.0535;
			  InterestRate=5.35;
			  lengthTerm=7;
		  }
		  else if(duration.getSelectedIndex()==1)
		  {
			  interest=.0550;
			  InterestRate=5.50;
			  lengthTerm=15;
		  }
		  else if(duration.getSelectedIndex()==2)
		  {
			  interest=.0575;
			  InterestRate=5.75;
			  lengthTerm=30;
		  }
		  double payment = (loan*(interest/12))/(1-(Math.pow(1/(1+(interest/12)),(lengthTerm*12))));
		  String ab=""+twodigits.format(payment);
		  tAmt.setText(ab);



  }

}

This post has been edited by Dark_Nexus: 01 October 2006 - 05:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding jLabel command

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Question regarding jLabel command

Posted 01 October 2006 - 08:38 PM

Use the setFont method available in the JComponent class that you inherited.

JComponent setFont API.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1