欢迎来到天天文库
浏览记录
ID:15753473
大小:23.50 KB
页数:3页
时间:2018-08-05
《在jbuilder中把标题栏隐藏掉之后 实现拖动窗口》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1楼gtlang78()回复于2006-11-2909:35:32得分80importjava.awt.*;importjava.awt.event.MouseEvent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.event.MouseInputAdapter;publicclassUndecoratedFrameextendsJFrame{privatestaticfinalintTITLE_HEIGHT=20;publicUndecoratedFr
2、ame()throwsHeadlessException{super();setUndecorated(true);MouseHandlerml=newMouseHandler();addMouseListener(ml);addMouseMotionListener(ml);}publicUndecoratedFrame(Stringtitle)throwsHeadlessException{super(title);setUndecorated(true);MouseHandlerml=newMouseHandler();addMouseL
3、istener(ml);addMouseMotionListener(ml);}publicInsetsgetInsets(){returnnewInsets(TITLE_HEIGHT,1,1,1);}publicvoidpaint(Graphicsg){super.paint(g);g.setColor(newColor(0,0,128));g.drawRect(0,0,getWidth()-1,getHeight()-1);g.fillRect(0,0,getWidth(),TITLE_HEIGHT);FontMetricsfm=g.get
4、FontMetrics();g.setColor(Color.white);g.drawString(getTitle(),2,(TITLE_HEIGHT-fm.getHeight())/2+fm.getAscent());}privateclassMouseHandlerextendsMouseInputAdapter{privatePointpoint;publicvoidmousePressed(MouseEvente){if(e.getY()<=TITLE_HEIGHT){this.point=e.getPoint();}}public
5、voidmouseDragged(MouseEvente){if(point!=null){setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));Pointp=e.getPoint();intdx=p.x-point.x;intdy=p.y-point.y;intx=getX();inty=getY();setLocation(x+dx,y+dy);}}publicvoidmouseReleased(MouseEvente){point=null;setCursor(Cursor.g
6、etDefaultCursor());}}publicstaticvoidmain(String[]args){JFramef=newUndecoratedFrame("UndecoratedFrame");f.getContentPane().add(newJLabel("HelloWorld!",JLabel.CENTER),BorderLayout.CENTER);f.setSize(400,400);f.setLocationRelativeTo(null);f.setDefaultCloseOperation(JFrame.EXIT_
7、ON_CLOSE);f.setVisible(true);}}
此文档下载收益归作者所有