云代码会员 - 云代码空间
——
直接实例化就可以使用,一些swing控件的改进的:
[java:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
1. /*
2. * Arjick@163.com
3. *
4. */
5. package exec;
6. import javax.swing.*;
7. import java.awt.*;
8. import java.awt.event.*;
9. import java.awt.geom.AffineTransform;
10. import java.util.ArrayList;
11. import java.util.HashMap;
12. /**
13. * A JTabbedPane which has a close ('X') icon on each tab.
14. * To add a tab, use the method addTab(String, Component)
15. * To have an extra icon on each tab (e.g. like in JBuilder,
16. * showing the file type) use the method
17. * addTab(String, Component, Icon).
18. * Only clicking the 'X' closes the tab. */
19. public class JClosableTabbedPane extends JTabbedPane implements MouseListener {
20. private double scaleRatio = 0.3;
21. private HashMap<String, Component> maps = new HashMap<String, Component>();
22. public JClosableTabbedPane() {
23. super();
24. addMouseListener(this);
25. }
26. public void addTab(String title, Component component) {
27. this.addTab(title, component, null);
28. }
29. public void addTab(String title, Component component, Icon extraIcon) {
30. super.addTab(title, new CloseTabIcon(extraIcon), component);
31. }
32. public void insertTab(String title, Icon icon, Component component, String tip, int index) {
33. tip = "tab" + component.hashCode();
34. maps.put(tip, component);
35. super.insertTab(title, icon, component, tip, index);
36. }
37. public void removeTabAt(int index) {
38. Component component = getComponentAt(index);
39. maps.remove("tab" + component.hashCode());
40. super.removeTabAt(index);
41. }
42. public JToolTip createToolTip() {
43. ImageToolTip tooltip = new ImageToolTip();
44. tooltip.setComponent(this);
45. return tooltip;
46. }
47. class ImageToolTip extends JToolTip {
48. public Dimension getPreferredSize() {
49. String tip = getTipText();
50. Component component = maps.get(tip);
51. if (component != null) {
52. return new Dimension((int) (getScaleRatio() * component.getWidth()), (int) (getScaleRatio() * component.getHeight()));
53. } else {
54. return super.getPreferredSize();
55. }
56. }
57. public void paintComponent(Graphics g) {
58. String tip = getTipText();
59. Component component = maps.get(tip);
60. if (component instanceof JComponent) {
61. JComponent jcomponent = (JComponent) component;
62. Graphics2D g2d = (Graphics2D) g;
63. AffineTransform at = g2d.getTransform();
64. g2d.transform(AffineTransform.getScaleInstance(getScaleRatio(), getScaleRatio()));
65. ArrayList<JComponent> dbcomponents = new ArrayList<JComponent>();
66. updateDoubleBuffered(jcomponent, dbcomponents);
67. jcomponent.paint(g);
68. resetDoubleBuffered(dbcomponents);
69. g2d.setTransform(at);
70. }
71. }
72. private void updateDoubleBuffered(JComponent component, ArrayList<JComponent> dbcomponents) {
73. if (component.isDoubleBuffered()) {
74. dbcomponents.add(component);
75. component.setDoubleBuffered(false);
76. }
77. for (int i = 0; i < component.getComponentCount(); i++) {
78. Component c = component.getComponent(i);
79. if (c instanceof JComponent) {
80. updateDoubleBuffered((JComponent) c, dbcomponents);
81. }
82. }
83. }
84. private void resetDoubleBuffered(ArrayList<JComponent> dbcomponents) {
85. for (JComponent component : dbcomponents) {
86. component.setDoubleBuffered(true);
87. }
88. }
89. }
90. public double getScaleRatio() {
91. return scaleRatio;
92. }
93. public void setScaleRatio(double scaleRatio) {
94. this.scaleRatio = scaleRatio;
95. }
96. public void mouseClicked(MouseEvent e) {
97. int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
98. if (tabNumber < 0) {
99. return;
100. }
101. Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
102. if (rect.contains(e.getX(), e.getY())) {
103. //the tab is being closed
104. this.removeTabAt(tabNumber);
105. }
106. }
107. public void mouseEntered(MouseEvent e) {
108. }
109. public void mouseExited(MouseEvent e) {
110. }
111. public void mousePressed(MouseEvent e) {
112. }
113. public void mouseReleased(MouseEvent e) {
114. }
115. }
116. /**
117. * The class which generates the 'X' icon for the tabs. The constructor
118. * accepts an icon which is extra to the 'X' icon, so you can have tabs
119. * like in JBuilder. This value is null if no extra icon is required.
120. */
121. class CloseTabIcon implements Icon {
122. private int x_pos;
123. private int y_pos;
124. private int width;
125. private int height;
126. private Icon fileIcon;
127. public CloseTabIcon(Icon fileIcon) {
128. this.fileIcon = fileIcon;
129. width = 16;
130. height = 16;
131. }
132. public void paintIcon(Component c, Graphics g, int x, int y) {
133. this.x_pos = x;
134. this.y_pos = y;
135. Color col = g.getColor();
136. g.setColor(Color.black);
137. int y_p = y + 2;
138. g.drawLine(x + 1, y_p, x + 12, y_p);
139. g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
140. g.drawLine(x, y_p + 1, x, y_p + 12);
141. g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
142. g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
143. g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
144. g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
145. g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
146. g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
147. g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
148. g.setColor(col);
149. if (fileIcon != null) {
150. fileIcon.paintIcon(c, g, x + width, y_p);
151. }
152. }
153. public int getIconWidth() {
154. return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
155. }
156. public int getIconHeight() {
157. return height;
158. }
159. public Rectangle getBounds() {
160. return new Rectangle(x_pos, y_pos, width, height);
161. }
162. /*public static void main(String args[]) {
163. try {
164. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
165. }
166. catch (Exception e) {
167. e.printStackTrace();
168. }
169. JClosableTabbedPane pane = new JClosableTabbedPane();
170. ImageIcon icon = new ImageIcon("images/middle.jpg");
171. pane.addTab("tab1",new JButton("first Button"),icon);
172. pane.addTab("tab2",new JButton("sec Button"),icon);
173. pane.addTab("tab3",new JButton("third Button"),icon);
174. pane.addTab("tab4",new JButton("fourth Button"),icon);
175. JFrame frame = new JFrame("Demo");
176. frame.getContentPane().add(pane,BorderLayout.CENTER);
177. frame.setSize(500,300);
178. frame.setLocation(300,200);
179. frame.show();
180. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
181. }*/
182. }
· 上一篇:JAVA加入windows系统服务(Java Service Wrapper)
· 下一篇:JAVA分析html算法(JAVA网页蜘蛛算法)