首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

100个Java经典例证

2012-10-08 
100个Java经典例子package?test11????import?java.awt.event.ActionEvent??import?java.awt.event.Actio

100个Java经典例子

  1. package?test11;????
  2. import?java.awt.event.ActionEvent;??import?java.awt.event.ActionListener;??
  3. import?java.awt.event.KeyEvent;????
  4. import?javax.swing.AbstractButton;??import?javax.swing.ImageIcon;??
  5. import?javax.swing.JButton;??import?javax.swing.JFrame;??
  6. import?javax.swing.JPanel;????
  7. /**??*?Title:?按钮演示?
  8. ?*?Description:?提供一个按钮的演示。如何实现按钮和是一个按钮失效??*?Filename:??
  9. ?*/??public?class?ButtonDemo?extends?JPanel??
  10. ????????????????????????implements?ActionListener?{????????
  11. ????private?static?final?long?serialVersionUID?=?1L;??????protected?JButton?b1,?b2,?b3;??
  12. /**??*方法说明:构造器,初始图形界面构建?
  13. ?*/??????public?ButtonDemo()?{??
  14. ????????ImageIcon?leftButtonIcon?=?createImageIcon("images/right.gif");??????????ImageIcon?middleButtonIcon?=?createImageIcon("images/middle.gif");??
  15. ????????ImageIcon?rightButtonIcon?=?createImageIcon("images/left.gif");????
  16. ????????b1?=?new?JButton("失效中间按钮(D)",?leftButtonIcon);??????????b1.setVerticalTextPosition(AbstractButton.CENTER);//水平中间对齐??
  17. ????????b1.setHorizontalTextPosition(AbstractButton.LEADING);//相当于LEFT??????????b1.setMnemonic(KeyEvent.VK_D);//将b1邦定alt+D键??
  18. ????????b1.setActionCommand("disable");????
  19. ????????b2?=?new?JButton("M中间按钮",?middleButtonIcon);??????????b2.setVerticalTextPosition(AbstractButton.BOTTOM);??
  20. ????????b2.setHorizontalTextPosition(AbstractButton.CENTER);??????????b2.setMnemonic(KeyEvent.VK_M);//将b2邦定alt+M键??
  21. ??????????b3?=?new?JButton("E激活中间按钮",?rightButtonIcon);??
  22. ????????b3.setMnemonic(KeyEvent.VK_E);//将b3邦定alt+E键??????????b3.setActionCommand("enable");??
  23. ????????b3.setEnabled(false);????
  24. ????????//给1和3添加事件监听??????????b1.addActionListener(this);??
  25. ????????b3.addActionListener(this);??????????//设置按钮提示文本??
  26. ????????b1.setToolTipText("点击这个按钮,将使中间的按钮失效!");??????????b2.setToolTipText("点击这个按钮,没有任何的事件发生!");??
  27. ????????b3.setToolTipText("点击这个按钮,将使中间的按钮有效");????
  28. ????????//将按钮添加到JPanel中??????????add(b1);??
  29. ????????add(b2);??????????add(b3);??
  30. ????}??/**?
  31. ?*方法说明:事件处理??*/??
  32. ????public?void?actionPerformed(ActionEvent?e)?{??????????if?("disable".equals(e.getActionCommand()))?{??
  33. ????????????b2.setEnabled(false);??????????????b1.setEnabled(false);??
  34. ????????????b3.setEnabled(true);??????????}?else?{??
  35. ????????????b2.setEnabled(true);??????????????b1.setEnabled(true);??
  36. ????????????b3.setEnabled(false);??????????}??
  37. ????}??/**?
  38. ?*方法说明:创建图标,??*输入参数:String?path?图标所在的路径?
  39. ?*返回类型:ImageIcon?图标对象??*/??
  40. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?ButtonDemo.class.getResource(path);??
  41. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  42. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  43. ????????????return?null;??????????}??
  44. ????}??/**?
  45. ?*方法说明:主方法??*/??
  46. ????public?static?void?main(String[]?args)?{??????????//设置使用新的swing界面??
  47. ????????JFrame.setDefaultLookAndFeelDecorated(true);????
  48. ????????//创建一个窗体??????????JFrame?frame?=?new?JFrame("ButtonDemo");??
  49. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  50. ????????//创建一个面板??????????ButtonDemo?newContentPane?=?new?ButtonDemo();??
  51. ????????newContentPane.setOpaque(true);???????????frame.setContentPane(newContentPane);??
  52. ??????????//显示窗体??
  53. ????????frame.pack();??????????frame.setVisible(true);??
  54. ????}??}??
view plain
  1. package?test12;????
  2. import?java.awt.*;??import?java.awt.event.*;??
  3. import?javax.swing.*;??/**?
  4. ?*?Title:?检查盒演示??*?Description:?选择不同的选择框显示不同的图片?
  5. ?*?Filename:?CheckBoxDemo.java<??*/??
  6. public?class?CheckBoxDemo?extends?JPanel????????????????????????????implements?ItemListener?{??
  7. ?????????private?static?final?long?serialVersionUID?=?1L;??
  8. ????JCheckBox?chinButton;??????JCheckBox?glassesButton;??
  9. ????JCheckBox?hairButton;??????JCheckBox?teethButton;??
  10. ??????/*?
  11. ?????*?有四个检查盒,分别对应下巴、眼镜、头发和牙齿??????*?图片不是拼出来的,而是根据检查盒选择拼写图片文件名?
  12. ?????*?图片文件名的定义格式为"geek-XXXX.gif"??????*?其中?XXXX?根据检查盒的不同选择,而不同。它的格式如下:?
  13. ????????----?????????????//没有选择?
  14. ????????c---?????????????//一个选择?
  15. ???????-g--????????--h-?
  16. ???????---t??
  17. ???????cg--?????????????//两个选择????????c-h-?
  18. ???????c--t????????-gh-?
  19. ???????-g-t????????--ht?
  20. ????????-ght?????????????//三个选择?
  21. ???????c-ht????????cg-t?
  22. ???????cgh-??
  23. ???????cght?????????????//所有都选??????*/??
  24. ??????StringBuffer?choices;??
  25. ????JLabel?pictureLabel;????
  26. ????public?CheckBoxDemo()?{??????????super(new?BorderLayout());??
  27. ??????????//创建检查盒??
  28. ????????chinButton?=?new?JCheckBox("下巴(c)");??????????chinButton.setMnemonic(KeyEvent.VK_C);??
  29. ????????chinButton.setSelected(true);????
  30. ????????glassesButton?=?new?JCheckBox("眼镜(g)");??????????glassesButton.setMnemonic(KeyEvent.VK_G);??
  31. ????????glassesButton.setSelected(true);????
  32. ????????hairButton?=?new?JCheckBox("头发(h)");??????????hairButton.setMnemonic(KeyEvent.VK_H);??
  33. ????????hairButton.setSelected(true);????
  34. ????????teethButton?=?new?JCheckBox("牙齿(t)");??????????teethButton.setMnemonic(KeyEvent.VK_T);??
  35. ????????teethButton.setSelected(true);????
  36. ????????//给检查盒添加监听??????????chinButton.addItemListener(this);??
  37. ????????glassesButton.addItemListener(this);??????????hairButton.addItemListener(this);??
  38. ????????teethButton.addItemListener(this);????
  39. ????????choices?=?new?StringBuffer("cght");????
  40. ????????//放置一个带图片的标签??????????pictureLabel?=?new?JLabel();??
  41. ????????pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));??????????updatePicture();??
  42. ??????????//将检查盒放置到面版中??
  43. ????????JPanel?checkPanel?=?new?JPanel(new?GridLayout(0,?1));??????????checkPanel.add(chinButton);??
  44. ????????checkPanel.add(glassesButton);??????????checkPanel.add(hairButton);??
  45. ????????checkPanel.add(teethButton);????
  46. ????????add(checkPanel,?BorderLayout.LINE_START);??????????add(pictureLabel,?BorderLayout.CENTER);??
  47. ????????setBorder(BorderFactory.createEmptyBorder(20,20,20,20));??????}??
  48. /**??*<br>方法说明:监听检查盒事件,拼凑图片的文件名XXXX部分?
  49. ?*<br>输入参数:??*<br>返回类型:?
  50. ?*/??????public?void?itemStateChanged(ItemEvent?e)?{??
  51. ????????int?index?=?0;??????????char?c?=?'-';??
  52. ????????Object?source?=?e.getItemSelectable();????
  53. ????????if?(source?==?chinButton)?{??????????????index?=?0;??
  54. ????????????c?=?'c';??????????}?else?if?(source?==?glassesButton)?{??
  55. ????????????index?=?1;??????????????c?=?'g';??
  56. ????????}?else?if?(source?==?hairButton)?{??????????????index?=?2;??
  57. ????????????c?=?'h';??????????}?else?if?(source?==?teethButton)?{??
  58. ????????????index?=?3;??????????????c?=?'t';??
  59. ????????}????????????
  60. ????????//取消选择事件??????????if?(e.getStateChange()?==?ItemEvent.DESELECTED)?{??
  61. ????????????c?=?'-';??????????}??
  62. ??????????//改变文件名字XXXX??
  63. ????????choices.setCharAt(index,?c);????
  64. ????????updatePicture();??????}??
  65. /**??*<br>方法说明:绘制图片?
  66. ?*<br>输入参数:??*<br>返回类型:?
  67. ?*/??????protected?void?updatePicture()?{??
  68. ????????//将得到的图片制成图标??????????ImageIcon?icon?=?createImageIcon(??
  69. ????????????????????????????????????"images/geek/geek-"??????????????????????????????????????+?choices.toString()??
  70. ????????????????????????????????????+?".gif");??????????pictureLabel.setIcon(icon);??
  71. ????????pictureLabel.setToolTipText(choices.toString());??????????if?(icon?==?null)?{??
  72. ????????????pictureLabel.setText("没有发现图片");??????????}?else?{??
  73. ????????????pictureLabel.setText(null);??????????}??
  74. ????}??/**?
  75. ?*<br>方法说明:获取图标??*<br>输入参数:String?path?图片路径?
  76. ?*<br>返回类型:ImageIcon对象??*/??
  77. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?CheckBoxDemo.class.getResource(path);??
  78. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  79. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  80. ????????????return?null;??????????}??
  81. ????}??/**?
  82. ?*<br>方法说明:主方法??*<br>输入参数:?
  83. ?*<br>返回类型:??*/??
  84. ????public?static?void?main(String?s[])?{???????????JFrame.setDefaultLookAndFeelDecorated(true);??
  85. ??????????//创建一个窗体,??
  86. ????????JFrame?frame?=?new?JFrame("CheckBoxDemo");??????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  87. ??????????//创建一个面板??
  88. ????????JComponent?newContentPane?=?new?CheckBoxDemo();??????????newContentPane.setOpaque(true);??
  89. ????????frame.setContentPane(newContentPane);????
  90. ????????//显示窗体??????????frame.pack();??
  91. ????????frame.setVisible(true);??????}??
  92. }??

?

view plain
  1. package?test13;????
  2. import?java.awt.*;??import?java.awt.event.*;??
  3. import?javax.swing.*;??import?java.util.*;??
  4. import?java.text.SimpleDateFormat;??/**?
  5. ?*?Title:?ComboBox下拉域演示</p>??*?Description:?通过选择或这输入一种日期格式来格式化今天的日期?
  6. ?*?Filename:?ComboBoxDemo.java??*/??
  7. ??public?class?ComboBoxDemo?extends?JPanel??
  8. ???????????????????????????implements?ActionListener?{????
  9. ????private?static?final?long?serialVersionUID?=?1L;??????static?JFrame?frame;??
  10. ????JLabel?result;??????String?currentPattern;??
  11. /**??*方法说明:构造器。初始化窗体构件?
  12. ?*/??????public?ComboBoxDemo()?{??
  13. ????????setLayout(new?BoxLayout(this,?BoxLayout.PAGE_AXIS));??????????String[]?patternExamples?=?{??
  14. ?????????????????"dd?MMMMM?yyyy",???????????????????"dd.MM.yy",??
  15. ?????????????????"MM/dd/yy",???????????????????"yyyy.MM.dd?G?'at'?hh:mm:ss?z",??
  16. ?????????????????"EEE,?MMM?d,?''yy",???????????????????"h:mm?a",??
  17. ?????????????????"H:mm:ss:SSS",???????????????????"K:mm?a,z",??
  18. ?????????????????"yyyy.MMMMM.dd?GGG?hh:mm?aaa"???????????????????};??
  19. ??????????currentPattern?=?patternExamples[0];??
  20. ??????????//设置一个规范的用户界面??
  21. ????????JLabel?patternLabel1?=?new?JLabel("输入一个字符格式或者");??????????JLabel?patternLabel2?=?new?JLabel("从下拉列表中选择一种:");??
  22. ??????????JComboBox?patternList?=?new?JComboBox(patternExamples);??
  23. ????????patternList.setEditable(true);//标注这里ComboBox可进行编辑??????????patternList.addActionListener(this);??
  24. ??????????//创建一个显示结果用户界面??
  25. ????????JLabel?resultLabel?=?new?JLabel("当前?日期/时间",??????????????????????????????????????????JLabel.LEADING);//相当于LEFT??
  26. ????????result?=?new?JLabel("?");??????????result.setForeground(Color.black);??
  27. ????????result.setBorder(BorderFactory.createCompoundBorder(???????????????BorderFactory.createLineBorder(Color.black),??
  28. ?????????????BorderFactory.createEmptyBorder(5,5,5,5)??????????));??
  29. ??????????//布置构件??
  30. ????????JPanel?patternPanel?=?new?JPanel();??????????patternPanel.setLayout(new?BoxLayout(patternPanel,??
  31. ???????????????????????????????BoxLayout.PAGE_AXIS));??????????patternPanel.add(patternLabel1);??
  32. ????????patternPanel.add(patternLabel2);??????????patternList.setAlignmentX(Component.LEFT_ALIGNMENT);??
  33. ????????patternPanel.add(patternList);????
  34. ????????JPanel?resultPanel?=?new?JPanel(new?GridLayout(0,?1));??????????resultPanel.add(resultLabel);??
  35. ????????resultPanel.add(result);????
  36. ????????patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);??????????resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);??
  37. ??????????add(patternPanel);??
  38. ????????add(Box.createRigidArea(new?Dimension(0,?10)));??????????add(resultPanel);??
  39. ??????????setBorder(BorderFactory.createEmptyBorder(10,10,10,10));??
  40. ??????????reformat();??
  41. ????}???/**?
  42. ?*方法说明:事件处理??*/??
  43. ????public?void?actionPerformed(ActionEvent?e)?{??????????JComboBox?cb?=?(JComboBox)e.getSource();??
  44. ????????String?newSelection?=?(String)cb.getSelectedItem();??????????currentPattern?=?newSelection;??
  45. ????????reformat();??????}??
  46. /**??*方法说明:格式和显示今天的日期?
  47. ?*/??????public?void?reformat()?{??
  48. ????????Date?today?=?new?Date();??????????SimpleDateFormat?formatter?=??
  49. ???????????new?SimpleDateFormat(currentPattern);??????????try?{??
  50. ????????????String?dateString?=?formatter.format(today);??????????????result.setForeground(Color.black);??
  51. ????????????result.setText(dateString);??????????}?catch?(IllegalArgumentException?iae)?{??
  52. ????????????result.setForeground(Color.red);??????????????result.setText("Error:?"?+?iae.getMessage());??
  53. ????????}??????}??
  54. /**??*方法说明:主方法?
  55. ?*/??????public?static?void?main(String[]?args)?{??
  56. ????????JFrame.setDefaultLookAndFeelDecorated(true);????
  57. ????????//创建一个窗体??????????frame?=?new?JFrame("ComboBoxDemo");??
  58. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  59. ????????//创建一个面版容器??????????JComponent?newContentPane?=?new?ComboBoxDemo();??
  60. ????????newContentPane.setOpaque(true);??????????frame.setContentPane(newContentPane);??
  61. ??????????//显示窗体??
  62. ????????frame.pack();??????????frame.setVisible(true);??
  63. ????}??}??

?

view plain
  1. package?test14;????
  2. import?java.awt.*;??import?java.awt.event.*;??
  3. import?javax.swing.*;??import?javax.swing.event.*;??
  4. /**??*?Title:?列表框?
  5. ?*?Description:?通过输入框添加元素和点击“删除”按钮删除列表元素??*?Filename:?ListDemo.java?
  6. ?*/??public?class?ListDemo?extends?JPanel??
  7. ??????????????????????implements?ListSelectionListener?{??????private?static?final?long?serialVersionUID?=?1L;??
  8. ????private?JList?list;??????private?DefaultListModel?listModel;??
  9. ??????private?static?final?String?hireString?=?"添加";??
  10. ????private?static?final?String?fireString?=?"删除";??????private?JButton?fireButton;??
  11. ????private?JTextField?employeeName;????
  12. ????public?ListDemo()?{??????????super(new?BorderLayout());??
  13. ????????//构建List的列表元素??????????listModel?=?new?DefaultListModel();??
  14. ????????listModel.addElement("Alan?Sommerer");??????????listModel.addElement("Alison?Huml");??
  15. ????????listModel.addElement("Kathy?Walrath");??????????listModel.addElement("Lisa?Friendly");??
  16. ????????listModel.addElement("Mary?Campione");??????????listModel.addElement("Sharon?Zakhour");??
  17. ??????????//创建一个List构件,并将列表元素添加到列表中??
  18. ????????list?=?new?JList(listModel);??????????//设置选择模式为单选??
  19. ????????list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);??????????//初始化选择索引在0的位置,即第一个元素??
  20. ????????list.setSelectedIndex(0);??????????list.addListSelectionListener(this);??
  21. ????????//设置列表可以同时看5个元素??????????list.setVisibleRowCount(5);??
  22. ????????//给列表添加一个滑动块??????????JScrollPane?listScrollPane?=?new?JScrollPane(list);??
  23. ??????????JButton?hireButton?=?new?JButton(hireString);??
  24. ????????HireListener?hireListener?=?new?HireListener(hireButton);??????????hireButton.setActionCommand(hireString);??
  25. ????????hireButton.addActionListener(hireListener);??????????hireButton.setEnabled(false);??
  26. ??????????fireButton?=?new?JButton(fireString);??
  27. ????????fireButton.setActionCommand(fireString);??????????fireButton.addActionListener(new?FireListener());??
  28. ??????????employeeName?=?new?JTextField(10);??
  29. ????????employeeName.addActionListener(hireListener);??????????employeeName.getDocument().addDocumentListener(hireListener);??
  30. ????????@SuppressWarnings("unused")??????????String?name?=?listModel.getElementAt(??
  31. ??????????????????????????????list.getSelectedIndex()).toString();????
  32. ????????//创建一个面板??????????JPanel?buttonPane?=?new?JPanel();??
  33. ????????buttonPane.setLayout(new?BoxLayout(buttonPane,?????????????????????????????????????????????BoxLayout.LINE_AXIS));??
  34. ????????buttonPane.add(fireButton);??????????buttonPane.add(Box.createHorizontalStrut(5));??
  35. ????????buttonPane.add(new?JSeparator(SwingConstants.VERTICAL));??????????buttonPane.add(Box.createHorizontalStrut(5));??
  36. ????????buttonPane.add(employeeName);??????????buttonPane.add(hireButton);??
  37. ????????buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));????
  38. ????????add(listScrollPane,?BorderLayout.CENTER);??????????add(buttonPane,?BorderLayout.PAGE_END);??
  39. ????}??/**?
  40. ?*类说明:“添加”按钮监听??*类描述:当点击“添加”按钮后,实现将元素添加到列表框中?
  41. ?*/??????class?FireListener?implements?ActionListener?{??
  42. ????????public?void?actionPerformed(ActionEvent?e)?{???????????????
  43. ????????????int?index?=?list.getSelectedIndex();??????????????listModel.remove(index);??
  44. ??????????????int?size?=?listModel.getSize();??
  45. ??????????????if?(size?==?0)?{?//如果没有了选择项,则是“删除”按钮实效??
  46. ????????????????fireButton.setEnabled(false);????
  47. ????????????}?else?{?//选择了一个??????????????????if?(index?==?listModel.getSize())?{??
  48. ????????????????????//移除选项??????????????????????index--;??
  49. ????????????????}????
  50. ????????????????list.setSelectedIndex(index);??????????????????list.ensureIndexIsVisible(index);??
  51. ????????????}??????????}??
  52. ????}????
  53. /**??*类说明:“删除”按钮监听事件?
  54. ?*类描述:实现删除列表元素??*/??
  55. ????class?HireListener?implements?ActionListener,?DocumentListener?{??????????private?boolean?alreadyEnabled?=?false;??
  56. ????????private?JButton?button;????
  57. ????????public?HireListener(JButton?button)?{??????????????this.button?=?button;??
  58. ????????}????
  59. ????????//必须实现?ActionListener.??????????public?void?actionPerformed(ActionEvent?e)?{??
  60. ????????????String?name?=?employeeName.getText();????
  61. ????????????//如果输入空或有同名??????????????if?(name.equals("")?||?alreadyInList(name))?{??
  62. ????????????????Toolkit.getDefaultToolkit().beep();??????????????????employeeName.requestFocusInWindow();??
  63. ????????????????employeeName.selectAll();??????????????????return;??
  64. ????????????}????
  65. ????????????int?index?=?list.getSelectedIndex();?//获取选择项??????????????if?(index?==?-1)?{?//如果没有选择,就插入到第一个??
  66. ????????????????index?=?0;??????????????}?else?{???????????//如果有选择,那么插入到选择项的后面??
  67. ????????????????index++;??????????????}??
  68. ??????????????listModel.insertElementAt(employeeName.getText(),?index);??
  69. ???????????????//重新设置文本??
  70. ????????????employeeName.requestFocusInWindow();??????????????employeeName.setText("");??
  71. ??????????????//选择新的元素,并显示出来??
  72. ????????????list.setSelectedIndex(index);??????????????list.ensureIndexIsVisible(index);??
  73. ????????}??/**?
  74. ?*方法说明:检测是否在LIST中有重名元素??*输入参数:String?name?检测的名字?
  75. ?*返回类型:boolean?布尔值,如果存在返回true??*/??
  76. ??????????protected?boolean?alreadyInList(String?name)?{??
  77. ????????????return?listModel.contains(name);??????????}??
  78. ??/**?
  79. ?*方法说明:实现DocumentListener接口,必需实现的方法:??*/??
  80. ????????public?void?insertUpdate(DocumentEvent?e)?{??????????????enableButton();??
  81. ????????}????
  82. /**??*方法说明:实现DocumentListener接口,必需实现的方法?
  83. ?*/??????????public?void?removeUpdate(DocumentEvent?e)?{??
  84. ????????????handleEmptyTextField(e);??????????}??
  85. ??/**?
  86. ?*方法说明:实现DocumentListener接口,必需实现的方法??*/??
  87. ????????public?void?changedUpdate(DocumentEvent?e)?{??????????????if?(!handleEmptyTextField(e))?{??
  88. ????????????????enableButton();??????????????}??
  89. ????????}??/**?
  90. ?*方法说明:按钮使能??*/??
  91. ????????private?void?enableButton()?{??????????????if?(!alreadyEnabled)?{??
  92. ????????????????button.setEnabled(true);??????????????}??
  93. ????????}??/**?
  94. ?*方法说明:实现DocumentListener接口,必需实现的方法,修改按钮的状态??*/??
  95. ????????private?boolean?handleEmptyTextField(DocumentEvent?e)?{??????????????if?(e.getDocument().getLength()?<=?0)?{??
  96. ????????????????button.setEnabled(false);??????????????????alreadyEnabled?=?false;??
  97. ????????????????return?true;??????????????}??
  98. ????????????return?false;??????????}??
  99. ????}??/**?
  100. ?*方法说明:实现ListSelectionListener接口,必需实现的方法:??*/??
  101. ????public?void?valueChanged(ListSelectionEvent?e)?{??????????if?(e.getValueIsAdjusting()?==?false)?{??
  102. ??????????????if?(list.getSelectedIndex()?==?-1)?{??
  103. ????????????????fireButton.setEnabled(false);????
  104. ????????????}?else?{??????????????????fireButton.setEnabled(true);??
  105. ????????????}??????????}??
  106. ????}??/**?
  107. ?*方法说明:主方法??*/??
  108. ????public?static?void?main(String[]?args)?{????
  109. ????????JFrame.setDefaultLookAndFeelDecorated(true);????
  110. ????????//创建一个窗体??????????JFrame?frame?=?new?JFrame("ListDemo");??
  111. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  112. ????????//创建一个面版??????????JComponent?newContentPane?=?new?ListDemo();??
  113. ????????newContentPane.setOpaque(true);??????????frame.setContentPane(newContentPane);??
  114. ??????????//显示窗体??
  115. ????????frame.pack();??????????frame.setVisible(true);??
  116. ????}??}??

?

view plain
  1. package?test15;????
  2. import?javax.swing.JTabbedPane;??import?javax.swing.ImageIcon;??
  3. import?javax.swing.JLabel;??import?javax.swing.JPanel;??
  4. import?javax.swing.JFrame;??/**?
  5. ?*?Title:?选项卡演示??*?Description:?这里是一个选项卡演示,点击不同的卡片,显示的内容不同?
  6. ?*?Filename:?TabbedPaneDemo.java??*/??
  7. import?java.awt.*;????
  8. public?class?TabbedPaneDemo?extends?JPanel?{??????/**?
  9. ?????*???????*/??
  10. ????private?static?final?long?serialVersionUID?=?1L;????
  11. ????public?TabbedPaneDemo()?{??????????super(new?GridLayout(1,?1));??
  12. ??????????ImageIcon?icon?=?createImageIcon("images/middle.gif");??
  13. ????????JTabbedPane?tabbedPane?=?new?JTabbedPane();????
  14. ????????Component?panel1?=?makeTextPanel("#第一个卡片#");??????????tabbedPane.addTab("One",?icon,?panel1,??
  15. ??????????????????????????"第一个卡片提示信息!");??????????tabbedPane.setSelectedIndex(0);??
  16. ??????????Component?panel2?=?makeTextPanel("##第二个卡片##");??
  17. ????????tabbedPane.addTab("Two",?icon,?panel2,????????????????????????????"第二个卡片提示信息!");??
  18. ??????????Component?panel3?=?makeTextPanel("###第三个卡片###");??
  19. ????????tabbedPane.addTab("Three",?icon,?panel3,????????????????????????????"第三个卡片提示信息!");??
  20. ??????????Component?panel4?=?makeTextPanel("####第四个卡片####");??
  21. ????????tabbedPane.addTab("Four",?icon,?panel4,????????????????????????????"第四个卡片提示信息!");??
  22. ??????????//将选项卡添加到panl中??
  23. ????????add(tabbedPane);??????}??
  24. /**??*方法说明:添加信息到选项卡中?
  25. ?*输入参数:String?text?显示的信息内容??*返回类型:Component?成员对象?
  26. ?*/??????protected?Component?makeTextPanel(String?text)?{??
  27. ????????JPanel?panel?=?new?JPanel(false);??????????JLabel?filler?=?new?JLabel(text);??
  28. ????????filler.setHorizontalAlignment(JLabel.CENTER);??????????panel.setLayout(new?GridLayout(1,?1));??
  29. ????????panel.add(filler);??????????return?panel;??
  30. ????}??/**?
  31. ?*方法说明:获得图片??*输入参数:String?path?图片的路径?
  32. ?*返回类型:ImageIcon?图片对象??*/??
  33. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?TabbedPaneDemo.class.getResource(path);??
  34. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  35. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  36. ????????????return?null;??????????}??
  37. ????}????
  38. ????public?static?void?main(String[]?args)?{??????????//使用Swing窗体描述??
  39. ????????JFrame.setDefaultLookAndFeelDecorated(true);????
  40. ????????//创建窗体??????????JFrame?frame?=?new?JFrame("TabbedPaneDemo");??
  41. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??????????frame.getContentPane().add(new?TabbedPaneDemo(),??
  42. ?????????????????????????????????BorderLayout.CENTER);????
  43. ????????//显示窗体??????????frame.setSize(400,?200);??
  44. ????????frame.setVisible(true);??????}??
  45. }??

?

view plain
  1. package?test16;????
  2. import?javax.swing.JOptionPane;??import?javax.swing.JDialog;??
  3. import?javax.swing.JTextField;??import?java.beans.*;?//property?change?stuff??
  4. import?java.awt.*;??import?java.awt.event.*;??
  5. /**??*?Title:?用户自定义对话框?
  6. ?*?Description:?自己定义对话框的风格。这使得对话框的样式更加多样化??*?Filename:?CustomDialog.java?
  7. ?*/??class?CustomDialog?extends?JDialog??
  8. ???????????????????implements?ActionListener,????????????????????????????????PropertyChangeListener?{??
  9. ??????private?static?final?long?serialVersionUID?=?1L;??
  10. ????private?String?typedText?=?null;??????private?JTextField?textField;??
  11. ????private?DialogDemo?dd;????
  12. ????private?String?magicWord;??????private?JOptionPane?optionPane;??
  13. ??????private?String?btnString1?=?"确定";??
  14. ????private?String?btnString2?=?"取消";??/**?
  15. ?*方法说明:返回文本输入字符??*/??
  16. ????public?String?getValidatedText()?{??????????return?typedText;??
  17. ????}??/**?
  18. ?*方法说明:创建一个结果对话框??*/??
  19. ????public?CustomDialog(Frame?aFrame,?String?aWord,?DialogDemo?parent)?{??????????super(aFrame,?true);??
  20. ????????dd?=?parent;????????????
  21. ????????magicWord?=?aWord.toUpperCase();??????????setTitle("测试");??
  22. ??????????textField?=?new?JTextField(10);??
  23. ??????????//定义显示信息??
  24. ????????String?msgString1?=?"李先生:?jeck是你的英文名字吗?";??????????String?msgString2?=?"(这个答案是:?""?+?magicWord??
  25. ??????????????????????????????+?""。)";??????????Object[]?array?=?{msgString1,?msgString2,?textField};??
  26. ????
  27. ????????Object[]?options?=?{btnString1,?btnString2};????
  28. ????????//创建对话框??????????optionPane?=?new?JOptionPane(array,??
  29. ????????????????????????????????????JOptionPane.QUESTION_MESSAGE,??????????????????????????????????????JOptionPane.YES_NO_OPTION,??
  30. ????????????????????????????????????null,??????????????????????????????????????options,??
  31. ????????????????????????????????????options[0]);????
  32. ????????//显示对话框??????????setContentPane(optionPane);??
  33. ??????????//设置当关闭窗体动作模式??
  34. ????????setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);??????????addWindowListener(new?WindowAdapter()?{??
  35. ????????????????public?void?windowClosing(WindowEvent?we)?{????????????????????
  36. ????????????????????optionPane.setValue(new?Integer(??????????????????????????????????????????JOptionPane.CLOSED_OPTION));??
  37. ????????????}??????????});??
  38. ??????????//使的文本输入域得到焦点??
  39. ????????addComponentListener(new?ComponentAdapter()?{??????????????public?void?componentShown(ComponentEvent?ce)?{??
  40. ????????????????textField.requestFocusInWindow();??????????????}??
  41. ????????});????
  42. ????????//给文本域添加监听事件??????????textField.addActionListener(this);??
  43. ??????????//监听输入改变??
  44. ????????optionPane.addPropertyChangeListener(this);??????}??
  45. ??????/**?文本域监听处理?*/??
  46. ????public?void?actionPerformed(ActionEvent?e)?{??????????optionPane.setValue(btnString1);??
  47. ????}????
  48. ????/**?监听输入的改变?*/??????public?void?propertyChange(PropertyChangeEvent?e)?{??
  49. ????????String?prop?=?e.getPropertyName();????
  50. ????????if?(isVisible()???????????&&?(e.getSource()?==?optionPane)??
  51. ?????????&&?(JOptionPane.VALUE_PROPERTY.equals(prop)?||???????????????JOptionPane.INPUT_VALUE_PROPERTY.equals(prop)))?{??
  52. ????????????Object?value?=?optionPane.getValue();????
  53. ????????????if?(value?==?JOptionPane.UNINITIALIZED_VALUE)?{???????????????????return;??
  54. ????????????}????
  55. ????????????optionPane.setValue(??????????????????????JOptionPane.UNINITIALIZED_VALUE);??
  56. ??????????????if?(btnString1.equals(value))?{??
  57. ????????????????????typedText?=?textField.getText();??????????????????String?ucText?=?typedText.toUpperCase();??
  58. ????????????????if?(magicWord.equals(ucText))?{??????????????????????//如果输入有效,则清楚文本域并隐藏对话框??
  59. ????????????????????clearAndHide();??????????????????}?else?{??
  60. ????????????????????//文本输入无效??????????????????????textField.selectAll();??
  61. ????????????????????JOptionPane.showMessageDialog(??????????????????????????????????????CustomDialog.this,??
  62. ????????????????????????????????????"对不起,?""?+?typedText?+?""?"??????????????????????????????????????+?"是无效的输入。\n"??
  63. ????????????????????????????????????+?"请重新输入"??????????????????????????????????????+?magicWord?+?".",??
  64. ????????????????????????????????????"再试一次",??????????????????????????????????????JOptionPane.ERROR_MESSAGE);??
  65. ????????????????????typedText?=?null;??????????????????????textField.requestFocusInWindow();??
  66. ????????????????}??????????????}?else?{?//用户关闭了对话框或点击了“cancel”??
  67. ????????????????dd.setLabel("好吧!?"???????????????????????????+?"我们不能影响你的决定输入"??
  68. ?????????????????????????+?magicWord?+?"。");??????????????????typedText?=?null;??
  69. ????????????????clearAndHide();??????????????}??
  70. ????????}??????}??
  71. /**??*方法说明:清楚文本域并隐藏痘翱蝌?
  72. ??*/??
  73. ????public?void?clearAndHide()?{??????????textField.setText(null);??
  74. ????????setVisible(false);??????}??
  75. }??

?

view plain
  1. package?test16;????
  2. import?javax.swing.JOptionPane;??import?javax.swing.JDialog;??
  3. import?javax.swing.JButton;??import?javax.swing.JRadioButton;??
  4. import?javax.swing.ButtonGroup;??import?javax.swing.JLabel;??
  5. import?javax.swing.ImageIcon;??import?javax.swing.BoxLayout;??
  6. import?javax.swing.Box;??import?javax.swing.BorderFactory;??
  7. import?javax.swing.border.Border;??import?javax.swing.JTabbedPane;??
  8. import?javax.swing.JPanel;??import?javax.swing.JFrame;??
  9. import?java.beans.*;???import?java.awt.*;??
  10. import?java.awt.event.*;????
  11. /**??*Title:?对话框演示?
  12. ?*Description:?全面的演示各种类型的对话框的使用??*Filename:?DialogDemo.java?
  13. ?*/??public?class?DialogDemo?extends?JPanel?{??
  14. ??????private?static?final?long?serialVersionUID?=?1L;??
  15. ????JLabel?label;??????ImageIcon?icon?=?createImageIcon("images/middle.gif");??
  16. ????JFrame?frame;??????String?simpleDialogDesc?=?"简单的信息提示对话窗";??
  17. ????String?iconDesc?=?"带有图标的对话窗";??????String?moreDialogDesc?=?"复杂信息对话窗";??
  18. ????CustomDialog?customDialog;??/**?
  19. ?*方法说明:构造器,生成一个面板添加到JFrame中??*输入参数:?
  20. ?*返回类型:??*/??
  21. ????public?DialogDemo(JFrame?frame)?{??????????super(new?BorderLayout());??
  22. ????????this.frame?=?frame;??????????customDialog?=?new?CustomDialog(frame,?"tom",?this);??
  23. ????????customDialog.pack();????
  24. ????????//创建成员??????????JPanel?frequentPanel?=?createSimpleDialogBox();??
  25. ????????JPanel?featurePanel?=?createFeatureDialogBox();??????????JPanel?iconPanel?=?createIconDialogBox();??
  26. ????????label?=?new?JLabel("点击"显示"?按钮"?????????????????????????????+?"?显示一个选择的对话框",??
  27. ???????????????????????????JLabel.CENTER);????
  28. ????????//放置对象??????????Border?padding?=?BorderFactory.createEmptyBorder(20,20,5,20);??
  29. ????????frequentPanel.setBorder(padding);??????????featurePanel.setBorder(padding);??
  30. ????????iconPanel.setBorder(padding);??????????//创建选项卡??
  31. ????????JTabbedPane?tabbedPane?=?new?JTabbedPane();??????????tabbedPane.addTab("简单对话窗",?null,??
  32. ??????????????????????????frequentPanel,????????????????????????????simpleDialogDesc);???
  33. ????????tabbedPane.addTab("复杂对话窗",?null,????????????????????????????featurePanel,??
  34. ??????????????????????????moreDialogDesc);??????????tabbedPane.addTab("图标对话窗",?null,??
  35. ??????????????????????????iconPanel,????????????????????????????iconDesc);??
  36. ??????????add(tabbedPane,?BorderLayout.CENTER);??
  37. ????????add(label,?BorderLayout.PAGE_END);??????????label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));??
  38. ????}??/**?
  39. ?*方法说明:设置按钮上的文字??*输入参数:String?newText?添加的文字?
  40. ?*返回类型:??*/??
  41. ????void?setLabel(String?newText)?{??????????label.setText(newText);??
  42. ????}??/**?
  43. ?*方法说明:获取图片??*输入参数:String?path?图片完整路径和名字?
  44. ?*返回类型:ImageIcon?图片对象??*/??
  45. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?DialogDemo.class.getResource(path);??
  46. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  47. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  48. ????????????return?null;??????????}??
  49. ????}??/**?
  50. ?*方法说明:创建一个JPanel,给第一个选项卡??*输入参数:?
  51. ?*返回类型:??*/??
  52. ????private?JPanel?createSimpleDialogBox()?{??????????final?int?numButtons?=?4;??
  53. ????????JRadioButton[]?radioButtons?=?new?JRadioButton[numButtons];??????????final?ButtonGroup?group?=?new?ButtonGroup();??
  54. ??????????JButton?showItButton?=?null;??
  55. ??????????final?String?defaultMessageCommand?=?"default";??
  56. ????????final?String?yesNoCommand?=?"yesno";??????????final?String?yeahNahCommand?=?"yeahnah";??
  57. ????????final?String?yncCommand?=?"ync";??????????//添加单选到数字??
  58. ????????radioButtons[0]?=?new?JRadioButton("只有“OK”按钮");??????????radioButtons[0].setActionCommand(defaultMessageCommand);??
  59. ??????????radioButtons[1]?=?new?JRadioButton("有“Yes/No”二个按钮");??
  60. ????????radioButtons[1].setActionCommand(yesNoCommand);????
  61. ????????radioButtons[2]?=?new?JRadioButton("有“Yes/No”两个按钮?"????????????????????????+?"(程序添加文字)");??
  62. ????????radioButtons[2].setActionCommand(yeahNahCommand);????
  63. ????????radioButtons[3]?=?new?JRadioButton("有“Yes/No/Cancel”三个按钮?"?????????????????????????????+?"(程序添加文字)");??
  64. ????????radioButtons[3].setActionCommand(yncCommand);??????????//将四个单选组成一个群??
  65. ????????for?(int?i?=?0;?i?<?numButtons;?i++)?{??????????????group.add(radioButtons[i]);??
  66. ????????}??????????//设置第一个为默认选择??
  67. ????????radioButtons[0].setSelected(true);??????????//定义“显示”按钮??
  68. ????????showItButton?=?new?JButton("显示");??????????//给“显示”按钮添加监听??
  69. ????????showItButton.addActionListener(new?ActionListener()?{??????????????public?void?actionPerformed(ActionEvent?e)?{??
  70. ????????????????String?command?=?group.getSelection().getActionCommand();????
  71. ????????????????//ok对话窗??????????????????if?(command?==?defaultMessageCommand)?{??
  72. ????????????????????JOptionPane.showMessageDialog(frame,??????????????????????????????????"鸡蛋不可能是绿色的!");??
  73. ??????????????????//yes/no?对话窗??
  74. ????????????????}?else?if?(command?==?yesNoCommand)?{??????????????????????int?n?=?JOptionPane.showConfirmDialog(??
  75. ????????????????????????????frame,?"你喜欢吃酸菜鱼吗?",??????????????????????????????"一个很无聊的问题!!",??
  76. ????????????????????????????JOptionPane.YES_NO_OPTION);??????????????????????if?(n?==?JOptionPane.YES_OPTION)?{//选择yes??
  77. ????????????????????????setLabel("哇!我也是!");??????????????????????}?else?if?(n?==?JOptionPane.NO_OPTION)?{//选择no??
  78. ????????????????????????setLabel("唉!我喜欢吃!");??????????????????????}?else?{??
  79. ????????????????????????setLabel("快告诉我吧!");??????????????????????}??
  80. ??????????????????//yes/no?(自己输入选项)??
  81. ????????????????}?else?if?(command?==?yeahNahCommand)?{??????????????????????Object[]?options?=?{"是的",?"不喜欢"};??
  82. ????????????????????int?n?=?JOptionPane.showOptionDialog(frame,??????????????????????????????????????"你喜欢酸菜鱼吗?",??
  83. ????????????????????????????????????"又一个无聊的问题!",??????????????????????????????????????JOptionPane.YES_NO_OPTION,??
  84. ????????????????????????????????????JOptionPane.QUESTION_MESSAGE,??????????????????????????????????????null,??
  85. ????????????????????????????????????options,??????????????????????????????????????options[0]);??
  86. ????????????????????if?(n?==?JOptionPane.YES_OPTION)?{??????????????????????????setLabel("你哄人的吧,我也喜欢。");??
  87. ????????????????????}?else?if?(n?==?JOptionPane.NO_OPTION)?{??????????????????????????setLabel("其实我也不喜欢!");??
  88. ????????????????????}?else?{??????????????????????????setLabel("这都不肯告诉我,小气鬼!");??
  89. ????????????????????}????
  90. ????????????????//yes/no/cancel?对话框??????????????????}?else?if?(command?==?yncCommand)?{??
  91. ????????????????????Object[]?options?=?{"是的,给我来一份。",??????????????????????????????????????????"不,谢谢!",??
  92. ????????????????????????????????????????"不,我要水煮鱼!"};??????????????????????//构造对话框??
  93. ????????????????????int?n?=?JOptionPane.showOptionDialog(frame,??????????????????????????????????????"先生!我们这里有鲜美的酸菜鱼,您需要吗?",??
  94. ????????????????????????????????????"服务生的问题。",??????????????????????????????????????JOptionPane.YES_NO_CANCEL_OPTION,??
  95. ????????????????????????????????????JOptionPane.QUESTION_MESSAGE,??????????????????????????????????????null,??
  96. ????????????????????????????????????options,??????????????????????????????????????options[2]);??
  97. ????????????????????if?(n?==?JOptionPane.YES_OPTION)?{??????????????????????????setLabel("你要的酸菜鱼来了!");??
  98. ????????????????????}?else?if?(n?==?JOptionPane.NO_OPTION)?{??????????????????????????setLabel("好的,你需要其它的。");??
  99. ????????????????????}?else?if?(n?==?JOptionPane.CANCEL_OPTION)?{??????????????????????????setLabel("好的,我们给你做水煮鱼!");??
  100. ????????????????????}?else?{??????????????????????????setLabel("对不起!你还没有点菜呢!");??
  101. ????????????????????}??????????????????}??
  102. ????????????????return;??????????????}??
  103. ????????});????
  104. ????????return?createPane(simpleDialogDesc?+?":",????????????????????????????radioButtons,??
  105. ??????????????????????????showItButton);??????}??
  106. /**??*方法说明:提供给createSimpleDialogBox和createFeatureDialogBox方法?
  107. ?*方法说明:创建带提示信息、一列单选框和“显示”按钮??*输入参数:String?description?提示帮助信息?
  108. ?*输入参数:JRadioButton[]?radioButtons?单选框组??*输入参数:JButton?showButton?“显示”按钮?
  109. ?*返回类型:JPanel?添加好的面板??*/??
  110. ????private?JPanel?createPane(String?description,????????????????????????????????JRadioButton[]?radioButtons,??
  111. ??????????????????????????????JButton?showButton)?{????
  112. ????????int?numChoices?=?radioButtons.length;??????????JPanel?box?=?new?JPanel();??
  113. ????????JLabel?label?=?new?JLabel(description);????
  114. ????????box.setLayout(new?BoxLayout(box,?BoxLayout.PAGE_AXIS));??????????box.add(label);??
  115. ????????//添加radio??????????for?(int?i?=?0;?i?<?numChoices;?i++)?{??
  116. ????????????box.add(radioButtons[i]);??????????}??
  117. ??????????JPanel?pane?=?new?JPanel(new?BorderLayout());??
  118. ????????pane.add(box,?BorderLayout.PAGE_START);??????????pane.add(showButton,?BorderLayout.PAGE_END);??
  119. ????????return?pane;??????}??
  120. /**??*方法说明:提供给createSimpleDialogBox和createFeatureDialogBox方法?
  121. ?*方法说明:创建带提示信息、二列单选框和“显示”按钮??*输入参数:String?description?提示帮助信息?
  122. ?*输入参数:JRadioButton[]?radioButtons?单选框组??*输入参数:JButton?showButton?“显示”按钮?
  123. ?*返回类型:JPanel?添加好的面板??*/??
  124. ?????private?JPanel?create2ColPane(String?description,????????????????????????????????????JRadioButton[]?radioButtons,??
  125. ??????????????????????????????????JButton?showButton)?{??????????JLabel?label?=?new?JLabel(description);??
  126. ????????int?numPerColumn?=?radioButtons.length/2;????
  127. ????????JPanel?grid?=?new?JPanel(new?GridLayout(0,?2));??????????for?(int?i?=?0;?i?<?numPerColumn;?i++)?{??
  128. ????????????grid.add(radioButtons[i]);??????????????grid.add(radioButtons[i?+?numPerColumn]);??
  129. ????????}????
  130. ????????JPanel?box?=?new?JPanel();??????????box.setLayout(new?BoxLayout(box,?BoxLayout.PAGE_AXIS));??
  131. ????????box.add(label);??????????grid.setAlignmentX(0.0f);??
  132. ????????box.add(grid);????
  133. ????????JPanel?pane?=?new?JPanel(new?BorderLayout());??????????pane.add(box,?BorderLayout.PAGE_START);??
  134. ????????pane.add(showButton,?BorderLayout.PAGE_END);????
  135. ????????return?pane;??????}??
  136. /**??*方法说明:创建第三个选项卡的面板?
  137. ?*方法说明:这里都是实现showMessageDialog类,但是也可以指定图标??*输入参数:?
  138. ?*返回类型:JPanel?构造好的面板??*/??
  139. ??????private?JPanel?createIconDialogBox()?{??
  140. ????????JButton?showItButton?=?null;????
  141. ????????final?int?numButtons?=?6;??????????JRadioButton[]?radioButtons?=?new?JRadioButton[numButtons];??
  142. ????????final?ButtonGroup?group?=?new?ButtonGroup();????
  143. ????????final?String?plainCommand?=?"plain";??????????final?String?infoCommand?=?"info";??
  144. ????????final?String?questionCommand?=?"question";??????????final?String?errorCommand?=?"error";??
  145. ????????final?String?warningCommand?=?"warning";??????????final?String?customCommand?=?"custom";??
  146. ??????????radioButtons[0]?=?new?JRadioButton("普通(没有图标)");??
  147. ????????radioButtons[0].setActionCommand(plainCommand);????
  148. ????????radioButtons[1]?=?new?JRadioButton("信息图标");??????????radioButtons[1].setActionCommand(infoCommand);??
  149. ??????????radioButtons[2]?=?new?JRadioButton("问题图标");??
  150. ????????radioButtons[2].setActionCommand(questionCommand);????
  151. ????????radioButtons[3]?=?new?JRadioButton("错误图标");??????????radioButtons[3].setActionCommand(errorCommand);??
  152. ??????????radioButtons[4]?=?new?JRadioButton("警告图标");??
  153. ????????radioButtons[4].setActionCommand(warningCommand);????
  154. ????????radioButtons[5]?=?new?JRadioButton("自定义图标");??????????radioButtons[5].setActionCommand(customCommand);??
  155. ??????????for?(int?i?=?0;?i?<?numButtons;?i++)?{??
  156. ????????????group.add(radioButtons[i]);??????????}??
  157. ????????radioButtons[0].setSelected(true);????
  158. ????????showItButton?=?new?JButton("显示");??????????showItButton.addActionListener(new?ActionListener()?{??
  159. ????????????public?void?actionPerformed(ActionEvent?e)?{??????????????????String?command?=?group.getSelection().getActionCommand();??
  160. ??????????????????//没有图标??
  161. ????????????????if?(command?==?plainCommand)?{??????????????????????JOptionPane.showMessageDialog(frame,??
  162. ????????????????????????????????????"水煮鱼里不要放酸菜!",??????????????????????????????????????"无图标",??
  163. ????????????????????????????????????JOptionPane.PLAIN_MESSAGE);??????????????????//信息图标??
  164. ????????????????}?else?if?(command?==?infoCommand)?{??????????????????????JOptionPane.showMessageDialog(frame,??
  165. ????????????????????????????????????"水煮鱼里不要放酸菜!",??????????????????????????????????????"信息图标",??
  166. ????????????????????????????????????JOptionPane.INFORMATION_MESSAGE);????
  167. ????????????????//问题图标??????????????????}?else?if?(command?==?questionCommand)?{??
  168. ????????????????????JOptionPane.showMessageDialog(frame,??????????????????????????????????????"请你吃饭前洗手,好吗?",??
  169. ????????????????????????????????????"问题",??????????????????????????????????????JOptionPane.QUESTION_MESSAGE);??
  170. ????????????????//错误图标??????????????????}?else?if?(command?==?errorCommand)?{??
  171. ????????????????????JOptionPane.showMessageDialog(frame,??????????????????????????????????????"对不起,你的信用卡没有资金了!",??
  172. ????????????????????????????????????"错误信息",??????????????????????????????????????JOptionPane.ERROR_MESSAGE);??
  173. ????????????????//警告图标??????????????????}?else?if?(command?==?warningCommand)?{??
  174. ????????????????????JOptionPane.showMessageDialog(frame,??????????????????????????????????????"警告!你严重透支信用卡,请尽快补齐金额!",??
  175. ????????????????????????????????????"警告信息",??????????????????????????????????????JOptionPane.WARNING_MESSAGE);??
  176. ????????????????//自定义图标??????????????????}?else?if?(command?==?customCommand)?{??
  177. ????????????????????JOptionPane.showMessageDialog(frame,??????????????????????????????????????"哈哈。我想用什么图标都可以!",??
  178. ????????????????????????????????????"自定义对话窗",??????????????????????????????????????JOptionPane.INFORMATION_MESSAGE,??
  179. ????????????????????????????????????icon);??????????????????}??
  180. ????????????}??????????});??
  181. ??????????return?create2ColPane(iconDesc?+?":",??
  182. ??????????????????????????????radioButtons,????????????????????????????????showItButton);??
  183. ????}??/**?
  184. ?*方法说明:创建一个JPanel,放在第二个选项卡上??*输入参数:?
  185. ?*返回类型:??*/??
  186. ????private?JPanel?createFeatureDialogBox()?{??????????final?int?numButtons?=?5;??
  187. ????????JRadioButton[]?radioButtons?=?new?JRadioButton[numButtons];??????????final?ButtonGroup?group?=?new?ButtonGroup();??
  188. ??????????JButton?showItButton?=?null;??
  189. ????????//定义操作命令??????????final?String?pickOneCommand?=?"pickone";??
  190. ????????final?String?textEnteredCommand?=?"textfield";??????????final?String?nonAutoCommand?=?"nonautooption";??
  191. ????????final?String?customOptionCommand?=?"customoption";??????????final?String?nonModalCommand?=?"nonmodal";??
  192. ????????//定义radio数组??????????radioButtons[0]?=?new?JRadioButton("选择一个");??
  193. ????????radioButtons[0].setActionCommand(pickOneCommand);????
  194. ????????radioButtons[1]?=?new?JRadioButton("输入信息");??????????radioButtons[1].setActionCommand(textEnteredCommand);??
  195. ??????????radioButtons[2]?=?new?JRadioButton("关闭按钮无效");??
  196. ????????radioButtons[2].setActionCommand(nonAutoCommand);????
  197. ????????radioButtons[3]?=?new?JRadioButton("输入校验"?????????????????????????????????????????????+?"(用户输入信息)");??
  198. ????????radioButtons[3].setActionCommand(customOptionCommand);????
  199. ????????radioButtons[4]?=?new?JRadioButton("没有模式");??????????radioButtons[4].setActionCommand(nonModalCommand);??
  200. ????????//合成一个组群??????????for?(int?i?=?0;?i?<?numButtons;?i++)?{??
  201. ????????????group.add(radioButtons[i]);??????????}??
  202. ????????//设置第一个为默认选择??????????radioButtons[0].setSelected(true);??
  203. ??????????showItButton?=?new?JButton("显示");??
  204. ????????showItButton.addActionListener(new?ActionListener()?{??????????????@SuppressWarnings("static-access")??
  205. ????????????public?void?actionPerformed(ActionEvent?e)?{??????????????????String?command?=?group.getSelection().getActionCommand();??
  206. ??????????????????//选择一个??
  207. ????????????????if?(command?==?pickOneCommand)?{??????????????????????Object[]?possibilities?=?{"辣椒",?"西红柿",?"洋葱"};??
  208. ????????????????????//设置对话框??????????????????????String?s?=?(String)JOptionPane.showInputDialog(??
  209. ????????????????????????????????????????frame,????//所属窗体??????????????????????????????????????????"请选择项目:\n"??
  210. ????????????????????????????????????????+?""鸡蛋炒"",??//输出信息??????????????????????????????????????????"客户选择",??
  211. ????????????????????????????????????????JOptionPane.PLAIN_MESSAGE,??//对话框模式??????????????????????????????????????????icon,???????????//显示图标??
  212. ????????????????????????????????????????possibilities,???//选项内容??????????????????????????????????????????"辣椒");????//默认选项??
  213. ??????????????????????//如果有选择??
  214. ????????????????????if?((s?!=?null)?&&?(s.length()?>?0))?{??????????????????????????setLabel("鸡蛋炒"?+?s?+?"!");??
  215. ????????????????????????return;??????????????????????}??
  216. ??????????????????????//如果客户没有选择??
  217. ????????????????????setLabel("快点!");????
  218. ????????????????//文本输入??????????????????}?else?if?(command?==?textEnteredCommand)?{??
  219. ????????????????????String?s?=?(String)JOptionPane.showInputDialog(??????????????????????????????????????????frame,??
  220. ????????????????????????????????????????"选择一个配料\n"??????????????????????????????????????????+?""鸡蛋炒"",??
  221. ????????????????????????????????????????"客户输入",??????????????????????????????????????????JOptionPane.PLAIN_MESSAGE,??
  222. ????????????????????????????????????????icon,??????????????????????????????????????????null,??
  223. ????????????????????????????????????????"辣椒");????
  224. ????????????????????//如果用户有输入??????????????????????if?((s?!=?null)?&&?(s.length()?>?0))?{??
  225. ????????????????????????setLabel("你要的是鸡蛋炒"?+?s?+?"!");??????????????????????????return;??
  226. ????????????????????}????
  227. ????????????????????//如果返回的是空或者是null。??????????????????????setLabel("快些选择!");??
  228. ??????????????????//关闭按钮无效??
  229. ????????????????}?else?if?(command?==?nonAutoCommand)?{??????????????????????//构造一个对话框面板??
  230. ????????????????????final?JOptionPane?optionPane?=?new?JOptionPane(??????????????????????????????????????"关闭这个对话框\n"??
  231. ????????????????????????????????????+?"请点击下面的按钮\n"??????????????????????????????????????+?"明白吗?",??
  232. ????????????????????????????????????JOptionPane.QUESTION_MESSAGE,??????????????????????????????????????JOptionPane.YES_NO_OPTION);??
  233. ??????????????????????JDialog.setDefaultLookAndFeelDecorated(false);??
  234. ????????????????????//构造一个对话框??????????????????????final?JDialog?dialog?=?new?JDialog(frame,??
  235. ?????????????????????????????????????????????????"点击一个按钮",???????????????????????????????????????????????????true);??
  236. ????????????????????//将对话框面板添加到对话框中??????????????????????dialog.setContentPane(optionPane);??
  237. ????????????????????//设置对话框关闭时的操作模式??????????????????????dialog.setDefaultCloseOperation(??
  238. ????????????????????????JDialog.DO_NOTHING_ON_CLOSE);??????????????????????dialog.addWindowListener(new?WindowAdapter()?{??
  239. ????????????????????????public?void?windowClosing(WindowEvent?we)?{?//当点击关闭按钮??????????????????????????????setLabel("阻碍用户视图关闭窗体!");??
  240. ????????????????????????}??????????????????????});??
  241. ??????????????????????????????????????????JDialog.setDefaultLookAndFeelDecorated(true);??
  242. ??????????????????????????????????????????optionPane.addPropertyChangeListener(??
  243. ????????????????????????new?PropertyChangeListener()?{??????????????????????????????public?void?propertyChange(PropertyChangeEvent?e)?{??
  244. ????????????????????????????????String?prop?=?e.getPropertyName();????
  245. ????????????????????????????????if?(dialog.isVisible()???????????????????????????????????&&?(e.getSource()?==?optionPane)??
  246. ?????????????????????????????????&&?(JOptionPane.VALUE_PROPERTY.equals(prop)))?{??????????????????????????????????????//如果你要阻止关闭按钮,可以在这里进行处理。??
  247. ??????????????????????????????????????????????????????????????????????????dialog.setVisible(false);??
  248. ????????????????????????????????}??????????????????????????????}??
  249. ????????????????????????});??????????????????????dialog.pack();??
  250. ????????????????????dialog.setLocationRelativeTo(frame);??????????????????????dialog.setVisible(true);??
  251. ??????????????????????????????????????????int?value?=?((Integer)optionPane.getValue()).intValue();??
  252. ????????????????????if?(value?==?JOptionPane.YES_OPTION)?{??????????????????????????setLabel("好的");??
  253. ????????????????????}?else?if?(value?==?JOptionPane.NO_OPTION)?{??????????????????????????setLabel("试图点击关闭按钮来关闭一个不能关闭的对话框!"??
  254. ?????????????????????????????????+?"你不能!");??????????????????????}?else?{??
  255. ????????????????????????setLabel("窗体可以使用ESC键关闭。");??????????????????????}??
  256. ???????????????????//自己定义版面??
  257. ????????????????}?else?if?(command?==?customOptionCommand)?{??????????????????????customDialog.setLocationRelativeTo(frame);??
  258. ????????????????????customDialog.setVisible(true);????
  259. ????????????????????String?s?=?customDialog.getValidatedText();??????????????????????if?(s?!=?null)?{??
  260. ????????????????????????//The?text?is?valid.??????????????????????????setLabel("欢迎你!"??
  261. ?????????????????????????????????+?"你已经进入了""???????????????????????????????????+?s??
  262. ?????????????????????????????????+?""。");??????????????????????}??
  263. ??????????????????//没有模式??
  264. ????????????????}?else?if?(command?==?nonModalCommand)?{??????????????????????//创建一个对话框??
  265. ????????????????????final?JDialog?dialog?=?new?JDialog(frame,?????????????????????????????????????????????????????????"一个没有模式的对话框");??
  266. ????????????????????//使用html语言来显示信息??????????????????????JLabel?label?=?new?JLabel("<html><p?align=center>"??
  267. ????????????????????????+?"这是一个没有模式的对话框"??????????????????????????+?"你可以使用更多的格式"??
  268. ????????????????????????+?"甚至可以使用主窗体!");??????????????????????label.setHorizontalAlignment(JLabel.CENTER);??
  269. ????????????????????Font?font?=?label.getFont();????????????????????????
  270. ????????????????????label.setFont(label.getFont().deriveFont(font.PLAIN,???????????????????????????????????????????????????????????????14.0f));??
  271. ??????????????????????JButton?closeButton?=?new?JButton("关闭");??
  272. ????????????????????closeButton.addActionListener(new?ActionListener()?{??????????????????????????public?void?actionPerformed(ActionEvent?e)?{??
  273. ????????????????????????????dialog.setVisible(false);??????????????????????????????dialog.dispose();??
  274. ????????????????????????}??????????????????????});??
  275. ????????????????????JPanel?closePanel?=?new?JPanel();??????????????????????closePanel.setLayout(new?BoxLayout(closePanel,??
  276. ???????????????????????????????????????????????????????BoxLayout.LINE_AXIS));??????????????????????closePanel.add(Box.createHorizontalGlue());??
  277. ????????????????????closePanel.add(closeButton);??????????????????????closePanel.setBorder(BorderFactory.??
  278. ????????????????????????createEmptyBorder(0,0,5,5));????
  279. ????????????????????JPanel?contentPane?=?new?JPanel(new?BorderLayout());??????????????????????contentPane.add(label,?BorderLayout.CENTER);??
  280. ????????????????????contentPane.add(closePanel,?BorderLayout.PAGE_END);??????????????????????contentPane.setOpaque(true);??
  281. ????????????????????dialog.setContentPane(contentPane);????
  282. ????????????????????//显示窗体??????????????????????dialog.setSize(new?Dimension(300,?150));??
  283. ????????????????????dialog.setLocationRelativeTo(frame);??????????????????????dialog.setVisible(true);??
  284. ????????????????}??????????????}??
  285. ????????});????
  286. ????????return?createPane(moreDialogDesc?+?":",????????????????????????????radioButtons,??
  287. ??????????????????????????showItButton);??????}??
  288. ??????public?static?void?main(String[]?args)?{??
  289. ??????????JFrame.setDefaultLookAndFeelDecorated(true);??
  290. ????????JDialog.setDefaultLookAndFeelDecorated(true);????
  291. ????????//创建和设置一个窗体??????????JFrame?frame?=?new?JFrame("DialogDemo");??
  292. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  293. ????????//设置一个面板??????????Container?contentPane?=?frame.getContentPane();??
  294. ????????contentPane.setLayout(new?GridLayout(1,1));??????????contentPane.add(new?DialogDemo(frame));??
  295. ??????????//显示窗体??
  296. ????????frame.pack();??????????frame.setVisible(true);??
  297. ????}??}??

?

view plain
  1. package?test17;????
  2. import?java.io.*;??import?java.awt.*;??
  3. import?java.awt.event.*;??import?javax.swing.*;??
  4. /**??*?Title:?文件对话框演示?
  5. ?*?Description:?演示打开文件对话框和保存文件对话框,使用了文件过滤。??*?Filename:?FileChooserDemo.java?
  6. ?*/????
  7. public?class?FileChooserDemo?extends?JPanel???????????????????????????????implements?ActionListener?{??
  8. ??????private?static?final?long?serialVersionUID?=?1L;??
  9. ????static?private?final?String?newline?=?"\n";??????JButton?openButton,?saveButton;??
  10. ????JTextArea?log;??????JFileChooser?fc;??
  11. ??????public?FileChooserDemo()?{??
  12. ????????super(new?BorderLayout());????
  13. ????????log?=?new?JTextArea(15,40);??????????log.setMargin(new?Insets(10,10,10,10));??
  14. ????????log.setEditable(false);??????????JScrollPane?logScrollPane?=?new?JScrollPane(log);??
  15. ??????????//创建一个文件过滤,使用当前目录??
  16. ????????fc?=?new?JFileChooser(".");??????????//过滤条件在MyFilter类中定义??
  17. ????????fc.addChoosableFileFilter(new?MyFilter());????
  18. ????????openButton?=?new?JButton("打开文件",???????????????????????????????????createImageIcon("images/Open16.gif"));??
  19. ????????openButton.addActionListener(this);????
  20. ????????saveButton?=?new?JButton("保存文件",???????????????????????????????????createImageIcon("images/Save16.gif"));??
  21. ????????saveButton.addActionListener(this);????
  22. ????????//构建一个面板,添加“打开文件”和“保存文件”??????????JPanel?buttonPanel?=?new?JPanel();???
  23. ????????buttonPanel.add(openButton);??????????buttonPanel.add(saveButton);??
  24. ??????????add(buttonPanel,?BorderLayout.PAGE_START);??
  25. ????????add(logScrollPane,?BorderLayout.CENTER);??????}??
  26. /**??*方法说明:事件处理?
  27. ?*输入参数:??*返回类型:?
  28. ?*/??????public?void?actionPerformed(ActionEvent?e)?{??
  29. ??????????//当点击“打开文件”按钮??
  30. ????????if?(e.getSource()?==?openButton)?{??????????????int?returnVal?=?fc.showOpenDialog(FileChooserDemo.this);??
  31. ??????????????if?(returnVal?==?JFileChooser.APPROVE_OPTION)?{??
  32. ????????????????File?file?=?fc.getSelectedFile();??????????????????//在这里添加一些对文件的处理??
  33. ????????????????log.append("打开文件:?"?+?file.getName()?+?newline);??????????????}?else?{??
  34. ????????????????log.append("打开文件被用户取消!"?+?newline);??????????????}??
  35. ??????????//点击“保存文件”按钮??
  36. ????????}?else?if?(e.getSource()?==?saveButton)?{??????????????int?returnVal?=?fc.showSaveDialog(FileChooserDemo.this);??
  37. ????????????if?(returnVal?==?JFileChooser.APPROVE_OPTION)?{??????????????????File?file?=?fc.getSelectedFile();??
  38. ????????????????//在这里添加一些对文件的处理??????????????????log.append("保存文件:?"?+?file.getName()??+?newline);??
  39. ????????????}?else?{??????????????????log.append("保存文件被用户取消!"?+?newline);??
  40. ????????????}??????????}??
  41. ????}??/**?
  42. ?*方法说明:获取图像对象??*输入参数:String?path?图片路径?
  43. ?*返回类型:ImageIcon?图片对象??*/??
  44. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?FileChooserDemo.class.getResource(path);??
  45. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  46. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  47. ????????????return?null;??????????}??
  48. ????}????
  49. ????public?static?void?main(String[]?args)?{??????????JFrame.setDefaultLookAndFeelDecorated(true);??
  50. ????????JDialog.setDefaultLookAndFeelDecorated(true);????
  51. ????????//创建窗体??????????JFrame?frame?=?new?JFrame("FileChooserDemo");??
  52. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  53. ????????//创建一个面板??????????JComponent?newContentPane?=?new?FileChooserDemo();??
  54. ????????newContentPane.setOpaque(true);??????????frame.setContentPane(newContentPane);??
  55. ??????????//显示窗体??
  56. ????????frame.pack();??????????frame.setVisible(true);??
  57. ????}??}??

?

view plain
  1. package?test17;????
  2. import?java.io.File;??import?javax.swing.filechooser.*;??
  3. /**??*?Title:?文件过滤器演示?
  4. ?*?Description:?FileChooserDemo文件使用的文件过滤器??*?Filename:?MyFilter.java?
  5. ?*/????
  6. public?class?MyFilter?extends?FileFilter?{?????@SuppressWarnings("unused")??
  7. private?String?files;?????public?boolean?accept(File?f)?{??
  8. ????????if?(f.isDirectory())?{??????????????return?true;??
  9. ????????}????
  10. ????????String?extension?=?getExtension(f);??????????if?(extension?!=?null)?{??
  11. ??????????????????????????if?(extension.equals("java"))?{//定义过滤Java文件??
  12. ????????????????????return?true;??????????????}?else?{??
  13. ????????????????return?false;??????????????}??
  14. ??????????}??
  15. ??????????return?false;??
  16. ????}????
  17. ????//过滤器描述??????public?String?getDescription()?{??
  18. ????????return?"Java";??????}??
  19. /**??*方法说明:获取文件扩展名?
  20. ?*/??????public?static?String?getExtension(File?f)?{??
  21. ????????String?ext?=?null;??????????String?s?=?f.getName();??
  22. ????????int?i?=?s.lastIndexOf('.');????
  23. ????????if?(i?>?0?&&??i?<?s.length()?-?1)?{??????????????ext?=?s.substring(i+1).toLowerCase();??
  24. ????????}??????????return?ext;??
  25. ????}??}??

?

view plain
  1. package?test18;????
  2. import?javax.swing.*;??import?java.awt.*;??
  3. import?java.awt.event.*;??/**?
  4. ?*?Description:?这里演示使用html语言在swing面板上构造显示信息??*?Filename:?HtmlDemo.java?
  5. ?*/????
  6. public?class?HtmlDemo?extends?JPanel????????????????????????implements?ActionListener?{??
  7. ??????private?static?final?long?serialVersionUID?=?1L;??
  8. ????JLabel?theLabel;??????JTextArea?htmlTextArea;??
  9. /**??*方法说明:构造器,描述窗体中的成员?
  10. ?*输入参数:??*返回类型:?
  11. ?*/??????public?HtmlDemo()?{??
  12. ????????setLayout(new?BoxLayout(this,?BoxLayout.LINE_AXIS));????
  13. ????????String?initialText?=?"<html>\n"?+??????????????????"颜色和字体测试:\n"?+??
  14. ????????????????"<ul>\n"?+??????????????????"<li><font?color=red>red</font>\n"?+??
  15. ????????????????"<li><font?color=blue>blue</font>\n"?+??????????????????"<li><font?color=green>green</font>\n"?+??
  16. ????????????????"<li><font?size=-2>small</font>\n"?+??????????????????"<li><font?size=+2>large</font>\n"?+??
  17. ????????????????"<li><i>italic</i>\n"?+??????????????????"<li><b>bold</b>\n"?+??
  18. ????????????????"</ul>\n";??????????//定义一个文本框??
  19. ????????htmlTextArea?=?new?JTextArea(10,?20);??????????htmlTextArea.setText(initialText);??
  20. ????????JScrollPane?scrollPane?=?new?JScrollPane(htmlTextArea);??????????//定义按钮??
  21. ????????JButton?changeTheLabel?=?new?JButton("改变显示");??????????changeTheLabel.setMnemonic(KeyEvent.VK_C);??
  22. ????????changeTheLabel.setAlignmentX(Component.CENTER_ALIGNMENT);??????????changeTheLabel.addActionListener(this);??
  23. ????????//定义标签??????????theLabel?=?new?JLabel(initialText)?{??
  24. ???????????????????????private?static?final?long?serialVersionUID?=?1L;??
  25. ????????????public?Dimension?getPreferredSize()?{??????????????????return?new?Dimension(200,?200);??
  26. ????????????}??????????????public?Dimension?getMinimumSize()?{??
  27. ????????????????return?new?Dimension(200,?200);??????????????}??
  28. ????????????public?Dimension?getMaximumSize()?{??????????????????return?new?Dimension(200,?200);??
  29. ????????????}??????????};??
  30. ????????//设置标签的对齐方式??????????theLabel.setVerticalAlignment(SwingConstants.CENTER);??
  31. ????????theLabel.setHorizontalAlignment(SwingConstants.CENTER);??????????//构造一个带边框的左边的编辑面板??
  32. ????????JPanel?leftPanel?=?new?JPanel();??????????leftPanel.setLayout(new?BoxLayout(leftPanel,?BoxLayout.PAGE_AXIS));??
  33. ????????leftPanel.setBorder(BorderFactory.createCompoundBorder(??????????????????BorderFactory.createTitledBorder(??
  34. ????????????????????"编辑HTML,点击按钮显示结果。"),??????????????????BorderFactory.createEmptyBorder(10,10,10,10)));??
  35. ????????leftPanel.add(scrollPane);??????????leftPanel.add(Box.createRigidArea(new?Dimension(0,10)));??
  36. ????????leftPanel.add(changeTheLabel);???????????//构造一个带边框的右边显示的面板??
  37. ????????JPanel?rightPanel?=?new?JPanel();??????????rightPanel.setLayout(new?BoxLayout(rightPanel,?BoxLayout.PAGE_AXIS));??
  38. ????????rightPanel.setBorder(BorderFactory.createCompoundBorder(??????????????????????????BorderFactory.createTitledBorder("这里使用标签显示HTML结果"),??
  39. ????????????????????????BorderFactory.createEmptyBorder(10,10,10,10)));??????????rightPanel.add(theLabel);??
  40. ??????????????????setBorder(BorderFactory.createEmptyBorder(10,10,10,10));??
  41. ????????add(leftPanel);??????????add(Box.createRigidArea(new?Dimension(10,0)));??
  42. ????????add(rightPanel);??????}??
  43. /**??*方法说明:事件监听,当用户点击按钮触发?
  44. ?*输入参数:??*返回类型:?
  45. ?*/??????public?void?actionPerformed(ActionEvent?e)?{??
  46. ????????theLabel.setText(htmlTextArea.getText());??????}??
  47. /**??*方法说明:主方法?
  48. ?*输入参数:??*返回类型:?
  49. ?*/??????public?static?void?main(String[]?args)?{??
  50. ??????????JFrame.setDefaultLookAndFeelDecorated(true);??
  51. ??????????//创建窗体??
  52. ????????JFrame?frame?=?new?JFrame("HtmlDemo");??????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  53. ??????????//创建面板??
  54. ????????JComponent?newContentPane?=?new?HtmlDemo();??????????newContentPane.setOpaque(true);??
  55. ????????frame.setContentPane(newContentPane);????
  56. ????????//显示窗体??????????frame.pack();??
  57. ????????frame.setVisible(true);??????}??
  58. }??

?

view plain
  1. package?test19;????
  2. import?java.awt.*;??import?java.awt.event.*;??
  3. import?javax.swing.JPopupMenu;??import?javax.swing.JMenu;??
  4. import?javax.swing.JMenuItem;??import?javax.swing.JCheckBoxMenuItem;??
  5. import?javax.swing.JRadioButtonMenuItem;??import?javax.swing.ButtonGroup;??
  6. import?javax.swing.JMenuBar;??import?javax.swing.KeyStroke;??
  7. import?javax.swing.ImageIcon;????
  8. import?javax.swing.JPanel;??import?javax.swing.JTextArea;??
  9. import?javax.swing.JScrollPane;??import?javax.swing.JFrame;??
  10. /**??*?Title:?菜单演示?
  11. ?*?Description:?演示菜单的建立和快捷键的使用。??*?Filename:?MenuDemo.java?
  12. ?*/????
  13. public?class?MenuDemo?implements?ActionListener,?ItemListener?{??????JTextArea?output;??
  14. ????JScrollPane?scrollPane;??????String?newline?=?"\n";??
  15. /**??*方法说明:组建菜单栏?
  16. ?*输入参数:??*返回类型:?
  17. ?*/??????public?JMenuBar?createMenuBar()?{??
  18. ????????JMenuBar?menuBar;??????????JMenu?menu,?submenu;??
  19. ????????JMenuItem?menuItem;??????????JRadioButtonMenuItem?rbMenuItem;??
  20. ????????JCheckBoxMenuItem?cbMenuItem;????
  21. ????????//定义菜单条??????????menuBar?=?new?JMenuBar();??
  22. ??????????//定义第一个菜单??
  23. ????????menu?=?new?JMenu("(A)菜单");??????????menu.setMnemonic(KeyEvent.VK_A);??
  24. ????????menuBar.add(menu);????
  25. ????????//下面开始定义菜单项????????????
  26. ????????//只有文字??????????menuItem?=?new?JMenuItem("(O)只有文本的菜单",??
  27. ?????????????????????????????????KeyEvent.VK_O);??????????//设置快捷键??
  28. ????????menuItem.setAccelerator(KeyStroke.getKeyStroke(??????????????????KeyEvent.VK_1,?ActionEvent.ALT_MASK));??
  29. ????????//添加监听??????????menuItem.addActionListener(this);??
  30. ????????menu.add(menuItem);??????????//有图标还有文字??
  31. ????????ImageIcon?icon?=?createImageIcon("images/middle.gif");??????????menuItem?=?new?JMenuItem("(B)有图标和文字的菜单",?icon);??
  32. ????????menuItem.setMnemonic(KeyEvent.VK_B);??????????menuItem.addActionListener(this);??
  33. ????????menu.add(menuItem);??????????//只有图标??
  34. ????????menuItem?=?new?JMenuItem(icon);??????????menuItem.setMnemonic(KeyEvent.VK_D);??
  35. ????????menuItem.addActionListener(this);??????????menu.add(menuItem);??
  36. ??????????//定义一组radio?button(单选按钮)菜单??
  37. ????????menu.addSeparator();??????????ButtonGroup?group?=?new?ButtonGroup();??
  38. ??????????rbMenuItem?=?new?JRadioButtonMenuItem("(R)使用radio的菜单");??
  39. ????????rbMenuItem.setSelected(true);??????????rbMenuItem.setMnemonic(KeyEvent.VK_R);??
  40. ????????group.add(rbMenuItem);??????????rbMenuItem.addActionListener(this);??
  41. ????????menu.add(rbMenuItem);????
  42. ????????rbMenuItem?=?new?JRadioButtonMenuItem("(d)另外一个radio菜单");??????????rbMenuItem.setMnemonic(KeyEvent.VK_D);??
  43. ????????group.add(rbMenuItem);??????????rbMenuItem.addActionListener(this);??
  44. ????????menu.add(rbMenuItem);????
  45. ????????//定义一组check?box(检查盒)菜单??????????menu.addSeparator();??
  46. ????????cbMenuItem?=?new?JCheckBoxMenuItem("(C)使用检查盒的菜单");??????????cbMenuItem.setMnemonic(KeyEvent.VK_C);??
  47. ????????cbMenuItem.addItemListener(this);??????????menu.add(cbMenuItem);??
  48. ??????????cbMenuItem?=?new?JCheckBoxMenuItem("(H)另外一个检查盒");??
  49. ????????cbMenuItem.setMnemonic(KeyEvent.VK_H);??????????cbMenuItem.addItemListener(this);??
  50. ????????menu.add(cbMenuItem);????
  51. ????????//定义一个带子菜单??????????menu.addSeparator();??
  52. ????????submenu?=?new?JMenu("(S)带有子菜单");??????????submenu.setMnemonic(KeyEvent.VK_S);??
  53. ????????//定义子菜单??????????menuItem?=?new?JMenuItem("这是子菜单");??
  54. ????????//定义快捷键??????????menuItem.setAccelerator(KeyStroke.getKeyStroke(??
  55. ????????????????KeyEvent.VK_2,?ActionEvent.ALT_MASK));??????????menuItem.addActionListener(this);??
  56. ????????submenu.add(menuItem);????
  57. ????????menuItem?=?new?JMenuItem("子菜单项");??????????menuItem.addActionListener(this);??
  58. ????????submenu.add(menuItem);??????????menu.add(submenu);??
  59. ??????????//定义第二个菜单??
  60. ????????menu?=?new?JMenu("(N)第二个菜单");??????????menu.setMnemonic(KeyEvent.VK_N);??
  61. ????????menuBar.add(menu);????
  62. ????????return?menuBar;??????}??
  63. /**??*方法说明:构建面板?
  64. ?*输入参数:??*返回类型:?
  65. ?*/??????public?Container?createContentPane()?{??
  66. ????????//构造一个面板??????????JPanel?contentPane?=?new?JPanel(new?BorderLayout());??
  67. ????????contentPane.setOpaque(true);????
  68. ????????//定义一个文本域??????????output?=?new?JTextArea(5,?30);??
  69. ????????output.setEditable(false);??????????scrollPane?=?new?JScrollPane(output);??
  70. ??????????//将文本域添加到面板中??
  71. ????????contentPane.add(scrollPane,?BorderLayout.CENTER);????
  72. ????????return?contentPane;??????}??
  73. /**??*方法说明:构建弹出菜单?
  74. ?*输入参数:??*返回类型:?
  75. ?*/??????public?void?createPopupMenu()?{??
  76. ????????JMenuItem?menuItem;????
  77. ????????//构件弹出菜单??????????JPopupMenu?popup?=?new?JPopupMenu();??
  78. ????????ImageIcon?openicon?=?createImageIcon("images/Open16.gif");??????????menuItem?=?new?JMenuItem("打开文件",openicon);??
  79. ????????menuItem.addActionListener(this);??????????popup.add(menuItem);??
  80. ????????ImageIcon?saveicon?=?createImageIcon("images/Save16.gif");??????????menuItem?=?new?JMenuItem("保存文件",saveicon);??
  81. ????????menuItem.addActionListener(this);??????????popup.add(menuItem);??
  82. ??????????//添加一个监听给文本域,以便点击右键时响应??
  83. ????????MouseListener?popupListener?=?new?PopupListener(popup);??????????output.addMouseListener(popupListener);??
  84. ????}??/**?
  85. ?*方法说明:监听普通的菜单选择??*输入参数:ActionEvent?e?事件?
  86. ?*返回类型:??*/??
  87. ????public?void?actionPerformed(ActionEvent?e)?{??????????JMenuItem?source?=?(JMenuItem)(e.getSource());??
  88. ????????String?s?=?"监测事件。"?????????????????????+?newline??
  89. ???????????????????+?"????事件源:?"?+?source.getText()?????????????????????+?"?(选择对象"?+?getClassName(source)?+?")";??
  90. ????????output.append(s?+?newline);??????}??
  91. /**??*方法说明:监听检查盒菜单选择项?
  92. ?*输入参数:ItemEvent?e?检查盒触发的事件??*返回类型:?
  93. ?*/??????public?void?itemStateChanged(ItemEvent?e)?{??
  94. ????????JMenuItem?source?=?(JMenuItem)(e.getSource());??????????String?s?=?"菜单项监听"??
  95. ???????????????????+?newline?????????????????????+?"????事件源:?"?+?source.getText()??
  96. ???????????????????+?"?(选择对象?"?+?getClassName(source)?+?")"?????????????????????+?newline??
  97. ???????????????????+?"????新的状态:?"?????????????????????+?((e.getStateChange()?==?ItemEvent.SELECTED)????
  98. ?????????????????????"选择":"不选择");??????????output.append(s?+?newline);??
  99. ????}??/**?
  100. ?*方法说明:获得类的名字??*输入参数:?
  101. ?*返回类型:??*/??
  102. ????protected?String?getClassName(Object?o)?{??????????String?classString?=?o.getClass().getName();??
  103. ????????int?dotIndex?=?classString.lastIndexOf(".");??????????return?classString.substring(dotIndex+1);??
  104. ????}??/**?
  105. ?*方法说明:根据路径查找图片??*输入参数:String?path?图片的路径?
  106. ?*返回类型:ImageIcon?图片对象??*/??
  107. ????protected?static?ImageIcon?createImageIcon(String?path)?{??????????java.net.URL?imgURL?=?MenuDemo.class.getResource(path);??
  108. ????????if?(imgURL?!=?null)?{??????????????return?new?ImageIcon(imgURL);??
  109. ????????}?else?{??????????????System.err.println("Couldn't?find?file:?"?+?path);??
  110. ????????????return?null;??????????}??
  111. ????}????
  112. ????public?static?void?main(String[]?args)?{??????????JFrame.setDefaultLookAndFeelDecorated(true);??
  113. ??????????//创建一个窗体??
  114. ????????JFrame?frame?=?new?JFrame("MenuDemo");??????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  115. ??????????//创建菜单,并添加到面板中??
  116. ????????MenuDemo?demo?=?new?MenuDemo();??????????frame.setJMenuBar(demo.createMenuBar());??
  117. ????????frame.setContentPane(demo.createContentPane());????
  118. ????????//生成弹出菜单??????????demo.createPopupMenu();??
  119. ??????????//显示窗体??
  120. ????????frame.setSize(450,?260);??????????frame.setVisible(true);??
  121. ????}??//弹出菜单监听类??
  122. ????class?PopupListener?extends?MouseAdapter?{??????????JPopupMenu?popup;??
  123. ??????????PopupListener(JPopupMenu?popupMenu)?{??
  124. ????????????popup?=?popupMenu;??????????}??
  125. ??????????????????public?void?mousePressed(MouseEvent?e)?{??
  126. ????????????maybeShowPopup(e);??????????}??
  127. ??????????public?void?mouseReleased(MouseEvent?e)?{??
  128. ????????????maybeShowPopup(e);??????????}??
  129. ??????????private?void?maybeShowPopup(MouseEvent?e)?{??
  130. ????????????if?(e.isPopupTrigger())?{??????????????????popup.show(e.getComponent(),??
  131. ???????????????????????????e.getX(),?e.getY());??????????????}??
  132. ????????}??????}??
  133. }??

?

view plain
  1. package?test20;????
  2. import?javax.swing.JToolBar;??import?javax.swing.JButton;??
  3. import?javax.swing.ImageIcon;????
  4. import?javax.swing.JFrame;??import?javax.swing.JTextArea;??
  5. import?javax.swing.JScrollPane;??import?javax.swing.JPanel;??
  6. ??import?java.net.URL;??
  7. ??import?java.awt.*;??
  8. import?java.awt.event.*;??/**?
  9. ?*?Title:?工具栏演示??*?Description:?提供一个工具栏,包括“打开”、“保存”、“搜索”工具按钮?
  10. ?*?Filename:?ToolBarDemo.java??*/??
  11. public?class?ToolBarDemo?extends?JPanel???????????????????????????implements?ActionListener?{??
  12. ??????private?static?final?long?serialVersionUID?=?1L;??
  13. ????protected?JTextArea?textArea;??????protected?String?newline?=?"\n";??
  14. ????static?final?private?String?OPEN?=?"OPEN";??????static?final?private?String?SAVE?=?"SAVE";??
  15. ????static?final?private?String?SEARCH?=?"SEARCH";??/**?
  16. ?*方法说明:构造器??*输入参数:?
  17. ?*返回类型:??*/??
  18. ????public?ToolBarDemo()?{??????????super(new?BorderLayout());??
  19. ??????????//创建工具栏??
  20. ????????JToolBar?toolBar?=?new?JToolBar();??????????addButtons(toolBar);??
  21. ??????????//创建一个文本域,用来输出一些信息??
  22. ????????textArea?=?new?JTextArea(15,?30);??????????textArea.setEditable(false);??
  23. ????????JScrollPane?scrollPane?=?new?JScrollPane(textArea);????
  24. ????????//安放成员??????????setPreferredSize(new?Dimension(450,?110));??
  25. ????????add(toolBar,?BorderLayout.PAGE_START);??????????add(scrollPane,?BorderLayout.CENTER);??
  26. ????}??/**?
  27. ?*方法说明:构建工具栏??*输入参数:JToolBar?toolBar?工具条?
  28. ?*返回类型:??*/??
  29. ????protected?void?addButtons(JToolBar?toolBar)?{??????????JButton?button?=?null;??
  30. ??????????//第一个按钮,“打开”??
  31. ????????button?=?makeNavigationButton("Open16",?OPEN,????????????????????????????????????????"打开一个文件!",??
  32. ??????????????????????????????????????"打开");??????????toolBar.add(button);??
  33. ??????????//第二个按钮,“保存”??
  34. ????????button?=?makeNavigationButton("Save16",?SAVE,????????????????????????????????????????"保存当前文件!",??
  35. ??????????????????????????????????????"保存");??????????toolBar.add(button);??
  36. ??????????//第三个按钮,“搜索”??
  37. ????????button?=?makeNavigationButton("Search16",?SEARCH,????????????????????????????????????????"搜索文件中的字符!",??
  38. ??????????????????????????????????????"搜索");??????????toolBar.add(button);??
  39. ????}??/**?
  40. ?*方法说明:构造工具栏上的按钮??*输入参数:?
  41. ?*返回类型:??*/??
  42. ????protected?JButton?makeNavigationButton(String?imageName,?????????????????????????????????????????????String?actionCommand,??
  43. ???????????????????????????????????????????String?toolTipText,?????????????????????????????????????????????String?altText)?{??
  44. ????????//搜索图片??????????String?imgLocation?=?"images/"??
  45. ?????????????????????????????+?imageName???????????????????????????????+?".gif";??
  46. ????????URL?imageURL?=?ToolBarDemo.class.getResource(imgLocation);????
  47. ????????//初始化工具按钮??????????JButton?button?=?new?JButton();??
  48. ????????//设置按钮的命令??????????button.setActionCommand(actionCommand);??
  49. ????????//设置提示信息??????????button.setToolTipText(toolTipText);??
  50. ????????button.addActionListener(this);????????????
  51. ????????if?(imageURL?!=?null)?{??????????????????????//找到图像??????????????button.setIcon(new?ImageIcon(imageURL));??
  52. ????????}?else?{?????????????????????????????????????//没有图像??????????????button.setText(altText);??
  53. ????????????System.err.println("Resource?not?found:?"?????????????????????????????????+?imgLocation);??
  54. ????????}????
  55. ????????return?button;??????}??
  56. /**??*方法说明:事件监听?
  57. ?*输入参数:??*返回类型:?
  58. ?*/??????public?void?actionPerformed(ActionEvent?e)?{??
  59. ????????String?cmd?=?e.getActionCommand();??????????String?description?=?null;??
  60. ??????????if?(OPEN.equals(cmd))?{?//点击第一个按钮??
  61. ????????????description?=?"打开一个文件操作!";??????????}?else?if?(SAVE.equals(cmd))?{?//点击第二个按钮??
  62. ????????????description?=?"保存文件操作";??????????}?else?if?(SEARCH.equals(cmd))?{?//点击第三个按钮??
  63. ????????????description?=?"搜索字符操作";??????????}??
  64. ??????????displayResult("如果这里是真正的程序,你将进入:?"??
  65. ????????????????????????+?description);??????}??
  66. ??????protected?void?displayResult(String?actionDescription)?{??
  67. ????????textArea.append(actionDescription?+?newline);??????}??
  68. ??????public?static?void?main(String[]?args)?{??
  69. ????????JFrame.setDefaultLookAndFeelDecorated(true);????
  70. ????????//定义窗体??????????JFrame?frame?=?new?JFrame("ToolBarDemo");??
  71. ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);????
  72. ????????//定义面板??????????ToolBarDemo?newContentPane?=?new?ToolBarDemo();??
  73. ????????newContentPane.setOpaque(true);??????????frame.setContentPane(newContentPane);??
  74. ??????????//显示窗体??
  75. ????????frame.pack();??????????frame.setVisible(true);??
  76. ????}??}?

热点排行