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!