是更好的做法是声明单个对象或将匿名对象循环到ArrayList中? [英] Is it Better practice to Declare individual objects or loop Anonymous objects into ArrayList?

查看:89
本文介绍了是更好的做法是声明单个对象或将匿名对象循环到ArrayList中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过教科书学习Java编程。编程练习要求您:

I am learning programming with Java through a textbook. A programming exercise asks you to:

(Swing常用功能)显示包含六个标签的框架。将标签的背景
设置为白色。将标签的前景分别设置为黑色,蓝色,青色,
绿色,品红色和橙色,如图12.28a所示。将每个标签的边框
设置为黄色的线条边框。将每个标签的字体设置为
Times Roman,粗体和20像素。将每个标签的文本和工具提示文本设置为其前景色的
名称。

(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.

我使用一个数组并使用一个循环用匿名对象填充它(如类Sixlabels扩展JFrame {}所示):

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]);

}
}

书中答案不使用数组列表(如公共Exercise12_8中所示扩展JFrame {});:

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);
     }
}

我的问题是:是否更好的做法是宣布单独的JLabel对象(如本书所示)或者像我一样匿名填充Array(或ArrayList)?我在书中看到的唯一好处是可读性和可变名称(可以在未来的程序中使用,但不能在这个特定的程序中使用)。

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).

推荐答案

我可以为您提供第三个答案。创建一个采用 JLabel 的方法并进行配置。这样你可以使用其他两个中的任何一个并降低你的代码复杂性。

I can provide you a third answer. Create a method that takes a JLabel and configures it. That way you could use either of the other two and reduce your code complexity.

答案是,这取决于。这取决于你想要达到的目标。

The answer is, it depends. It depends on what it is you want to achieve.

如果你做了很多改动,反复对同样严重的组件,比如你在这里,我可能会很想使用简单的 List 或数组。如果元素的数量是动态的,或者只能在类内部访问,则此功能特别有用。

If you are making lots of changes, repeatedly to the same serious of components, such as you are here, I might be tempted to use a simple List or array. This is particularly useful if the number of elements is dynamic and or only accessiable internally to the class.

另一方面,如果要更改或更新状态在外部组件,通常更容易提供对组件的命名访问。是的,你可以提供一个使用 int 参数的方法,但随后你开始遇到问题,总是要验证参数:P

If, on the other hand you want to change or update the state of the components externally, it's normally easier to provide named access to the components. Yes, you could provide a method that uses a int parameter, but then you start running into issues with always having to validate the parameter :P

所以我想,问题是,你需要达到什么目标?

So I guess, the question is, what is it you need to achieve?

这篇关于是更好的做法是声明单个对象或将匿名对象循环到ArrayList中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆