|
Подскажите новичку, есть ли в java FontDialog, или чтото ему подобное....
#33505409
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
|
В общем посидел подумал, и решил написать свой собственный FontDialog.
Сразу прошу умных людей не стебатся над кодом, так как я в Jave новичок.
Я работаю в среде Oracle JDeveloper 10, собственно оттуда и содрал макет FontDialog-а.
Главное результат меня полность устраивает, и может быть комуто пригодится...
Исходный класс
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280.
public class FontDialog extends JDialog
{
private JLabel Label_Name = new JLabel();
private JLabel Label_Size = new JLabel();
private JLabel Label_Sample = new JLabel();
private JComboBox jComboBox1 = new JComboBox();
private JSpinner jSpinner1 = new JSpinner();
private JCheckBox jCheckBox1 = new JCheckBox();
private JCheckBox jCheckBox2 = new JCheckBox();
private JScrollPane jScrollPane2 = new JScrollPane();
private JButton Button_OK = new JButton();
private JButton Button_Cancel = new JButton();
public FontDialog()
{
this ( null , "", false);
}
/**
*
* @param modal
* @param title
* @param parent
*/
public FontDialog(Frame parent, String title, boolean modal)
{
super (parent, title, modal);
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private JLabel Label_Text = new JLabel();
private void jbInit() throws Exception
{
this .setSize( new Dimension( 560 , 230 ));
Dimension frameSize = this .getSize();
int xLocation = (screenSize.width - frameSize.width)/ 2 ;
int yLocation = (screenSize.height - frameSize.height)/ 2 ;
this .setLocation(xLocation, yLocation);
this .getContentPane().setLayout( null );
this .setTitle("Font Dialog");
this .setModal(true);
this .addComponentListener( new java.awt.event.ComponentAdapter()
{
public void componentShown(ComponentEvent e)
{
this_componentShown(e);
}
});
Label_Name.setText("Name");
Label_Name.setBounds( new Rectangle( 20 , 25 , 34 , 14 ));
Label_Name.setPreferredSize( new Dimension( 30 , 14 ));
Label_Name.setMinimumSize( new Dimension( 30 , 14 ));
Label_Name.setMaximumSize( new Dimension( 40 , 14 ));
Label_Name.setSize( new Dimension( 50 , 14 ));
Label_Size.setText("Size");
Label_Size.setBounds( new Rectangle( 20 , 55 , 50 , 14 ));
Label_Size.setPreferredSize( new Dimension( 30 , 14 ));
Label_Size.setMinimumSize( new Dimension( 30 , 14 ));
Label_Size.setMaximumSize( new Dimension( 40 , 14 ));
Label_Size.setSize( new Dimension( 50 , 14 ));
Label_Sample.setText("Sample");
Label_Sample.setBounds( new Rectangle( 20 , 95 , 50 , 14 ));
Label_Sample.setPreferredSize( new Dimension( 30 , 14 ));
Label_Sample.setMinimumSize( new Dimension( 30 , 14 ));
Label_Sample.setMaximumSize( new Dimension( 40 , 14 ));
Label_Sample.setSize( new Dimension( 50 , 14 ));
jComboBox1.setBounds( new Rectangle( 70 , 20 , 460 , 20 ));
jCheckBox1.setText("Bold");
jCheckBox1.setBounds( new Rectangle( 115 , 50 , 50 , 20 ));
jCheckBox2.setText("Italic");
jCheckBox2.setBounds( new Rectangle( 165 , 50 , 70 , 20 ));
jScrollPane2.setBounds( new Rectangle( 70 , 80 , 460 , 45 ));
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
jScrollPane2.getViewport().add(Label_Text, null );
this .getContentPane().add(Button_Cancel, null );
this .getContentPane().add(Button_OK, null );
this .getContentPane().add(jSpinner1, null );
this .getContentPane().add(jScrollPane2, null );
this .getContentPane().add(jCheckBox2, null );
this .getContentPane().add(jCheckBox1, null );
this .getContentPane().add(jComboBox1, null );
this .getContentPane().add(Label_Sample, null );
this .getContentPane().add(Label_Size, null );
this .getContentPane().add(Label_Name, null );
jComboBox1.addKeyListener( new java.awt.event.KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
jComboBox1_keyPressed(e);
}
});
jCheckBox2.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jCheckBox2_actionPerformed(e);
}
});
jCheckBox1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jCheckBox1_actionPerformed(e);
}
});
jComboBox1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jComboBox1_actionPerformed(e);
}
});
Label_Text.setText("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ");
LoadFontName();
LoadFontSize();
SpinnerNumberModel model = new SpinnerNumberModel( 11 , 6 , 32 , 1 );
Button_Cancel.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button_Cancel_actionPerformed(e);
}
});
Button_OK.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button_OK_actionPerformed(e);
}
});
Button_Cancel.setBounds( new Rectangle( 460 , 160 , 73 , 23 ));
Button_Cancel.setText("Cancel");
Button_OK.setBounds( new Rectangle( 365 , 160 , 73 , 23 ));
Button_OK.setText("OK");
jSpinner1.setModel(model);
jSpinner1.setBounds( new Rectangle( 70 , 50 , 35 , 20 ));
jSpinner1.addChangeListener( new ChangeListener()
{
public void stateChanged(ChangeEvent evt)
{
FontSize = ( new Integer(jSpinner1.getValue().toString())).intValue();
TestFont();
}
});
}
private Font DefaultFont;
private String FontName;
private int FontStyle;
private int FontSize;
public void GetCurFont(Component comp)
{
DefaultFont = comp.getFont();
}
private void LoadFontName()
{
String [] listModel =
{ "Arial",
"Courier",
"Dialog",
"MS sanserif",
"Times New Roman",
"Tahoma",
"Verdana"};
MyComboBoxModel model = new MyComboBoxModel(listModel);
jComboBox1.setModel(model);
}
private void LoadFontSize()
{
jSpinner1.getModel().setValue( new Integer(FontSize));
}
private void SetDefaultFont()
{
FontName = DefaultFont.getFamily();
FontStyle = DefaultFont.getStyle();
FontSize = DefaultFont.getSize();
System.out.println("DefaultFont : "+FontName+" "+FontStyle+ " "+FontSize);
int pos = 0 ;
for ( int i = 0 ; i < jComboBox1.getItemCount(); i++)
{
if (jComboBox1.getItemAt(i).toString().equalsIgnoreCase(FontName)) pos = i;
}
jComboBox1.setSelectedIndex(pos);
}
private void TestFont()
{
System.out.println("New Font = "+FontName+" "+FontStyle+ " "+FontSize);
Label_Text.setFont( new Font (FontName, FontStyle, FontSize));
DefaultFont = new Font (FontName, FontStyle, FontSize);
}
public Font GetNewFont()
{
return DefaultFont;
}
private void this_componentShown(ComponentEvent e)
{
SetDefaultFont();
}
private void jComboBox1_actionPerformed(ActionEvent e)
{
if (jComboBox1.isPopupVisible())
{
FontName = jComboBox1.getSelectedItem().toString();
TestFont();
}
}
private void jCheckBox1_actionPerformed(ActionEvent e)
{
if (!jCheckBox1.isSelected() & !jCheckBox2.isSelected())
{FontStyle = 0 ;}
else if (jCheckBox1.isSelected() & !jCheckBox2.isSelected())
{FontStyle = 1 ;}
else if (!jCheckBox1.isSelected() & jCheckBox2.isSelected())
{FontStyle = 2 ;}
else if (jCheckBox1.isSelected() & jCheckBox2.isSelected())
{FontStyle = 3 ;}
else {}
TestFont();
}
private void jCheckBox2_actionPerformed(ActionEvent e)
{
if (!jCheckBox1.isSelected() & !jCheckBox2.isSelected())
{FontStyle = 0 ;}
else if (jCheckBox1.isSelected() & !jCheckBox2.isSelected())
{FontStyle = 1 ;}
else if (!jCheckBox1.isSelected() & jCheckBox2.isSelected())
{FontStyle = 2 ;}
else if (jCheckBox1.isSelected() & jCheckBox2.isSelected())
{FontStyle = 3 ;}
else {}
TestFont();
}
private void jComboBox1_keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER || key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN)
{
FontName = jComboBox1.getSelectedItem().toString();
TestFont();
}
}
private void Button_OK_actionPerformed(ActionEvent e)
{
setVisible(false);
}
private void Button_Cancel_actionPerformed(ActionEvent e)
{
DefaultFont = null ;
setVisible(false);
}
}
Навсяк случай класс MyComboBoxModel :
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27.
public class MyComboBoxModel extends DefaultComboBoxModel
{
/** * The TreeSet which holds the combobox's data (ordered no duplicates)
*/
private TreeSet values = null ;
public MyComboBoxModel (List items)
{
super ();
this .values = new TreeSet();
int i, c;
for (i = 0 , c = items.size(); i < c; i++)
{
values.add(items.get(i).toString());
}
Iterator it = values.iterator();
while (it.hasNext())
{
super .addElement(it.next().toString());
}
}
public MyComboBoxModel ( final Object items[])
{
this (Arrays.asList(items));
}
}
Ну и само место использования....
1. 2. 3. 4. 5. 6.
FontDialog dialog = new FontDialog( null , "", true);
dialog.GetCurFont( this );
dialog.setVisible(true);
System.out.println("New Font = "+dialog.GetNewFont());
jLabel1.setFont(dialog.GetNewFont());
dialog.dispose();
А далее кому как угодно.....
|
|
|