package task_two; 21. 22.public class Test { 23. 24. /** 25. * @param args 26. */ 27. public static void main(String[] args) { 28. // TODO Auto-generated method stub 29. new FrameHaveDialog(); 30. } 31. 32.}
Mypanel类:
01.package task_two; 02.import java.awt.*; 03.import java.awt.event.*; 04.import javax.swing.*; 05.public class Mypanel extends Panel { 06. 07. JButton button1,button2; 08. public Mypanel(){ 09. button1 = new JButton("Yes"); 10. button2 = new JButton("Cancle"); 11. add(button1); 12. add(button2); 13. } 14.}
FrameHaveDialog类
01.package task_two; 02. 03.import java.awt.FlowLayout; 04.import java.awt.Font; 05.import java.awt.event.ActionEvent; 06.import java.awt.event.ActionListener; 07.import java.awt.event.FocusListener; 08. 09.import javax.swing.*; 10. 11.//该窗口有一个按钮和一个文本区,当单击该按钮时,弹出对话框FontDialog. 12.//然后根据用户在对话框下拉列表中选择的为显示文本区中的文本。最后编写一个程序执行入口进行测试。 13.public class FrameHaveDialog extends JFrame implements ActionListener{ 14. JTextArea text1; 15. JButton button; 16. FontDialog dialog; 17. public FrameHaveDialog(){ 18. text1 = new JTextArea(6,15); 19. button = new JButton("打开对话框"); 20. add(text1); 21. add(button); 22. button.addActionListener(this); 23. dialog = new FontDialog(this,"修改字体对话框",true); 24. setLayout(new FlowLayout()); 25. setBounds(200, 200, 200, 200); 26. setVisible(true); 27. } 28. 29. public void actionPerformed(ActionEvent e) { 30. //int index = dialog.list.getSelectedIndex(); 31. //text1.setFont(new Font((String) dialog.list.getItemAt(index),Font.PLAIN,12)); 32. dialog.setVisible(true); 33. if(dialog.panel.button1.hasFocus()==true) 34. { 35. 36. int index = dialog.list.getSelectedIndex(); 37. text1.setFont(new Font((String) dialog.list.getItemAt(index),Font.PLAIN,dialog.list.getFont().getSize())); 38. } 39. 40. } 41.}
FontFamily类:
01.package task_two; 02. 03.import java.awt.GraphicsEnvironment; 04. 05.//编写一个FontFamily类,该类对象获取当前机器可用的全部字体名称。 06.public class FontFamily { 07. public String[] getfont(){ 08. GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); 09. String[] fontName = e.getAvailableFontFamilyNames(); 10. return fontName; 11. } 12.}
FontDialog类:
01.package task_two; 02. 03.import java.awt.BorderLayout; 04.import java.awt.Font; 05.import java.awt.event.ActionEvent; 06.import java.awt.event.ActionListener; 07.import java.awt.event.ItemEvent; 08.import java.awt.event.ItemListener; 09. 10.import javax.swing.*; 11. 12.//编写一个对话框FontDialog,该对话框是模式对话框,采用BorderLayout布局. 13.//包含一个JComboBox放在北面显示全部字体的名称,包含一个JLabel放在中间,显示字体的效果,包含两个按钮放在南面. 14.//点击YES,在对话框所依赖的窗口中设置字体的效果,点击Cancle取消。 15.public class FontDialog extends JDialog implements ItemListener, ActionListener{ 16. JComboBox list; 17. JLabel lable; 18. Mypanel panel; 19. public FontDialog(JFrame f, String s, boolean b) { 20. super(f, s, b); 21. panel = new Mypanel(); 22. list = new JComboBox(); 23. lable = new JLabel("字体的效果"); 24. FontFamily fontFamily = new FontFamily(); 25. String[] s1 = fontFamily.getfont(); 26. for (int i = 0; i < s1.length; i++) { 27. list.addItem(s1[i]); 28. } 29. 30. add(list,BorderLayout.NORTH); 31. add(lable,BorderLayout.CENTER); 32. add(panel,BorderLayout.SOUTH); 33. list.addItemListener(this); 34. panel.button1.addActionListener(this); 35. panel.button2.addActionListener(this); 36. setBounds(380, 380, 380, 380); 37. } 38. 39. 40. public void itemStateChanged(ItemEvent e) { 41. // TODO Auto-generated method stub 42. if(e.getSource()==list) 43. { 44. String name = (String)list.getSelectedItem(); 45. //UIManager.put("Label.font", new Font(name,Font.BOLD,12)); 46. //System.out.print(list.getItemAt(6)); 47. lable.setFont(new Font(name,Font.PLAIN,list.getFont().getSize())); 48. } 49. 50. } 51. public void actionPerformed(ActionEvent e) { 52. if(e.getSource()==panel.button1) 53. { 54. //int i = list.getSelectedIndex(); 55. //FrameHaveDialog frameHaveDialog = new FrameHaveDialog(); 56. //int index = list.getSelectedIndex(); 57. //lable.setFont(new Font((String) list.getItemAt(index),Font.PLAIN,list.getFont().getSize())); 58. //System.out.print(0); 59. setVisible(false); 60. //System.exit(0); 61. } 62. else if(e.getSource()==panel.button2) 63. { 64. //System.out.print(1); 65. setVisible(false); 66. //System.exit(0); 67. } 68. 69. } 70. 71. 72. 73.}