I am learning programming with Java through a textbook. A programming exercise asks you to:
(Swing common features) Display a frame that contains six labels. Set the background of the labels to white. Set the foreground of the labels to black, blue, cyan, green, magenta, and orange, respectively, as shown in Figure 12.28a. Set the border of each label to a line border with the color yellow. Set the font of each label to Times Roman, bold, and 20 pixels. Set the text and tool tip text of each label to the name of its foreground color.
I have two answers to the problem. My answer and the books answer. Both answers work fine.
I use an Array and populate it with anonymous objects by using a loop (as shown in class Sixlabels extends JFrame{}):
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TWELVE_point_8 {
public static void main(String[] args) {
JFrame frame = new SixLabels();
frame.setTitle("Six Labels");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SixLabels extends JFrame {
public SixLabels() {
setLayout(new GridLayout(2, 3));
JLabel[] list = {
new JLabel("Black"),
new JLabel("Blue"),
new JLabel("Cyan"),
new JLabel("Green"),
new JLabel("Magenta"),
new JLabel("Orange")};
// set foreground colors
list[0].setForeground(Color.black);
list[1].setForeground(Color.blue);
list[2].setForeground(Color.cyan);
list[3].setForeground(Color.green);
list[4].setForeground(Color.magenta);
list[5].setForeground(Color.orange);
// set background colors
for (int i = 0; i < list.length; i++)
list[i].setBackground(Color.white);
// set fonts
Font font = new Font("TimesRoman", Font.BOLD, 20);
for (int i = 0; i < list.length; i++)
list[i].setFont(font);
// set borders
Border lineBorder = new LineBorder(Color.yellow, 1);
for (int i = 0; i < list.length; i++)
list[i].setBorder(lineBorder);
// set tooltip
for (int i = 0; i < list.length; i++)
list[i].setToolTipText(list[i].getText());
// add all labels to container
for (int i = 0; i < list.length; i++)
add(list[i]);
}
}
And the book answer does not use an array list (as shown in public Exercise12_8 extends JFrame{});:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Exercise12_8 extends JFrame {
private JLabel jlblBlack = new JLabel("black");
private JLabel jlblBlue = new JLabel("blue");
private JLabel jlblCyan = new JLabel("cyan");
private JLabel jlblGreen = new JLabel("green");
private JLabel jlblMagenta = new JLabel("magenta");
private JLabel jlblOrange = new JLabel("orange");
public Exercise12_8() {
setLayout(new GridLayout(2, 3));
this.add(jlblBlack);
this.add(jlblBlue);
this.add(jlblCyan);
this.add(jlblGreen);
this.add(jlblMagenta);
this.add(jlblOrange);
jlblBlack.setBackground(Color.WHITE);
jlblBlue.setBackground(Color.WHITE);
jlblCyan.setBackground(Color.WHITE);
jlblGreen.setBackground(Color.WHITE);
jlblMagenta.setBackground(Color.WHITE);
jlblOrange.setBackground(Color.WHITE);
jlblBlack.setForeground(Color.BLACK);
jlblBlue.setForeground(Color.BLUE);
jlblCyan.setForeground(Color.CYAN);
jlblGreen.setForeground(Color.GREEN);
jlblMagenta.setForeground(Color.MAGENTA);
jlblOrange.setForeground(Color.ORANGE);
Font font = new Font("TImesRoman", Font.BOLD, 20);
jlblBlack.setFont(font);
jlblBlue.setFont(font);
jlblCyan.setFont(font);
jlblGreen.setFont(font);
jlblMagenta.setFont(font);
jlblOrange.setFont(font);
Border border = new LineBorder(Color.YELLOW);
jlblBlack.setBorder(border);
jlblBlue.setBorder(border);
jlblCyan.setBorder(border);
jlblGreen.setBorder(border);
jlblMagenta.setBorder(border);
jlblOrange.setBorder(border);
jlblBlack.setToolTipText("black");
jlblBlue.setToolTipText("blue");
jlblCyan.setToolTipText("cyan");
jlblGreen.setToolTipText("green");
jlblMagenta.setToolTipText("magenta");
jlblOrange.setToolTipText("orange");
}
public static void main(String[] args) {
Exercise12_8 frame = new Exercise12_8();
frame.setTitle("Exercise12_8");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
My question is: is it better practice to declare the JLabel object individually (as the book has) or to populate the Array (or ArrayList) anonymously as I have? The only benefit I see in doing it the book's way is readability and having a variable name (which may be used in future programs, but not in this particular one).