我正在通过教科书学习 Java 编程。编程练习要求您:
(Swing 常用功能)显示一个包含六个标签的框架。将标签的背景设置为白色。将标签的前景分别设置为黑色、蓝色、青色、绿色、洋红色和橙色,如图 12.28a 所示。将每个标签的边框设置为黄色的线边框。将每个标签的字体设置为 Times Roman、粗体和 20 像素。将每个标签的文本和工具提示文本设置为其前景色的名称。
对于这个问题,我有两个答案。我的答案和书籍的答案。两个答案都很好。
我使用一个数组并通过循环使用匿名对象填充它(如 Sixlabels 类扩展 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]);
}
}
并且书上的答案没有使用数组列表(如公开的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);
}
}
我的问题是:更好的做法是单独声明 JLabel 对象(如本书所言)还是像我一样匿名填充 Array(或 ArrayList)?我看到按照本书的方式进行操作的唯一好处是可读性和具有变量名(可能会在未来的程序中使用,但不会在这个特定的程序中使用)。