JTabbedPane添加關閉按鈕和縮圖

來源:互聯網
上載者:User

  直接執行個體化就可以使用,一些swing控制項的改進的:

  /*<br />* Arjick@163.com<br />*<br />*/<br />package exec;<br />import javax.swing.*;<br />import java.awt.*;<br />import java.awt.event.*;<br />import java.awt.geom.AffineTransform;<br />import java.util.ArrayList;<br />import java.util.HashMap;<br />/**<br />* A JTabbedPane which has a close ('X') icon on each tab.<br />* To add a tab, use the method addTab(String, Component)<br />* To have an extra icon on each tab (e.g. like in JBuilder,<br />* showing the file type) use the method<br />* addTab(String, Component, Icon).<br />* Only clicking the 'X' closes the tab. */<br />public class JClosableTabbedPane extends JTabbedPane implements MouseListener {<br /> private double scaleRatio = 0.3;<br /> private HashMap<String, Component> maps = new HashMap<String, Component>();<br /> public JClosableTabbedPane() {<br /> super();<br /> addMouseListener(this);<br /> }<br /> public void addTab(String title, Component component) {<br /> this.addTab(title, component, null);<br /> }<br /> public void addTab(String title, Component component, Icon extraIcon) {<br /> super.addTab(title, new CloseTabIcon(extraIcon), component);<br /> }<br /> public void insertTab(String title, Icon icon, Component component, String tip, int index) {<br /> tip = "tab" + component.hashCode();<br /> maps.put(tip, component);<br /> super.insertTab(title, icon, component, tip, index);<br /> }<br /> public void removeTabAt(int index) {<br /> Component component = getComponentAt(index);<br /> maps.remove("tab" + component.hashCode());<br /> super.removeTabAt(index);<br /> }<br /> public JToolTip createToolTip() {<br /> ImageToolTip tooltip = new ImageToolTip();<br /> tooltip.setComponent(this);<br /> return tooltip;<br /> }<br /> class ImageToolTip extends JToolTip {<br /> public Dimension getPreferredSize() {<br /> String tip = getTipText();<br /> Component component = maps.get(tip);<br /> if (component != null) {<br /> return new Dimension((int) (getScaleRatio() * component.getWidth()), (int) (getScaleRatio() * component.getHeight()));<br /> } else {<br /> return super.getPreferredSize();<br /> }<br /> }<br /> public void paintComponent(Graphics g) {<br /> String tip = getTipText();<br /> Component component = maps.get(tip);<br /> if (component instanceof JComponent) {<br /> JComponent jcomponent = (JComponent) component;<br /> Graphics2D g2d = (Graphics2D) g;<br /> AffineTransform at = g2d.getTransform();<br /> g2d.transform(AffineTransform.getScaleInstance(getScaleRatio(), getScaleRatio()));<br /> ArrayList<JComponent> dbcomponents = new ArrayList<JComponent>();<br /> updateDoubleBuffered(jcomponent, dbcomponents);<br /> jcomponent.paint(g);<br /> resetDoubleBuffered(dbcomponents);<br /> g2d.setTransform(at);<br /> }<br /> }<br /> private void updateDoubleBuffered(JComponent component, ArrayList<JComponent> dbcomponents) {<br /> if (component.isDoubleBuffered()) {<br /> dbcomponents.add(component);<br /> component.setDoubleBuffered(false);<br /> }<br /> for (int i = 0; i < component.getComponentCount(); i++) {<br /> Component c = component.getComponent(i);<br /> if (c instanceof JComponent) {<br /> updateDoubleBuffered((JComponent) c, dbcomponents);<br /> }<br /> }<br /> }<br /> private void resetDoubleBuffered(ArrayList<JComponent> dbcomponents) {<br /> for (JComponent component : dbcomponents) {<br /> component.setDoubleBuffered(true);<br /> }<br /> }<br /> }<br /> public double getScaleRatio() {<br /> return scaleRatio;<br /> }<br /> public void setScaleRatio(double scaleRatio) {<br /> this.scaleRatio = scaleRatio;<br /> }<br /> public void mouseClicked(MouseEvent e) {<br /> int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());<br /> if (tabNumber < 0) {<br /> return;<br /> }<br /> Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();<br /> if (rect.contains(e.getX(), e.getY())) {<br /> //the tab is being closed<br /> this.removeTabAt(tabNumber);<br /> }<br /> }<br /> public void mouseEntered(MouseEvent e) {<br /> }<br /> public void mouseExited(MouseEvent e) {<br /> }<br /> public void mousePressed(MouseEvent e) {<br /> }<br /> public void mouseReleased(MouseEvent e) {<br /> }<br />}<br />/**<br />* The class which generates the 'X' icon for the tabs. The constructor<br />* accepts an icon which is extra to the 'X' icon, so you can have tabs<br />* like in JBuilder. This value is null if no extra icon is required.<br />*/<br />class CloseTabIcon implements Icon {<br /> private int x_pos;<br /> private int y_pos;<br /> private int width;<br /> private int height;<br /> private Icon fileIcon;<br /> public CloseTabIcon(Icon fileIcon) {<br /> this.fileIcon = fileIcon;<br /> width = 16;<br /> height = 16;<br /> }<br /> public void paintIcon(Component c, Graphics g, int x, int y) {<br /> this.x_pos = x;<br /> this.y_pos = y;<br /> Color col = g.getColor();<br /> g.setColor(Color.black);<br /> int y_p = y + 2;<br /> g.drawLine(x + 1, y_p, x + 12, y_p);<br /> g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);<br /> g.drawLine(x, y_p + 1, x, y_p + 12);<br /> g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);<br /> g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);<br /> g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);<br /> g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);<br /> g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);<br /> g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);<br /> g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);<br /> g.setColor(col);<br /> if (fileIcon != null) {<br /> fileIcon.paintIcon(c, g, x + width, y_p);<br /> }<br /> }<br /> public int getIconWidth() {<br /> return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);<br /> }<br /> public int getIconHeight() {<br /> return height;<br /> }<br /> public Rectangle getBounds() {<br /> return new Rectangle(x_pos, y_pos, width, height);<br /> }<br /> /*public static void main(String args[]) {<br /> try {<br /> UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());<br /> }<br /> catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> JClosableTabbedPane pane = new JClosableTabbedPane();<br /> ImageIcon icon = new ImageIcon("images/middle.jpg");<br /> pane.addTab("tab1",new JButton("first Button"),icon);<br /> pane.addTab("tab2",new JButton("sec Button"),icon);<br /> pane.addTab("tab3",new JButton("third Button"),icon);<br /> pane.addTab("tab4",new JButton("fourth Button"),icon);<br /> JFrame frame = new JFrame("Demo");<br /> frame.getContentPane().add(pane,BorderLayout.CENTER);<br /> frame.setSize(500,300);<br /> frame.setLocation(300,200);<br /> frame.show();<br /> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br /> }*/<br />}<br /> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.