0

I wanted to know how I can make the text of my labels bigger and put breaks in between of the labels. So basically for the code I want it to be:
(Big) Ohms Law
(A bit smaller) Voltage (textbox + button here)
(Same size as Voltage) Resistance (textbox + button here)
So far I have this:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class OhmsLawApplet extends Applet implements ActionListener
{
// declare variables
int RESISTANCE;
int VOLTAGE;
int OHMS;
int V = 0;
int R = 0;
int I = 0;
//construct components
Label OhmsLabel = new Label("Ohms Law"); // Label at the top
Label VOLTAGELabel = new Label("Voltage:"); // Label for Voltage 
    TextField VOLTAGEField = new TextField(); //Textfield to input Voltage 
Label RESISTANCELabel = new Label("Resistance:"); // Label for Resistance
    TextField RESISTANCEField = new TextField();  //Textfield to input Resistance
Button CALCULATEButton = new Button("Calculate");

public void init(){
    // Add the components to the Applet
    setForeground(Color.black);
    add(OhmsLabel);
    add(VOLTAGELabel);
    add(VOLTAGEField);
    add(RESISTANCELabel);
    add(RESISTANCEField);
    add(CALCULATEButton);
    CALCULATEButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
// UNFINISHED/ DON'T KNOW HOW TO DO YET
VOLTAGE = Integer.parseInt(VOLTAGEField.getText());
RESISTANCE = Integer.parseInt(RESISTANCEField.getText());
VOLTAGE = I*R;
RESISTANCE = V/I;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CSStudent
  • 3
  • 2
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 27 '16 at 06:44

1 Answers1

0

You can use Component#setFont(Font) to set the font and thus update the textsize:

Label label = ...
int newSize = ...
label.setFont(label.getFont().getName() , label.getFont().getStyle() , newSize);

This piece of code will create a new font with the style-attributes but a different fontsize.

As for changing the distance of the labels: that depends upon the used LayoutManager. You can set the size of each component manually using Component#setPreferredSize(Dimension), which will work for most LayoutManagers. But the distance of the components solely depends upon the used LayoutManager. You might want to have a look at the GridBagLayout