Not a member of Pastebin yet?
Sign Up,
it unlocks many cool 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.
- */
- import java.awt.*;
- import javax.swing.*;
- import javax.swing.border.*;
- public class SwingCommonFeatures extends JFrame
- {
- // Main method
- public static void main(String[] args)
- {
- SwingCommonFeatures frame = new SwingCommonFeatures();
- frame.setTitle("Exercise 12_08");
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- // Constructor
- public SwingCommonFeatures()
- {
- setLayout(new GridLayout(3, 3));
- // Create labels
- JLabel black = new JLabel("black");
- JLabel blue = new JLabel("blue");
- JLabel cyan = new JLabel("cyan");
- JLabel green = new JLabel("green");
- JLabel magenta = new JLabel("magenta");
- JLabel orange = new JLabel("orange");
- // Set background colors
- black.setBackground(Color.WHITE);
- blue.setBackground(Color.WHITE);
- cyan.setBackground(Color.WHITE);
- green.setBackground(Color.WHITE);
- magenta.setBackground(Color.WHITE);
- orange.setBackground(Color.WHITE);
- // Set background colors
- black.setForeground(Color.BLACK);
- blue.setForeground(Color.BLUE);
- cyan.setForeground(Color.CYAN);
- green.setForeground(Color.GREEN);
- magenta.setForeground(Color.MAGENTA);
- orange.setForeground(Color.ORANGE);
- // Create and set borders
- LineBorder b = new LineBorder(Color.YELLOW);
- black.setBorder(b);
- blue.setBorder(b);
- cyan.setBorder(b);
- green.setBorder(b);
- magenta.setBorder(b);
- orange.setBorder(b);
- // Create and set fonts
- Font f = new Font("TimesRoman", Font.BOLD, 20);
- black.setFont(f);
- blue.setFont(f);
- cyan.setFont(f);
- green.setFont(f);
- magenta.setFont(f);
- orange.setFont(f);
- // Set tool tip text
- black.setToolTipText("Black");
- blue.setToolTipText("Blue");
- cyan.setToolTipText("Cyan");
- green.setToolTipText("Green");
- magenta.setToolTipText("Magenta");
- orange.setToolTipText("Orange");
- // Add labels
- add(black); add(blue); add(cyan);
- add(green); add(magenta); add(orange);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment