- 浏览: 185055 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
wanglijunjsj:
谢谢,很有用
java log4j的一些总结 -
lxb_champagne:
this.init(); 类都没初始化好,this没用的。
java final变量的初始化问题 -
lg_asus:
上面代码有点小问题,最新代码:
public class Pr ...
判断素数 -
lg_asus:
测试10 million的以内的数据,算出所有素数时间在500 ...
判断素数 -
lg_asus:
文章中说错了:如果只是找一个数在不在其中,则可以直接遍历一次, ...
40亿不重复的正整数,如何判断一个数是否在其中
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.util.ArrayList; import java.util.HashMap; /** * A JTabbedPane which has a close ('X') icon on each tab. * To add a tab, use the method addTab(String, Component) * To have an extra icon on each tab (e.g. like in JBuilder, * showing the file type) use the method * addTab(String, Component, Icon). * Only clicking the 'X' closes the tab. */ public class ClosedTabPanel extends JTabbedPane implements MouseListener { private double scaleRatio = 0.3; private HashMap<String, Component> maps = new HashMap<String, Component>(); public ClosedTabPanel() { super(); addMouseListener(this); } public static void main(String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } ClosedTabPanel pane = new ClosedTabPanel(); //ImageIcon icon = new ImageIcon(".\\Icons\\home.jpg"); ImageIcon icon = null; pane.addTab("tab1",new JButton("first Button"),icon); pane.addTab("tab2",new JButton("sec Button"),icon); pane.addTab("tab3",new JButton("third Button"),icon); pane.addTab("tab4",new JButton("fourth Button"),icon); JFrame frame = new JFrame("Demo"); frame.getContentPane().add(pane,BorderLayout.CENTER); frame.setSize(500,300); frame.setLocation(300,200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void addTab(String title, Component component) { this.addTab(title, component, null); } public void addTab(String title, Component component, Icon extraIcon) { super.addTab(title, new CloseTabIcon(extraIcon), component); } public void insertTab(String title, Icon icon, Component component, String tip, int index) { tip = "tab" + component.hashCode(); maps.put(tip, component); super.insertTab(title, icon, component, tip, index); } public void removeTabAt(int index) { Component component = getComponentAt(index); maps.remove("tab" + component.hashCode()); super.removeTabAt(index); } public JToolTip createToolTip() { ImageToolTip tooltip = new ImageToolTip(); tooltip.setComponent(this); return tooltip; } class ImageToolTip extends JToolTip { public Dimension getPreferredSize() { String tip = getTipText(); Component component = maps.get(tip); if (component != null) { return new Dimension((int) (getScaleRatio() * component.getWidth()), (int) (getScaleRatio() * component.getHeight())); } else { return super.getPreferredSize(); } } public void paintComponent(Graphics g) { String tip = getTipText(); Component component = maps.get(tip); if (component instanceof JComponent) { JComponent jcomponent = (JComponent) component; Graphics2D g2d = (Graphics2D) g; AffineTransform at = g2d.getTransform(); g2d.transform(AffineTransform.getScaleInstance(getScaleRatio(), getScaleRatio())); ArrayList<JComponent> dbcomponents = new ArrayList<JComponent>(); updateDoubleBuffered(jcomponent, dbcomponents); jcomponent.paint(g); resetDoubleBuffered(dbcomponents); g2d.setTransform(at); } } private void updateDoubleBuffered(JComponent component, ArrayList<JComponent> dbcomponents) { if (component.isDoubleBuffered()) { dbcomponents.add(component); component.setDoubleBuffered(false); } for (int i = 0; i < component.getComponentCount(); i++) { Component c = component.getComponent(i); if (c instanceof JComponent) { updateDoubleBuffered((JComponent) c, dbcomponents); } } } private void resetDoubleBuffered(ArrayList<JComponent> dbcomponents) { for (JComponent component : dbcomponents) { component.setDoubleBuffered(true); } } } public double getScaleRatio() { return scaleRatio; } public void setScaleRatio(double scaleRatio) { this.scaleRatio = scaleRatio; } public void mouseClicked(MouseEvent e) { int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY()); if (tabNumber < 0) { return; } Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds(); if (rect.contains(e.getX(), e.getY())) { //the tab is being closed this.removeTabAt(tabNumber); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } } /** * The class which generates the 'X' icon for the tabs. The constructor * accepts an icon which is extra to the 'X' icon, so you can have tabs * like in JBuilder. This value is null if no extra icon is required. */ class CloseTabIcon implements Icon { private int x_pos; private int y_pos; private int width; private int height; private Icon fileIcon; public CloseTabIcon(Icon fileIcon) { this.fileIcon = fileIcon; width = 16; height = 16; } public void paintIcon(Component c, Graphics g, int x, int y) { this.x_pos = x; this.y_pos = y; Color col = g.getColor(); g.setColor(new Color(250,100,100)); int y_p = y + 2; g.drawLine(x + 1, y_p, x + 12, y_p); g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13); g.drawLine(x, y_p + 1, x, y_p + 12); g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12); g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10); g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10); g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9); g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10); g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10); g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9); g.setColor(col); if (fileIcon != null) { fileIcon.paintIcon(c, g, x + width, y_p); } } public int getIconWidth() { return width + (fileIcon != null ? fileIcon.getIconWidth() : 0); } public int getIconHeight() { return height; } public Rectangle getBounds() { return new Rectangle(x_pos, y_pos, width, height); } }
轉:http://blog.csdn.net/arjick/archive/2009/09/07/4526692.aspx
发表评论
-
swt tree demo with a text editor
2012-12-25 16:41 1450项目需要,从XML中读取数据生成一个Tree,然后可以对叶子节 ... -
Paint in AWT andSwing
2012-04-20 21:32 0jdk 1.0中用的是AWT,有一个缺点,就是每个组件要和一个 ... -
SwingUtilities的invokeLater和invokeAndWait
2012-02-28 18:00 1115参考: http://blog.csdn.net/bzwm/a ... -
swing第三方jar增强功能
2012-02-14 16:34 2472原文:http://hb.qq.com/a/20101220/ ... -
swing 日期组件
2012-02-03 15:35 1121/** * chega * 2011-9-21上午 ... -
java swing全屏显示方式
2012-02-01 11:01 4997覆盖任务栏和不覆盖任务栏见:http://blog.csdn. ... -
带有行标题的JTable
2012-01-06 15:59 1173/** * @author chega * thi ... -
JColorChooser
2010-07-18 21:25 1122import java.awt.*; import ja ... -
JFileChooser
2010-07-18 20:17 1194import java.awt.*; import ja ... -
AbstractAction
2010-07-18 10:00 1776import java.awt.*; import ja ... -
JMenuItem和JTollBar
2010-07-18 09:34 1443import java.awt.*; import ja ... -
JTree
2010-07-17 16:41 1154import java.awt.*; import ja ... -
JTree
2010-07-17 14:58 992import java.awt.*; import ja ... -
JTree
2010-07-17 13:17 1006import java.awt.*; import ja ... -
UndoableEditListener 和 DocumentListener
2010-07-15 21:24 1295import java.awt.*; import ja ... -
JTable增刪行列
2010-07-14 19:57 1421import java.awt.*; import ja ... -
JTable背景色與Boolean類型數據顯示衝突的問題
2010-07-12 16:53 904import java.awt.*; import java ... -
JComboBox每個Item加圖標
2010-07-11 16:46 1268import java.awt.*; import java ... -
JComboBox
2010-07-11 16:33 1276JComboBox(ComboModel):可以繼承Abstr ... -
JList雙向列表
2010-07-11 15:43 784import java.awt.*; import java ...
相关推荐
一个通过自定义JPanel来实现关闭按钮的JTabbedPane,因为使用了适配器去继承父类的方法,所以代码比较多但功能一般
带关闭按钮可定制的JTabbedPane,首先解决了java自带的UI丑的不行的问题,然后附带了可关闭按钮,这个可关闭按钮,可以在添加标签时,自定义是否出现.该资源属于网络资源,谢谢作者的无私奉献.象征性的收1点积分,因为我的...
Java中的JTabbedPane无自带关闭按钮,Netbeans的jar包中实现了自带关闭功能,比自己编写控件方便多了。
Netbeans中的TabbedContainer容器,解决了Swing中jTabbedPane需手动设置关闭按钮,可以轻松设置,类似于Netbeans主窗口的布局。
标签化窗格:JTabbedPane 拆分窗格:JSplitPane 滚动窗格:JScrollPane 工具栏:JToolBar 桌面窗格:JDesktopPane 内部框架:JInternalFrame 分层窗格:JLayeredPane 标签:JLabel 按钮:JButton 开启/关闭按钮:...
加入了好多人在CJW论坛上贴子中想要的带关闭按钮的JCloseableTabbedPane可关闭的JTabbedPane的组件 2005/06/21 对JDatePicker加入了时分秒的输入 2006/01/20 修正了JPopupButton在XP风格下呈两个按钮样子的BUG 对...
JCloseableTabbedPane 带关闭按钮/可设置菜单的JTabbedPane JDateField 日期输入框,输入正确格式的日期 JDatePicker 继承自JComboBox的日期选择框,保证输入正确格式的日期(yyyy-MM-dd) 履历: ...
在主界面上单击菜单,可以打开子窗体.java swing自带的JTabbedPane没有显示关闭按钮的功能,这里在com.mkmis.controls包下自定义了一个TabbedPane控件,可以实现带关闭按钮的页签面板.应用结构如下图所示: 2 ...
17.1.5 可变尺寸线程池的使用 378 17.1.6 延迟线程池的使用 380 17.1.7 使用自定义参数的线程池 381 17.2 有返回值的线程调用 384 17.2.1 Callable接口简介 384 17.2.2 Future接口简介 384 17.2.3 ...
14.7.4选项板(JTabbedPane)使用示例485 14.7.5工具栏(JToolBar)使用示例486 14.8常用组件488 14.8.1标签(Jlabel)使用示例488 14.8.2按钮(JButton)使用示例491 14.8.3文本框(JTextField)和密码框...