1. package org.jr.swing;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月15日
  6. * @author Cherami
  7. */
  8. import javax.swing.*;
  9. /**
  10. * 可以动态管理按钮的文本、提示信息以及图像的显示状态的按钮组件。
  11. * @since 0.3
  12. */
  13. public class JDynamicButton
  14. extends JButton {
  15. String text = "";
  16. String tooltipText;
  17. Icon icon;
  18. /**
  19. * 构造一个空白的JDynamicButton。
  20. * @since 0.3
  21. */
  22. public JDynamicButton() {
  23. }
  24. /**
  25. * 构造一个具有图像的JDynamicButton。
  26. * @param icon 图像
  27. * @since 0.3
  28. */
  29. public JDynamicButton(Icon icon) {
  30. super(icon);
  31. this.icon = icon;
  32. }
  33. /**
  34. * 构造一个具有按钮文本的JDynamicButton。
  35. * @param text 按钮文本
  36. * @since 0.4
  37. */
  38. public JDynamicButton(String text) {
  39. super(text);
  40. this.text = text;
  41. }
  42. /**
  43. * 构造一个具有按钮文本和提示信息的JDynamicButton。
  44. * @param text 按钮文本
  45. * @param tooltipText 提示信息
  46. * @since 0.4
  47. */
  48. public JDynamicButton(String text, String tooltipText) {
  49. super(text);
  50. this.text = text;
  51. setToolTipText(tooltipText);
  52. }
  53. /**
  54. * 构造一个具有文本和图像的JDynamicButton。
  55. * @param text 显示文本
  56. * @param icon 图像
  57. * @since 0.3
  58. */
  59. public JDynamicButton(String text, Icon icon) {
  60. super(text, icon);
  61. this.text = text;
  62. this.icon = icon;
  63. }
  64. /**
  65. * 构造一个具有文本、提示信息和图像的JDynamicButton。
  66. * @param text 显示文本
  67. * @param tooltipText 提示信息
  68. * @param icon 图像
  69. * @since 0.3
  70. */
  71. public JDynamicButton(String text, String tooltipText, Icon icon) {
  72. super(text, icon);
  73. this.text = text;
  74. this.icon = icon;
  75. setToolTipText(tooltipText);
  76. }
  77. /**
  78. * 设置按钮的显示文本。
  79. * 如果原来的显示文本的显示模式被设置为不显示,那么重新设置以后将会把新的文本显示出来。
  80. * @param text 显示文本
  81. * @since 0.3
  82. */
  83. public void setText(String text) {
  84. this.text = text;
  85. showText(true);
  86. }
  87. /**
  88. * 设置按钮的提示信息。
  89. * 如果原来的提示信息的显示模式被设置为不显示,那么重新设置以后将会把新的提示信息显示出来。
  90. * @param tooltipText 提示信息
  91. * @since 0.3
  92. */
  93. public void setToolTipText(String tooltipText) {
  94. this.tooltipText = tooltipText;
  95. showToolTipText(true);
  96. }
  97. /**
  98. * 设置按钮的图像。
  99. * 如果原来的图像的显示模式被设置为不显示,那么重新设置以后将会把新的图像显示出来。
  100. * @param defaultIcon 正常状态下的缺省图像
  101. * @since 0.3
  102. */
  103. public void setIcon(Icon defaultIcon) {
  104. this.icon = defaultIcon;
  105. showIcon(true);
  106. }
  107. /**
  108. * 设置是否显示文本
  109. * @param show true的时候显示,否则不显示。
  110. * @since 0.3
  111. */
  112. public void showText(boolean show) {
  113. if (show == true) {
  114. super.setText(text);
  115. }
  116. else {
  117. super.setText("");
  118. }
  119. }
  120. /**
  121. * 设置是否显示提示信息。
  122. * @param show true的时候显示,否则不显示。
  123. * @since 0.3
  124. */
  125. public void showToolTipText(boolean show) {
  126. if (show == true) {
  127. super.setToolTipText(tooltipText);
  128. }
  129. else {
  130. super.setToolTipText(null);
  131. }
  132. }
  133. /**
  134. * 设置图像是否显示。
  135. * @param show true的时候显示,否则不显示。
  136. * @since 0.3
  137. */
  138. public void showIcon(boolean show) {
  139. if (show == true) {
  140. super.setIcon(icon);
  141. }
  142. else {
  143. super.setIcon(null);
  144. }
  145. }
  146. }