Lesson5 – JLabel, Numbers & Action

I have not posted recently as I am spending all my available time on my new application but I though I post some new code example for those that like learning by example.

So here we have for today I have example how to.

1) Created a JLabel

final JLabel currentNumber = new JLabel("0", JLabel.CENTER);

2) Set Font size for the JLabel text

currentNumber.setFont(currentNumber.getFont().deriveFont(25.0f));

3) Create a GRID in a JFrame to have multiple rows

frame.setLayout(new GridLayout(6, 1)); // This equals 6 rows and 1 columm

4) Adding JLabel to a GRIDED Frame

frame.add(currentNumber);
frame.add(addNumber);
frame.add(newNumber);
frame.add(equalNumber);
frame.add(TotalNumber);
frame.add(exitButton);

5) Create Integers

int newValue = 1;

6) Create Integer and assign a String value

int oldValue = Integer.valueOf(TotalNumber.getText()).intValue();

7) Assign an Integer value to a String

TotalNumber.setText(String.valueOf(TotalValue));

8) Assign a Integer value by calculating two value

int TotalValue = (oldValue+newValue);

Any questions please post and let me know. I am always happy to help.

Example of the output.

image

To download the complete code for lesson 5 just click here. SimpleGUI-Lesson5