thorax232

Java Backgrounds, Foregrounds, Borders, Tool Tip Texts

Jul 20th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. /*
  2.  * 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,
  3.  * 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
  4.  * 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.
  5.  */
  6. import java.awt.*;
  7. import javax.swing.*;
  8. import javax.swing.border.*;
  9.  
  10. public class SwingCommonFeatures extends JFrame
  11. {
  12.     // Main method
  13.     public static void main(String[] args)
  14.     {
  15.         SwingCommonFeatures frame = new SwingCommonFeatures();
  16.         frame.setTitle("Exercise 12_08");
  17.         frame.pack();
  18.         frame.setLocationRelativeTo(null);
  19.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  20.         frame.setVisible(true);
  21.     }
  22.    
  23.     // Constructor
  24.     public SwingCommonFeatures()
  25.     {
  26.         setLayout(new GridLayout(3, 3));
  27.    
  28.         // Create labels
  29.         JLabel black = new JLabel("black");
  30.         JLabel blue = new JLabel("blue");
  31.         JLabel cyan = new JLabel("cyan");
  32.         JLabel green = new JLabel("green");
  33.         JLabel magenta = new JLabel("magenta");
  34.         JLabel orange = new JLabel("orange");
  35.        
  36.         // Set background colors
  37.         black.setBackground(Color.WHITE);
  38.         blue.setBackground(Color.WHITE);
  39.         cyan.setBackground(Color.WHITE);
  40.         green.setBackground(Color.WHITE);
  41.         magenta.setBackground(Color.WHITE);
  42.         orange.setBackground(Color.WHITE);
  43.        
  44.         // Set background colors
  45.         black.setForeground(Color.BLACK);
  46.         blue.setForeground(Color.BLUE);
  47.         cyan.setForeground(Color.CYAN);
  48.         green.setForeground(Color.GREEN);
  49.         magenta.setForeground(Color.MAGENTA);
  50.         orange.setForeground(Color.ORANGE);
  51.        
  52.         // Create and set borders
  53.         LineBorder b = new LineBorder(Color.YELLOW);
  54.         black.setBorder(b);
  55.         blue.setBorder(b);
  56.         cyan.setBorder(b);
  57.         green.setBorder(b);
  58.         magenta.setBorder(b);
  59.         orange.setBorder(b);
  60.        
  61.         // Create and set fonts
  62.         Font f = new Font("TimesRoman", Font.BOLD, 20);
  63.         black.setFont(f);
  64.         blue.setFont(f);
  65.         cyan.setFont(f);
  66.         green.setFont(f);
  67.         magenta.setFont(f);
  68.         orange.setFont(f);
  69.        
  70.         // Set tool tip text
  71.         black.setToolTipText("Black");
  72.         blue.setToolTipText("Blue");
  73.         cyan.setToolTipText("Cyan");
  74.         green.setToolTipText("Green");
  75.         magenta.setToolTipText("Magenta");
  76.         orange.setToolTipText("Orange");
  77.        
  78.         // Add labels
  79.         add(black); add(blue); add(cyan);
  80.         add(green); add(magenta); add(orange);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment