读书人

Swing带关闭按钮的JTabelpane成效

发布时间: 2012-12-22 12:05:06 作者: rapoo

Swing带关闭按钮的JTabelpane效果

?

Swing中有JTabelPane也签面板可以使用,但是面板不带有关闭按钮,为了模仿eclipse里面打开代码窗口关闭的效果,需要在JTabelpane上面也加一个关闭的"X"效果。由于自己Swing水平还比较有限,于是到网上找了一个开源的,开源Swing控件的名称为OpenSwing.OpenSwing对Swing常用的一些Swing空间做了封装,使用起来还是比较方便的。如果您对Swing有兴趣也可以参考OpenSwing的实现自己做几个方便自己用。OpenSwing自带的JCloseableTabbedPane带的关闭按钮带有立体效果。我略作了修改将立体效果去掉了。

1、没修改前效果


Swing带关闭按钮的JTabelpane成效

2、Nimbus样式效果,还是Nimbus样式好看


Swing带关闭按钮的JTabelpane成效

3、第二个页签"X"显示和其他不一样

这里的效果是有代码中的

tab.setIconDrawCenter(1, false);

?

这句话设置的,如果想和其他效果一样,注释掉该行代码即可。

4、最后效果样式


Swing带关闭按钮的JTabelpane成效

?

5、代码如下:

package gui;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;/** * <p>Title: OpenSwing</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a> * @version 1.0 */public class JCloseableTabbedPane extends JTabbedPane implements Serializable{    public static final String ON_TAB_CLOSE = "ON_TAB_CLOSE";    public static final String ON_TAB_DOUBLECLICK = "ON_TAB_DOUBLECLICK";    private JPopupMenu popup = null;    public JCloseableTabbedPane(){        super();        init();    }    public JCloseableTabbedPane(int tabPlacement){        super(tabPlacement);        init();    }    public JCloseableTabbedPane(int tabPlacement, int tabLayoutPolicy){        super(tabPlacement, tabLayoutPolicy);        init();    }    protected void init(){        addMouseListener(new DefaultMouseAdapter());    }    public void setPopup(JPopupMenu popup){        this.popup = popup;    }    public void setIconDrawCenter(int index, boolean drawCenter){        ((CloseIcon)getIconAt(index)).setDrawCenter(drawCenter);        repaint();    }    public JPopupMenu getPopup(){        return popup;    }    public boolean isDrawCenter(int index){        return((CloseIcon)getIconAt(index)).isDrawCenter();    }    protected EventListenerList closeListenerList = new EventListenerList();    public void addCloseListener(ActionListener l){        closeListenerList.add(ActionListener.class, l);    }    public void removeCloseListener(ActionListener l){        closeListenerList.remove(ActionListener.class, l);    }    protected void fireClosed(ActionEvent e){        Object[] listeners = closeListenerList.getListenerList();        for(int i = listeners.length - 2; i >= 0; i -= 2){            if(listeners[i] == ActionListener.class){                ((ActionListener)listeners[i + 1]).actionPerformed(e);            }        }    }    class DefaultMouseAdapter extends MouseAdapter{        CloseIcon icon;        public void mousePressed(MouseEvent e){            int index = indexAtLocation(e.getX(), e.getY());            if(index != -1){                icon = (CloseIcon)getIconAt(index);                if(icon.getBounds().contains(e.getPoint())){                    icon.setPressed(true);                    fireClosed(new ActionEvent(                        e.getComponent(),                        ActionEvent.ACTION_PERFORMED,                        ON_TAB_CLOSE));                } else if(e.getClickCount() == 2){                    fireClosed(new ActionEvent(                        e.getComponent(),                        ActionEvent.ACTION_PERFORMED,                        ON_TAB_DOUBLECLICK));                }            }        }        @Override        public void mouseEntered(MouseEvent e) {         int index = indexAtLocation(e.getX(), e.getY());             if(index != -1){                 icon = (CloseIcon)getIconAt(index);                 if(icon.getBounds().contains(e.getPoint())){                 }             }        }        public void mouseReleased(MouseEvent e){            if(icon != null){                icon.setPressed(false);                icon = null;                repaint();            }            if(popup != null){                if(!SwingUtilities.isRightMouseButton(e)){                    return;                }                if(indexAtLocation(e.getX(), e.getY()) != -1){                    popup.show(e.getComponent(), e.getX(), e.getY());                }            }        }    }    public Icon getIconAt(int index){        Icon icon = super.getIconAt(index);        if(icon == null || !(icon instanceof CloseIcon)){            super.setIconAt(index, new CloseIcon());        }        return super.getIconAt(index);    }    class CloseIcon implements Icon{        Rectangle rec = new Rectangle(0, 0, 15, 16);        private boolean pressed = false;        private boolean drawCenter = true;        public synchronized void paintIcon(            Component c, Graphics g, int x1, int y1){            int x = x1+5, y = y1;            if(pressed){                x++;                y++;            }            rec.x = x;            rec.y = y;            Color oldColor =Color.WHITE;            //去掉按钮立体形状/*            g.setColor(UIManager.getColor("TabbedPane.highlight"));            g.drawLine(x, y, x, y + rec.height);            g.drawLine(x, y, x + rec.width, y);            g.setColor(UIManager.getColor("TabbedPane.shadow"));            g.drawLine(x, y + rec.height, x + rec.width, y + rec.height);            g.drawLine(x + rec.width, y, x + rec.width, y + rec.height);            g.setColor(UIManager.getColor("TabbedPane.foreground"));*/            //draw X            //left top            g.drawRect(x + 4, y + 4, 1, 1);            g.drawRect(x + 5, y + 5, 1, 1);            g.drawRect(x + 5, y + 9, 1, 1);            g.drawRect(x + 4, y + 10, 1, 1);            //center            if(drawCenter){                g.drawRect(x + 6, y + 6, 1, 1);                g.drawRect(x + 8, y + 6, 1, 1);                g.drawRect(x + 6, y + 8, 1, 1);                g.drawRect(x + 8, y + 8, 1, 1);            }            //right top            g.drawRect(x + 10, y + 4, 1, 1);            g.drawRect(x + 9, y + 5, 1, 1);            //right bottom            g.drawRect(x + 9, y + 9, 1, 1);            g.drawRect(x + 10, y + 10, 1, 1);            g.setColor(oldColor);        }        private void drawRec(Graphics g, int x, int y){            g.drawRect(x, y, 1, 1);        }        public Rectangle getBounds(){            return rec;        }        public void setBounds(Rectangle rec){            this.rec = rec;        }        public int getIconWidth(){            return rec.width;        }        public int getIconHeight(){            return rec.height;        }        public void setPressed(boolean pressed){            this.pressed = pressed;        }        public void setDrawCenter(boolean drawCenter){            this.drawCenter = drawCenter;        }        public boolean isPressed(){            return pressed;        }        public boolean isDrawCenter(){            return drawCenter;        }    };    /**     * 测试     * @param args String[]     */    public static void main(String[] args){        try {            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");        }        catch (Exception e) {        }        JFrame frame = new JFrame();        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setTitle("JCloseableTabbedPane Demo");        frame.getContentPane().setLayout(new BorderLayout());        final JCloseableTabbedPane tab = new JCloseableTabbedPane();        tab.add(new JPanel(), "TabbedPane");        tab.add(new JPanel(), "Has");        tab.add(new JPanel(), "Popup");        tab.add(new JPanel(), "PopupMenu");//        tab.setIconDrawCenter(1, false);                //添加关闭按钮事件        tab.addCloseListener(new ActionListener(){            public void actionPerformed(ActionEvent e){                if(e.getActionCommand().equals(tab.ON_TAB_CLOSE)){                    tab.removeTabAt(tab.getSelectedIndex());                }            }        });        //设置弹出菜单        JPopupMenu menu = new JPopupMenu();        for(int i = 0; i < 10; i++){            menu.add(new JMenuItem("item "+i));        }        tab.setPopup(menu);        frame.getContentPane().add(tab, BorderLayout.CENTER);        frame.setSize(400, 320);        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();        frame.setLocation((d.width - frame.getSize().width) / 2,                          (d.height - frame.getSize().height) / 2);        frame.setVisible(true);    }}
?

?

慢慢研究Swing还是很强大的,很多电信级别的网管软件就都是Swing做出来的。

?

同时发布在其他Blog。

1 楼 cclsw07 2011-01-24 再做个右键类似eclipse上右键的功能~~

读书人网 >编程

热点推荐