← Home

Exercises

Congratulations for managing to go so far. As of now, you can consider yourself a fluent speaker of Java and Java swing. You are capable of building complex and useful GUI's. So remember that programming in swing is like playing lego: you create a new piece, add features to it: and then you add it to a bigger piece and so one. One piece of advice if you need to do more complex interfaces. First, do a quick sketch of what you want to achieve, thinking about which layout to use. If ever you have logic errors (the program compiles but the result is not the expected one) then go through your code step by step and be sure you have not forgotten to use the setVisible() method; that your sizes are correct; that you have correctly added the elements etc. At all times you must bear in mind that the computer is a big baby and you have to specify everything!

When programming in Java Swing, here are a few things you want to be careful about:

While this list of tasks may be daunting, bear in mind that all you nede can be found in this chapter and the chapters before it. You can do it. With that in mind, have a go!

Ex 1

Create the following window:

Exercise 3

Tools: You are going to have to use the following method: setBorder(BorderFactory.createEmptyBorder(integer, integer, integer, integer)). You apply it to any JPanel or JFrame and it creates an inner empty border with the parameters (top, left, bottom, right).

Ex 2

Create the following window:

Exercise 4

Ex 3

Create a program that through the command line asks for an input which will then be added to a GUI. E.g.: If I input "Hello there" the following is displayed:

Exercise 5

Hint: you may want to have a quick look at how to use scanner again.

Ex 4

Create the following window:

Exercise 6

Ex 5

Create the following GUI. If the left button is clicked, it should open a new red window and close the main window. If the right button is clicked a blue window should open and the main window should close:

Exercise 7

Ex 6

Create the following window, and if the button is clicked it should create 10 red simple windows at 10 random locations (have fun closing them afterwards):

Exercise 8

Exercise 8

Tools:

Ex 7

Create the following window:

Exercise 9

Hint: you can set the background of a JTextArea just like you would for a JPanel or a JFrame.

Ex 8

Create the following window, and when the button is clicked it should change the question "how old are you?" to a phrase related to the age. E.g. if I write "18" the text is changed to "have you started UCAS?":

Exercise 10

Ex 9

Write a program that checks if a number is prime or not. You might want to create a method that returns a boolean if a number is prime or not:

Exercise 11 Exercise 11

Ex 10

Inspiring yourself from exercise 11 create a program that displays the first 20 prime numbers. You will want to use JTextAreas:

Exercise 12

Exercise 12

Ex 11

Create a program that after asking you to choose between a temperature converter or an angle converter either displays an angle converter (degree/radian) or a temperature converter (the same as Part IV) based on what the user chose.

Ex 12

Create a program that, using scanner, asks you to input positive numbers until you input a negative one, after which it creates a GUI with the following information:

E.g. if you enter these numbers: 1.1, 2.3, 5.6, 7.8, -1 you have this returned to you:

Exercise 15

Ex 13

Create a method calculator just like this one:

Exercise 16 Exercise 16

The two methods here are the square root and cosine.

HINT: For cosine apply the method Math.cos(integer).

Ex 14-17

Throughout these next exercises you will create the following calculator:

Exercises 17-20

  1. Just create the layout and the design of the calculator. Try to keep your code as clear as possible.
  2. Try to make the plus, equal and reset button operational.
  3. Apply the same technique as previously and make all the buttons active.

HINT: Create two labels, one that shows the intermediate results and the final result whereas the other shows what number you have entered. You can then fetch the numbers clicked from the second label. Each operation should have a boolean variable that activates itself once the operation is activated and at the next operation should do the calculations with the new number entered.

You may think that I'm heartless for making you do more, and you are correct. →