home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / LabelPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  8.8 KB  |  291 lines

  1. /*
  2.  * @(#)LabelPanel.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.text.*;
  17.  
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.util.*;
  21.  
  22.  
  23. /**
  24.  * Labels!
  25.  *
  26.  * @version 1.1 11/14/97
  27.  * @author Jeff Dinkins
  28.  */
  29. public class LabelPanel extends JPanel 
  30. {
  31.     SwingSet swing;
  32.  
  33.     public LabelPanel(SwingSet swing) {
  34.     this.swing = swing;
  35.  
  36.     setBorder(swing.emptyBorder5);
  37.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  38.     setOpaque(false);
  39.  
  40.     JLabel label;
  41.  
  42.     JPanel labelsPanel = SwingSet.createVerticalPanel(true);
  43.     labelsPanel.setOpaque(false);
  44.  
  45.     JPanel row1 = SwingSet.createHorizontalPanel(false);
  46.     JPanel row2 = SwingSet.createHorizontalPanel(false);
  47.     JPanel row3 = SwingSet.createHorizontalPanel(false);
  48.     JPanel row4 = SwingSet.createHorizontalPanel(false);
  49.     JPanel row5 = SwingSet.createHorizontalPanel(false);
  50.  
  51.         label = new BorderedSwingSetLabel("Label 1");
  52.     label.setToolTipText("Duke says \"Howdy!\"");
  53.         label.setHorizontalAlignment(label.CENTER);
  54.         label.setIcon(swing.dukeWave);
  55.         label.setVerticalTextPosition(label.BOTTOM);
  56.         label.setHorizontalTextPosition(label.CENTER);
  57.         row1.add(label);
  58.         swing.labels.addElement(label);
  59.  
  60.         label = new BorderedSwingSetLabel("Label2");
  61.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  62.         label.setHorizontalAlignment(label.CENTER);
  63.         label.setVerticalTextPosition(label.BOTTOM);
  64.         label.setHorizontalTextPosition(label.CENTER);
  65.         label.setFont(swing.boldFont);
  66.         label.setForeground(Color.red);
  67.         row1.add(label);
  68.         swing.labels.addElement(label);
  69.  
  70.         label = new BorderedSwingSetLabel("Label3");
  71.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  72.         label.setVerticalTextPosition(label.BOTTOM);
  73.         label.setHorizontalAlignment(label.CENTER);
  74.         label.setHorizontalTextPosition(label.CENTER);
  75.         row2.add(label);
  76.         label.setFont(swing.bigFont);
  77.         swing.labels.addElement(label);
  78.  
  79.  
  80.         label = new BorderedSwingSetLabel("Label4");
  81.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  82.         label.setVerticalTextPosition(label.BOTTOM);
  83.         label.setHorizontalAlignment(label.CENTER);
  84.         label.setHorizontalTextPosition(label.CENTER);
  85.         label.setIcon(swing.dukeMagnify);
  86.         row2.add(label);
  87.         label.setFont(swing.bigBoldFont);
  88.         swing.labels.addElement(label);
  89.  
  90.         label = new BorderedSwingSetLabel("Label 5");
  91.     label.setToolTipText("Shhhh.... Duke is taking a little nap.");
  92.         label.setVerticalTextPosition(label.BOTTOM);
  93.         label.setHorizontalAlignment(label.CENTER);
  94.         label.setHorizontalTextPosition(label.CENTER);
  95.         row3.add(label);
  96.         label.setForeground(Color.blue);
  97.         label.setIcon(swing.dukeSnooze);
  98.         label.setFont(swing.bigBoldFont);
  99.         swing.labels.addElement(label);
  100.  
  101.         label = new BorderedSwingSetLabel("Label 6");
  102.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  103.         label.setVerticalTextPosition(label.BOTTOM);
  104.         label.setHorizontalAlignment(label.CENTER);
  105.         label.setHorizontalTextPosition(label.CENTER);
  106.         row3.add(label);
  107.         label.setFont(swing.reallyBigBoldFont);
  108.         label.setForeground(Color.green);
  109.         swing.labels.addElement(label);
  110.  
  111.         label = new JLabel("Type Here: ");
  112.         label.setHorizontalTextPosition(label.RIGHT);
  113.     label.setDisplayedMnemonic('T');
  114.     label.setToolTipText("The labelFor and displayedMnemonic properties work!");
  115.     JTextField tf = new JTextField("");
  116.     label.setLabelFor(tf);
  117.     row4.setBorder(swing.emptyBorder15);
  118.         row4.add(label);
  119.         row4.add(tf);
  120.         swing.labels.addElement(label);
  121.  
  122.         label = new JLabel("And Here: ");
  123.         label.setHorizontalTextPosition(label.RIGHT);
  124.     label.setDisplayedMnemonic('r');
  125.     label.setToolTipText("The labelFor and displayedMnemonic properties work!");
  126.     tf = new JTextField("");
  127.     label.setLabelFor(tf);
  128.     row5.setBorder(swing.emptyBorder15);
  129.         row5.add(label);
  130.         row5.add(tf);
  131.         swing.labels.addElement(label);
  132.  
  133.     // Add label panels to labelPanel
  134.     JPanel labelPanel = swing.createVerticalPanel(true);
  135.     labelPanel.setAlignmentX(LEFT_ALIGNMENT);
  136.     labelPanel.setAlignmentY(TOP_ALIGNMENT);
  137.  
  138.     labelPanel.add(row1);
  139.     labelPanel.add(row2);
  140.     labelPanel.add(row3);
  141.     labelPanel.add(row4);
  142.     labelPanel.add(row5);
  143.  
  144.     labelPanel.add(Box.createGlue());
  145.  
  146.  
  147.     // *************** Create the button controls ****************
  148.     JPanel controls = new JPanel() {
  149.         public Dimension getMaximumSize() {
  150.         return new Dimension(300, super.getMaximumSize().height);
  151.         }
  152.     };
  153.     controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
  154.     controls.setAlignmentY(TOP_ALIGNMENT);
  155.     controls.setAlignmentX(LEFT_ALIGNMENT);
  156.  
  157.     JPanel buttonControls = swing.createHorizontalPanel(true);
  158.     buttonControls.setAlignmentY(TOP_ALIGNMENT);
  159.     buttonControls.setAlignmentX(LEFT_ALIGNMENT);
  160.  
  161.     JPanel leftColumn = swing.createVerticalPanel(false);
  162.     leftColumn.setAlignmentX(LEFT_ALIGNMENT);
  163.     leftColumn.setAlignmentY(TOP_ALIGNMENT);
  164.  
  165.     JPanel rightColumn = swing.createVerticalPanel(false);
  166.     rightColumn.setAlignmentX(LEFT_ALIGNMENT);
  167.     rightColumn.setAlignmentY(TOP_ALIGNMENT);
  168.  
  169.     buttonControls.add(leftColumn);
  170.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  171.     buttonControls.add(rightColumn);
  172.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  173.  
  174.     controls.add(buttonControls);
  175.  
  176.     // Display Options
  177.     JLabel l = new JLabel("Display Options:");
  178.     leftColumn.add(l);
  179.     l.setFont(swing.boldFont);
  180.  
  181.     JCheckBox bordered = new JCheckBox("Paint Border");
  182.     bordered.setEnabled(false);
  183.         bordered.setMnemonic('b');
  184.      bordered.addItemListener(swing.buttonDisplayListener);
  185.      leftColumn.add(bordered);
  186.  
  187.      JCheckBox focused = new JCheckBox("Paint Focus");
  188.     focused.setEnabled(false);
  189.         focused.setMnemonic('f');
  190.      focused.addItemListener(swing.buttonDisplayListener);
  191.      leftColumn.add(focused);
  192.  
  193.     JCheckBox enabled = new JCheckBox("Enabled");
  194.     enabled.setToolTipText("Click here to enable or disable all labels.");
  195.     enabled.setSelected(true);
  196.         enabled.setMnemonic('e');
  197.     enabled.addItemListener(swing.buttonDisplayListener);
  198.     leftColumn.add(enabled);
  199.  
  200.  
  201.     leftColumn.add(Box.createRigidArea(swing.vpad20));
  202.  
  203.     l = new JLabel("Pad Amount:");
  204.     l.setEnabled(false);
  205.     leftColumn.add(l);
  206.     l.setFont(swing.boldFont);
  207.     
  208.     ButtonGroup group = new ButtonGroup();
  209.     JRadioButton defaultPad = new JRadioButton("Default");
  210.     defaultPad.setEnabled(false);
  211.         defaultPad.setMnemonic('d');
  212.     group.add(defaultPad);
  213.     defaultPad.setSelected(true);
  214.      defaultPad.addItemListener(swing.buttonPadListener);
  215.     leftColumn.add(defaultPad);
  216.  
  217.     JRadioButton zeroPad = new JRadioButton("0");
  218.         zeroPad.setMnemonic('0');
  219.     zeroPad.setEnabled(false);
  220.     group.add(zeroPad);
  221.      zeroPad.addItemListener(swing.buttonPadListener);
  222.     leftColumn.add(zeroPad);
  223.  
  224.     JRadioButton tenPad = new JRadioButton("10");
  225.         tenPad.setMnemonic('1');
  226.     tenPad.setEnabled(false);
  227.     group.add(tenPad);
  228.      tenPad.addItemListener(swing.buttonPadListener);
  229.     leftColumn.add(tenPad);
  230.     
  231.     // *************** Create the layout controls ****************
  232.     // Create Text Position Layout control
  233.     JPanel textPosition = DirectionButton.createDirectionPanel(true, "S", swing.textPositionListener);
  234.     JPanel labelAlignment = DirectionButton.createDirectionPanel(true, "C", swing.labelAlignmentListener);
  235.  
  236.     l = new JLabel("Text Position:");
  237.     rightColumn.add(l);
  238.     l.setFont(swing.boldFont);
  239.      rightColumn.add(textPosition);
  240.  
  241.      rightColumn.add(Box.createRigidArea(swing.vpad20));
  242.  
  243.     l = new JLabel("Content Alignment:");
  244.     rightColumn.add(l);
  245.     l.setFont(swing.boldFont);
  246.      rightColumn.add(labelAlignment);
  247.  
  248.      rightColumn.add(Box.createGlue());
  249.  
  250.     add(labelPanel);
  251.     add(Box.createRigidArea(swing.hpad10));
  252.      add(controls);
  253.     }
  254.  
  255.  
  256.     class BorderedSwingSetLabel extends JLabel {
  257.  
  258.     BorderedSwingSetLabel(String text) {
  259.         super(text);
  260.     }
  261.  
  262.     public Insets getInsets() {
  263.         Insets insets = super.getInsets();
  264.         insets.left += 3;
  265.         insets.right += 3;
  266.         insets.top += 3;
  267.         insets.bottom += 3;
  268.         return insets;
  269.     }
  270.  
  271.     public float getAlignX() {
  272.         return LEFT_ALIGNMENT;
  273.     }
  274.  
  275.     public Dimension getPreferredSize() {
  276.         return new Dimension(145, 90);
  277.     }
  278.  
  279.     public Dimension getMaximumSize() {
  280.         return new Dimension(250, 160);
  281.     }
  282.  
  283.     public void paint(Graphics g) {
  284.         super.paint(g);
  285.         g.setColor(Color.black);
  286.         g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
  287.     }
  288.     }
  289.     
  290. }
  291.