Guys need help. i'm having problem in setting the font-size of my jtfText.

every time i set the font-size of jtfText all the panels and buttons is also getting bigger but not their font-size just the buttons.

This is my code

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

public class ScientificCalculator extends JFrame implements ActionListener
{

	char o;
	int ctr=0;
	String value="", cv="", oBtn;
	Double answer, v1, v2;
	Double NumberConverted;
	
	//Components
	private JButton jbtMemoryClear, jbtMemoryRecall, jbtMemorySave,
					jbtMemoryAdd, jbtMemorySubtract,
					jbtNum9, jbtNum8, jbtNum7, jbtNum6, jbtNum5, jbtNum4,
					jbtNum3, jbtNum2, jbtNum1, jbtNum0, jbtDecimal, jbtPosNeg,
					jbtBackSpace, jbtClear, jbtClearEntry,
					jbtDivide, jbtSquareRoot, jbtPercent, jbtMultiply, jbtSubtract, 
					jbtReciprocal, jbtAdd, jbtEqual,
					jbtPi, jbtCloseOpenParenthesis1, jbtCloseOpenParenthesis2, jbtModulo,
					jbtInteger, jbtAnd, jbtOr, jbtXor, jbtExp, jbtCubeRoot, jbtNot,
					jbtA, jbtB, jbtC, jbtD, jbtE, jbtF;
	
	private JMenuBar mb;
	private JMenu v, e, hl;
	private JMenuItem stn, c, p, vh, ab, q;
	private JRadioButton h, d, ot, bn;

	//Container
	private JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12;
	private JFrame f;
	private JTextField jtfTxt;
	private double TEMP;
	private double SolveTEMP;
	String display = "";
	
	
	
	public ScientificCalculator()
	{	
		//Textfields
		Font font1 = new Font("Arial", Font.BOLD, 30);
		jtfTxt = new JTextField("0",15);
		jtfTxt.setFont(font1);
		jtfTxt.setHorizontalAlignment(JTextField.RIGHT);
		jtfTxt.setEnabled(false);
		jtfTxt.setForeground(Color.gray);
		
		//Number buttons
		jbtNum1 = new JButton("1");
		jbtNum1.addActionListener(this);
		jbtNum2 = new JButton("2");
		jbtNum2.addActionListener(this);
		jbtNum3 = new JButton("3");
		jbtNum3.addActionListener(this);
		jbtNum4 = new JButton("4");
		jbtNum4.addActionListener(this);
		jbtNum5 = new JButton("5");
		jbtNum5.addActionListener(this);
		jbtNum6 = new JButton("6");
		jbtNum6.addActionListener(this);
		jbtNum7 = new JButton("7");
		jbtNum7.addActionListener(this);
		jbtNum8 = new JButton("8");
		jbtNum8.addActionListener(this);
		jbtNum9 = new JButton("9");
		jbtNum9.addActionListener(this);
		jbtNum0 = new JButton("0");
		jbtNum0.addActionListener(this);
			
		//Add, Subtract, Multiply, Division, Percent, SquareRoot, Fraction, Dot and PositiveNegative Buttons
		jbtSubtract = new JButton("-");
		jbtSubtract.addActionListener(this);
		jbtSubtract.setForeground(Color.gray);
		jbtAdd = new JButton("+");
		jbtAdd.addActionListener(this);
		jbtAdd.setForeground(Color.gray);
		jbtMultiply = new JButton("*");
		jbtMultiply.addActionListener(this);
		jbtMultiply.setForeground(Color.gray);
		jbtDivide = new JButton("/");
		jbtDivide.addActionListener(this);
		jbtDivide.setForeground(Color.gray);
		jbtPercent = new JButton("%");
		jbtPercent.addActionListener(this);
		jbtPercent.setForeground(Color.gray);
		jbtSquareRoot = new JButton("sqrt");
		jbtSquareRoot.addActionListener(this);
		jbtSquareRoot.setForeground(Color.gray);
		jbtReciprocal = new JButton("1/x");
		jbtReciprocal.setForeground(Color.gray);
		jbtDecimal = new JButton(".");
		jbtDecimal.addActionListener(this);
		jbtPosNeg = new JButton("+/-");
		jbtPosNeg.addActionListener(this);
		jbtPosNeg.setForeground(Color.gray);
		
		//Clear, ClearEntry, Equal buttons
		jbtClear = new JButton("C");
		jbtClear.addActionListener(this);
		jbtClear.setBackground(Color.red);
		jbtClear.setForeground(Color.white);
		jbtClearEntry = new JButton("CE");
		jbtClearEntry.addActionListener(this);
		jbtClearEntry.setBackground(Color.red);
		jbtClearEntry.setForeground(Color.white);
		jbtBackSpace = new JButton("backspace");
		jbtBackSpace.addActionListener(this);
		jbtBackSpace.setBackground(Color.red);
		jbtBackSpace.setForeground(Color.white);
		jbtEqual = new JButton("=");
		jbtEqual.addActionListener(this);
		
		//MemoryClear, MemoryRecall, MemorySave, MemoryAdd, MemorySubtract buttons
		jbtMemoryClear = new JButton("MC");
		jbtMemoryClear.addActionListener(this);
		jbtMemoryClear.setBackground(Color.blue);
		jbtMemoryClear.setForeground(Color.white);
		jbtMemoryRecall = new JButton("MR");
		jbtMemoryRecall.addActionListener(this);
		jbtMemoryRecall.setBackground(Color.blue);
		jbtMemoryRecall.setForeground(Color.white);
		jbtMemorySave = new JButton("MS");
		jbtMemorySave.addActionListener(this);
		jbtMemorySave.setBackground(Color.blue);
		jbtMemorySave.setForeground(Color.white);
		jbtMemoryAdd = new JButton("M+");
		jbtMemoryAdd.addActionListener(this);
		jbtMemoryAdd.setBackground(Color.blue);
		jbtMemoryAdd.setForeground(Color.white);
		jbtMemorySubtract = new JButton("M-");
		jbtMemorySubtract.addActionListener(this);
		jbtMemorySubtract.setBackground(Color.blue);
		jbtMemorySubtract.setForeground(Color.white);
		
		//Scientific
		jbtPi = new JButton("pi");
		jbtPi.setForeground(Color.gray);
		jbtCloseOpenParenthesis1 = new JButton("(");
		jbtCloseOpenParenthesis1.setForeground(Color.gray);
		jbtCloseOpenParenthesis2 = new JButton(")");
		jbtCloseOpenParenthesis2.setForeground(Color.gray);
		jbtModulo = new JButton("mod");
		jbtModulo.setForeground(Color.gray);
		jbtInteger = new JButton("int");
		jbtInteger.setForeground(Color.gray);
		jbtAnd = new JButton("and");
		jbtAnd.setForeground(Color.gray);
		jbtOr = new JButton("or");
		jbtOr.setForeground(Color.gray);
		jbtXor = new JButton("xor");
		jbtXor.setForeground(Color.gray);
		jbtExp = new JButton("exp");
		jbtExp.setForeground(Color.gray);
		jbtCubeRoot = new JButton("cbrt");
		jbtCubeRoot.setForeground(Color.gray);
		jbtNot = new JButton("not");
		jbtNot.setForeground(Color.gray);
		
		//JRadioButton
		h = new JRadioButton("Hex");
		h = new JRadioButton("Hex" , false);
		h.setForeground(Color.orange);
		d = new JRadioButton("Dec");
		d = new JRadioButton("Dec" , false);
		d.setForeground(Color.orange);
		ot = new JRadioButton("Oct");
		ot = new JRadioButton("Oct" , false);
		ot.setForeground(Color.orange);
		bn = new JRadioButton("Bin");
		bn = new JRadioButton("Bin" , false);
		bn.setForeground(Color.orange);
			
		//ButtonGroup for JRadioButtons
		ButtonGroup bntgroup= new ButtonGroup();
		bntgroup.add(h);
		bntgroup.add(d);
		bntgroup.add(ot);
		bntgroup.add(bn);
		
		//A, B, C, D, E, and F
		jbtA = new JButton("A");
		jbtA.setBackground(Color.yellow);
		jbtB = new JButton("B");
		jbtB.setBackground(Color.yellow);
		jbtC = new JButton("C");
		jbtC.setBackground(Color.yellow);
		jbtD = new JButton("D");
		jbtD.setBackground(Color.yellow);
		jbtE = new JButton("E");
		jbtE.setBackground(Color.yellow);
		jbtF = new JButton("F");
		jbtF.setBackground(Color.yellow);
		
		//JMenuBar
		 mb = new JMenuBar();
	
		//JMenu
		v = new JMenu("View");
		e = new JMenu("Edit");
		hl = new JMenu("Help");
		
		//JMenuItem
		stn = new JMenuItem("Standard Calculator");
		stn.addActionListener(this);
		c = new JMenuItem("Copy");
		c.addActionListener(this);
		p = new JMenuItem("Paste");
		p.addActionListener(this);
		q = new JMenuItem("Quit");
		q.addActionListener(this);
		vh = new JMenuItem("View Help");
		ab = new JMenuItem("About Calculator");
		
			mb.add(v);
			v.add(stn);
			mb.add(e);
			e.add(c);
			e.add(p);
			e.add(q);
			mb.add(hl);
			hl.add(vh);
			hl.add(ab);
		
		//Containers
		f = new JFrame("Nikki's Calculator");
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		p5 = new JPanel();
		p6 = new JPanel();
		p7 = new JPanel();
		p8 = new JPanel();
		p9 = new JPanel();
		p10 = new JPanel();
		p11 = new JPanel();
		p12 = new JPanel();
	}
		
		
		public void launchFrame()
		{
			//Place buttons, textbox and others to the panels
			
			p1.add(h);
			p1.add(d);
			p1.add(ot);
			p1.add(bn);
			
			p2.add(jbtMemoryClear);
			p2.add(jbtMemoryRecall);
			p2.add(jbtMemorySave);
			p2.add(jbtMemoryAdd);
			p2.add(jbtMemorySubtract);
			
			p3.add(jbtInteger);
			p3.add(jbtPi);
			p3.add(jbtModulo);
			p3.add(jbtExp);
			p3.add(jbtCubeRoot);
			
			p4.add(jbtOr);
			p4.add(jbtXor);
			p4.add(jbtNot);
			p4.add(jbtAnd);
			p4.add(jbtSquareRoot);
			
			p5.add(jbtBackSpace);
			p5.add(jbtClearEntry);
			p5.add(jbtClear);
			
			p6.add(jbtNum7);
			p6.add(jbtNum8);
			p6.add(jbtNum9);
			p6.add(jbtDivide);
			p6.add(jbtReciprocal);
			
			p7.add(jbtNum4);
			p7.add(jbtNum5);
			p7.add(jbtNum6);
			p7.add(jbtMultiply);
			p7.add(jbtPercent);
			
			p8.add(jbtNum1);
			p8.add(jbtNum2);
			p8.add(jbtNum3);
			p8.add(jbtSubtract);
			p8.add(jbtAdd);
			
			p9.add(jbtNum0);
			p9.add(jbtDecimal);
			p9.add(jbtPosNeg);
			p9.add(jbtEqual);
			
			p10.add(jbtA);
			p10.add(jbtB);
			p10.add(jbtC);
			p10.add(jbtD);
			p10.add(jbtE);
			p10.add(jbtF);
			
			
			p1.setLayout(new GridLayout (1, 4, 2, 2) );
			p2.setLayout(new GridLayout (1, 5, 2, 2) );
			p3.setLayout(new GridLayout (1, 5, 2, 2) );
			p4.setLayout(new GridLayout (1, 5, 2, 2) );
			p5.setLayout(new GridLayout (1, 3, 2, 2) );
			p11.setLayout(new GridLayout (1,2));
			p6.setLayout(new GridLayout (1, 5, 2, 2) );
			p12.setLayout(new GridLayout (1,2));
			p7.setLayout(new GridLayout (1, 5, 2, 2) );
			p8.setLayout(new GridLayout (1, 5, 2, 2) );
			p9.setLayout(new GridLayout (1, 5, 2, 2) );
			p10.setLayout(new GridLayout (1, 6, 2, 2) );
			
			//Setting the layout
			f.setLayout(new GridLayout (13, 1, 2, 2) );
			f.add(jtfTxt);
			f.setJMenuBar(mb);
			f.setResizable(false);
			f.setBackground(Color.black);
			f.add(p1);
			f.add(p2);
			f.add(p3);
			f.add(p4);
			f.add(p12);
			f.add(p5);
			f.add(p11);
			f.add(p6);
			f.add(p7);
			f.add(p8);
			f.add(p9);
			f.add(p10);
			f.pack();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setVisible(true);

			}
			
	public void actionPerformed(ActionEvent a)
	{


		try
		{
			if(a.getSource()==jbtNum0)
			{
				value+=0;
				jtfTxt.setText(value);
			}
			if(a.getSource()==jbtNum1)
			{
				value+=1;
				jtfTxt.setText(value);
			}
			if(a.getSource()==jbtNum2)
			{
				value+=2;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum3)
			{
				value+=3;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum4)
			{
				value+=4;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum5)
			{
				value+=5;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum6)
			{
				value+=6;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum7)
			{
				value+=7;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum8)
			{
				value+=8;
				jtfTxt.setText(value);
			}
			if(a.getSource()== jbtNum9)
			{
				value+=9;
				jtfTxt.setText(value);
			}


			if (a.getSource() == jbtAdd)
			{
				v1 = Double.parseDouble( jtfTxt.getText() );
				ctr=0;
				o = '+';
				value="";
				jtfTxt.setText("" +value);
			}

			if (a.getSource() == jbtSubtract)
			{
					v1 = Double.parseDouble( jtfTxt.getText() );
					ctr=0;
					o = '-';
					value="";
					jtfTxt.setText("" +value);
			}

			if (a.getSource() == jbtMultiply)
			{
					v1 = Double.parseDouble( jtfTxt.getText() );
					ctr=0;
					o = '*';
					value="";
					jtfTxt.setText("" +value);
			}

			if (a.getSource() == jbtDivide)
			{
					v1 = Double.parseDouble( jtfTxt.getText() );
					ctr=0;
					o = '/';
					value="";
					jtfTxt.setText("" +value);
			}


			//Equal

			if(a.getSource()== jbtEqual)
			{
						value="";
						v2 = Double.parseDouble(jtfTxt.getText());

					if(o=='+')
					{
						ctr=0;
						answer = v1 + v2;
						jtfTxt.setText("" +answer);
						value=""; v1=null; v2=null;
					}
					else if(o=='-')
					{
						ctr=0;
						answer = v1 - v2;
						jtfTxt.setText("" +answer);
						value=""; v1=null; v2=null;
					}
					else if(o=='*')
					{
						ctr=0;
						answer = v1 * v2;
						jtfTxt.setText("" +answer);
						value=""; v1=null; v2=null;
					}
					else if(o=='/')
					{
						ctr=0;
						answer = v1 / v2;
						jtfTxt.setText("" +answer);
						value=""; v1=null; v2=null;
					}
					else if(o=='%')
					{
						ctr=0;
						answer = v1 % v2;
						jtfTxt.setText("" +answer);
						value=""; v1=null; v2=null;
					}
					else{}
			}

			//Clear
			if(a.getSource()== jbtClear)
			{
				ctr=0;
				v1=null;
				v2=null;
				value="";
				answer=0.;
				jtfTxt.setText("0");

			}

			if(a.getSource()== jbtClearEntry)
			{
				ctr=0;
				value="";
				jtfTxt.setText("0");
			}


			//Decimal
			if(a.getSource() == jbtDecimal)
			{
				if(ctr==0){
						value+=".";
						ctr+=1;
						jtfTxt.setText("" +value);
				}
				else{
					System.out.print("");
				}

			}

			
			//BackSpace
			if(a.getSource() == jbtBackSpace)
			{
					value = value.substring(0, value.length()-1 );
					jtfTxt.setText("" +value);
			}



			//SquareRoot
			if(a.getSource() == jbtSquareRoot)
			{
				ctr=0;
				value = "";
				v1 = Math.sqrt( Double.parseDouble( jtfTxt.getText() ) );
				jtfTxt.setText("" +v1);
			}
			
			//Integer
			if(a.getSource() == jbtInteger)
			{
				ctr=0;
				NumberConverted = ( Double.parseDouble(jtfTxt.getText()) * -1 );
				value = "";
				jtfTxt.setText("" +NumberConverted);
			}

			//Reciprocal
			if(a.getSource() == jbtReciprocal)
			{
				ctr=0;
				value = "";
				Double NumberContainer = ( 1 / Double.parseDouble(
				jtfTxt.getText() ) );
				jtfTxt.setText("" +NumberContainer);
			}

			//CubeRoot
			
			
			//Pi
			
			
			//And
			
			
			//Modulo
			
			
			//Exponent
			
			
			//Percent
			
			
			//Xor
			
			
			//Not
			
			
			//MemoryClear
			
			
			//MemoryRecall
			
			
			//MemorySave
			
			
			//MemoryAdd
			
			
			//MemorySubract
			
			
			// MenuBar
			if(a.getSource() == c)
			{
				cv = jtfTxt.getText();
			}

			if(a.getSource() == p)
			{
				jtfTxt.setText("" +cv);
			}

			if(a.getSource() == q)
			{
				System.exit(0);
			}
			if(a.getSource() == stn)
			{
				StandardCalculator sd = new StandardCalculator();
				sd.launchFrame();
				f.setVisible(false);
			}

		}	
		
		// End of Try
		catch(StringIndexOutOfBoundsException siobe)
		{
		}
		catch(NumberFormatException nfe)
		{
		}
		catch(NullPointerException npe)
		{
		}
		
	
	}
		public static void main(String args[])
		{
			ScientificCalculator sc = new ScientificCalculator();
			sc.launchFrame();
		}	
}

put this line in for your text

new JButton ("<html><CENTER><font size=\"3\">+/-</font></CENTER></html>")

set the size of the button with the

JButton.setPreferredSize (new Dimension (sizeX,sizeY)); // these are in px

I needed to use these for my calculator (lol)

Edited 5 Years Ago by sirlink99: n/a

No, i mean, i only want to change the font-size of the entered text in the jtfText.

Edited 5 Years Ago by Jake.20: n/a

JTextField disp = new JTextField (19);disp.setText ("0"); disp.setEditable (false);
disp.setFont (new Font ("Arial", 10,10));

Here is a website as well
.

Edited 5 Years Ago by sirlink99: n/a

The problem is that the GridLayout sizes each grid cell the same, so when you increased the height of your textfield by changing the font size, you increased the height of all rows in the grid.

To get around that you could use a BorderLayout of two panels, one with your text field at the top and another for all of your other buttons in the center.

That would allow you to separate the sizing of the two sections. Take a glance at this change to your layout code

//Setting the layout
JPanel upper = new JPanel();
upper.setLayout(new GridLayout(1,1));
upper.add(jtfTxt);

JPanel lower = new JPanel();
lower.setLayout(new GridLayout (13, 1, 2, 2) );
lower.add(p1);
lower.add(p2);
lower.add(p3);
lower.add(p4);
lower.add(p12);
lower.add(p5);
lower.add(p11);
lower.add(p6);
lower.add(p7);
lower.add(p8);
lower.add(p9);
lower.add(p10);

f.setLayout(new BorderLayout());
f.add(upper, BorderLayout.NORTH);
f.add(lower,BorderLayout.CENTER);
f.setJMenuBar(mb);
f.setResizable(false);
f.setBackground(Color.black);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Comments
good suggestion +1
This article has been dead for over six months. Start a new discussion instead.