1 14 package wingset; 15 16 import org.wings.*; 17 18 import java.awt.*; 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 22 26 public class LabelExample extends WingSetPane { 27 private static final SIcon WAIT_ICON = new SResourceIcon("org/wings/icons/Wait.gif"); 28 private static final String directions[] = {"nw", "n", "ne", "w", "e", "sw", "s", "se"}; 29 private static final SIcon onIcons[]; 30 private static final SIcon offIcons[]; 31 private ComponentControls controls; 32 33 static { 34 onIcons = new SIcon[directions.length]; 35 offIcons = new SIcon[directions.length]; 36 for (int i = 0; i < directions.length; ++i) { 37 String d = directions[i]; 38 onIcons[i] = new SURLIcon("../icons/Label" + d + "On.gif"); 39 offIcons[i] = new SURLIcon("../icons/Label" + d + "Off.gif"); 40 } 41 } 42 43 public SComponent createExample() { 44 controls = new ComponentControls(); 45 46 final SLabel testLabel = new SLabel("A red, bold, italic, serif text"); 47 testLabel.setHorizontalAlignment(LEFT); 48 testLabel.setIcon(WAIT_ICON); 49 testLabel.setForeground(Color.RED); 50 testLabel.setFont(new SFont("serif", SFont.ITALIC | SFont.BOLD, 10)); 51 controls.addSizable(testLabel); 52 53 SPanel p = new SPanel(new SGridLayout(3)); 54 p.add(new SLabel("Control the label's text position")); 55 p.add(new SSpacer(100,1)); 56 p.add(new SLabel("Result")); 57 p.add(createRoundRadio(testLabel)); 58 p.add(new SLabel()); 59 p.add(testLabel); 60 61 SForm form = new SForm(new SBorderLayout()); 62 form.add(controls, SBorderLayout.NORTH); 63 form.add(p, SBorderLayout.CENTER); 64 return form; 65 } 66 67 private SRadioButton createRadio(SPanel p, String constraint, 68 SButtonGroup buttonGroup, 69 String toolTip, int icon) { 70 SRadioButton button = new SRadioButton(); 71 button.setIcon(offIcons[icon]); 72 button.setSelectedIcon(onIcons[icon]); 73 button.setToolTipText(toolTip); 74 p.add(button, constraint); 75 buttonGroup.add(button); 76 return button; 77 } 78 79 SContainer createRoundRadio(final SLabel label) { 80 SPanel b = new SPanel(new SBoxLayout(SBoxLayout.Y_AXIS)); 81 82 SPanel sp = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS)); 83 SButtonGroup g = new SButtonGroup(); 84 final SRadioButton nw = createRadio(sp, "p=nw", g, "North West", 0); 85 final SRadioButton n = createRadio(sp, "p=n", g, "North", 1); 86 final SRadioButton ne = createRadio(sp, "p=ne", g, "North East", 2); 87 b.add(sp); 88 89 SPanel sp1 = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS)); 90 91 final SRadioButton w = createRadio(sp1, "p=w", g, "West", 3); 92 SLabel sl = new SLabel(); 93 sl.setIcon(new SURLIcon("../icons/cowSmall.gif")); 94 sp1.add(sl); 95 final SRadioButton e = createRadio(sp1, "p=e", g, "East", 4); 96 b.add(sp1); 97 98 SPanel sp2 = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS)); 99 final SRadioButton sw = createRadio(sp2, "p=sw", g, "South West", 5); 100 final SRadioButton s = createRadio(sp2, "p=s", g, "South", 6); 101 final SRadioButton se = createRadio(sp2, "p=se", g, "South East", 7); 102 103 nw.setShowAsFormComponent(false); 104 n.setShowAsFormComponent(false); 105 ne.setShowAsFormComponent(false); 106 w.setShowAsFormComponent(false); 107 e.setShowAsFormComponent(false); 108 sw.setShowAsFormComponent(false); 109 s.setShowAsFormComponent(false); 110 se.setShowAsFormComponent(false); 111 112 b.add(sp2); 113 114 g.addActionListener(new ActionListener () { 115 public void actionPerformed(ActionEvent ev) { 116 Object button = ((SButtonGroup) ev.getSource()) 117 .getSelection(); 118 if (button == n) { 119 label.setVerticalTextPosition(TOP); 120 label.setHorizontalTextPosition(CENTER); 121 } else if (button == nw) { 122 label.setVerticalTextPosition(TOP); 123 label.setHorizontalTextPosition(LEFT); 124 } else if (button == ne) { 125 label.setVerticalTextPosition(TOP); 126 label.setHorizontalTextPosition(RIGHT); 127 } else if (button == w) { 128 label.setVerticalTextPosition(CENTER); 129 label.setHorizontalTextPosition(LEFT); 130 } else if (button == e) { 131 label.setVerticalTextPosition(CENTER); 132 label.setHorizontalTextPosition(RIGHT); 133 } else if (button == sw) { 134 label.setVerticalTextPosition(BOTTOM); 135 label.setHorizontalTextPosition(LEFT); 136 } else if (button == se) { 137 label.setVerticalTextPosition(BOTTOM); 138 label.setHorizontalTextPosition(RIGHT); 139 } else if (button == s) { 140 label.setVerticalTextPosition(BOTTOM); 141 label.setHorizontalTextPosition(CENTER); 142 } 143 149 150 } 151 }); 152 153 n.setSelected(true); 154 155 return b; 156 } 157 } 158 159 160 | Popular Tags |