CS代考 Programming IT, Exercise book 5,

Programming IT, Exercise book 5,
Java Swing 1 – Basic Layout
Dr Peter Inglis and Dr Kevin Bryson v1.0 October 2019
In this example we are attempting to recreate the illustration of a Border layout given in the lecture slides. We will gradually build up to a complete solution which will look like this:

Copyright By PowCoder代写 加微信 powcoder

Note that you do not need to add your JLabels or JPanels as instance variables of the class as they have no event functionality. They can just be added after the usual JFrame information like setSize etc. Remember that if you are not using a Mac your windows will look a bit different to mine. Also note that the pictures in this document are smaller than they will appear on your screen (they have been reduced to save space).
1. Copy the file JustShowFrame.java from the lecture slides into new class Ex1 (so now your file will contain the declaration:
public class Ex1 extends JFrame {
Set the size of the JFrame to be 400×400, give it the title ¡°Border layout for JFrame¡± and add set the default close operation. Add JLabels containing the strings ¡°North¡±, ¡°South¡±, ¡°East¡± and ¡°West¡± directly to the North, South, East and West regions of the JFrame BorderLayout and run your example. It should look like this (not very nice!):

2. Now create a new class Ex2 which is similar to the above, but this time create JPanels: topPanel, rightPanel, bottomPanel and leftPanel, and add the JLabels to the panels before adding the JPanels to the JFrame. It should like this (not much nicer!):
3. Notice that in the output from part 2, the text in the North and South panels looks ok as it is centered horizontally. But the text in the East, Center and West panels needs

to be centered vertically as well. You can do this by changing the layout manager of those panels to GridBagLayout like so:
leftPanel.setLayout(new GridBagLayout());
You will be prompted by eclipse to import the GridBagLayout class.
Create a new class Ex3 which is similar to Ex2, but change the layout of the East, Center and West panels. It should now look like this:
4. In the image shown at the start of this question, there are borders around the different regions. In order to do this we first have to define a border type using the following command (note that we haven¡¯t covered this in the lectures):
Border blackline = BorderFactory.createLineBorder(Color.black);
You will be prompted by Eclipse to import additional classes javax.swing.BorderFactory, javax.swing.border.Border and java.awt.Color.
You can then add a border to each of your panels like so:
leftPanel.setBorder(blackline);
Create a new class Ex4 which is similar to Ex3, but with borders around the panels. It should look like this:

In this next step we are going to change the font of the labels. Again, this is not something that we have covered in the lectures so far. To do this, add a new command:
Font f = new Font(“TimesRoman”, Font.PLAIN, 24);
to the constructor of your JFrame class – I suggest putting it underneath the usual JFrame information. Again you will be prompted to import a new class, namely java.awt.Font. Then change the font of each of your labels like so:
leftLabel.setFont(f);
Create a new class Ex5 which is similar to Ex4, but in which all of your labels have the new font. It should look like this:

6. This part is not for the faint-hearted and can be omitted. It will produce the picture shown at the introduction to this exercise.
Now we are going to make the text in the East and West panels proceed vertically rather than horizontally. We can do very fancy things with the text in JLabels by using a language called html. Html stands for ¡°Hyper text markup language¡± and is used for documents designed to be displayed in a web browser. See the wikipedia page for more details.
Learning the intricacies of html would he a whole course in itself, so we will just tell you the code required to add some vertical text to a JLabel:
JLabel leftLabel = new JLabel(“ W
E
S
T “);
Or, to include spaces around the letters (which looks nicer):
JLabel leftLabel = new JLabel(“ &nbsp W &nbsp
&nbsp E ” + “&nbsp
&nbsp S &nbsp
&nbsp T &nbsp “);
Create a new class Ex6 which is similar to Ex5, but in which the text of your labels in the East and West regions proceeds vertically.

7. This part is just a simple refactoring of your code. Create two new classes, BorderPanel.java and Ex7.java. An outline for your class Ex7.java is as follows:
import javax.swing.JFrame;
public class Ex7 extends JFrame {
public Ex7() {
// add title, size and close information
BorderPanel myPanel = new BorderPanel(); this.add(myPanel);
// add main method similar to that for Ex1 – Ex6
Your BorderPanel class should extend JPanel, and should contain all of the code required from Ex6.java so that Ex7.java creates the same window as Ex6.java.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com