Set background/opaque to all JLabels

2019-07-20 01:27发布

问题:

See my code:

package hsleiden.webcat.exercise12_08;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class newFrame extends JFrame {
    public static void main(String[] args){

    newFrame frame = new newFrame();

    frame.setLayout(new GridLayout(2,3));
    frame.setSize(200, 200);
    frame.setTitle("Opdracht 12.8");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    }

    public newFrame(){
        JLabel label1 = new JLabel("Black", JLabel.CENTER);
        JLabel label2 = new JLabel("Blue", JLabel.CENTER);
        JLabel label3 = new JLabel("Cyan", JLabel.CENTER);
        JLabel label4 = new JLabel("Green", JLabel.CENTER);
        JLabel label5 = new JLabel("Magenta", JLabel.CENTER);
        JLabel label6 = new JLabel("Orange", JLabel.CENTER);

        Border border = BorderFactory.createLineBorder(Color.YELLOW);

        label1.setBackground(Color.WHITE);
        label2.setBackground(Color.WHITE);
        label3.setBackground(Color.WHITE);
        label4.setBackground(Color.WHITE);
        label5.setBackground(Color.WHITE);
        label6.setBackground(Color.WHITE);

        label1.setForeground(Color.BLACK);
        label2.setForeground(Color.BLUE);
        label3.setForeground(Color.CYAN);
        label4.setForeground(Color.GREEN);
        label5.setForeground(Color.MAGENTA);
        label6.setForeground(Color.ORANGE);

        label1.setBorder(border);
        label2.setBorder(border);
        label3.setBorder(border);
        label4.setBorder(border);
        label5.setBorder(border);
        label6.setBorder(border);

        label1.setOpaque(true);
        label2.setOpaque(true);
        label3.setOpaque(true);
        label4.setOpaque(true);
        label5.setOpaque(true);
        label6.setOpaque(true);


        add(label1);
        add(label2);
        add(label3);
        add(label4);
        add(label5);
        add(label6);

    }
}

As you can see its very troublesome to apply background, border, opaque per label.. thats 18 lines for 1 and the same thing. I was wondering if someone could tell me how I can apply all the things (background, opaque, border) to all labels without having to do it per label. Since they are all the same(except foreground).

Hope someone has a solution for me.

Thanks!

回答1:

  1. You can create your own class that extends JLabel and implements Clonable. You can override the clone method and create as many copies of a JLabel as you want.
  2. Spring provides a handy way of doing this with BeanUtils.copyProperties


回答2:

You can create method for creating and adding a new JLabel like next:

private void addNewLbl(String text, Color foreground){
    JLabel lbl = new JLabel(text, JLabel.CENTER);
    lbl.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
    lbl.setBackground(Color.WHITE);
    lbl.setForeground(foreground);
    lbl.setOpaque(true);
    add(lbl);
}

And use it in construction your JFrame: addNewLbl("black",Color.BLACK);



回答3:

Use an array of labels instead and call setBackground for every element in the array referencing a Color array to get the title & colors for corresponding components.