[Java] java无框无标题栏窗口(皮肤自定义) →→→→→进入此内容的聊天室

来自 , 2021-01-30, 写在 Java, 查看 127 次.
URL http://www.code666.cn/view/e0741335
  1. import java.awt.Cursor;
  2. import java.awt.Point;
  3. import java.awt.Toolkit;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseMotionAdapter;
  6. import java.net.URL;
  7.  
  8. import javax.swing.Icon;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12.  
  13. /**
  14.  * 无标题栏、无框窗口,皮肤自定义
  15.  */
  16. public class PicFrame extends JFrame {
  17.  
  18.         /**
  19.          *
  20.          */
  21.         private static final long serialVersionUID = 1L;
  22.         private Point loc = null;
  23.         private Point tmp = null;
  24.         private boolean isDragged = false;
  25.         private JLabel pic;
  26.  
  27.         public PicFrame() {
  28.                 pic = new JLabel();
  29.                 pic.setIcon(getIcon("theme.jpg"));
  30.                 pic.setBounds(0, 0, 510, 290);
  31.  
  32.                 // 初始化窗体
  33.                 setResizable(false);
  34.                 // 将窗体设置成无标题栏的语句,setUndecorated();注意此语句一定要放在setVisible之前,否则会报错
  35.                 setUndecorated(true);
  36.                 setDefaultCloseOperation(EXIT_ON_CLOSE);
  37.                 setSize(510, 290);
  38.                 setVisible(true);
  39.                 add(pic);
  40.  
  41.                 // 设置窗体为屏幕的中央位置
  42.                 int w = (Toolkit.getDefaultToolkit().getScreenSize().width - 510) / 2;
  43.                 int h = (Toolkit.getDefaultToolkit().getScreenSize().height - 290) / 2;
  44.                 this.setLocation(w, h);
  45.  
  46.                 // 为窗体添加鼠标事件
  47.                 this.addMouseListener(new java.awt.event.MouseAdapter() {
  48.                         public void mouseReleased(MouseEvent e) {
  49.                                 isDragged = false;
  50.                                 // 为指定的光标设置光标图像
  51.                                 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  52.                         }
  53.  
  54.                         public void mousePressed(MouseEvent e) {
  55.                                 tmp = new Point(e.getX(), e.getY());
  56.                                 isDragged = true;
  57.                                 setCursor(new Cursor(Cursor.MOVE_CURSOR));
  58.                         }
  59.                 });
  60.  
  61.                 this.addMouseMotionListener(new MouseMotionAdapter() {
  62.                         // 鼠标按键在组件上按下并拖动时调用。
  63.                         public void mouseDragged(MouseEvent e) {
  64.                                 if (isDragged) {
  65.                                         loc = new Point(getLocation().x + e.getX() - tmp.x,
  66.                                                         getLocation().y + e.getY() - tmp.y);
  67.                                         setLocation(loc);
  68.                                 }
  69.                         }
  70.                 });
  71.         }
  72.  
  73.         // 获取图片的方法
  74.         public Icon getIcon(String path) {
  75.                 URL url = PicFrame.class.getClassLoader().getResource(path);
  76.                 return new ImageIcon(url);
  77.         }
  78.  
  79.         public static void main(String[] args) {
  80.  
  81.                 new PicFrame();
  82.  
  83.         }
  84.  
  85. }
  86.  

回复 "java无框无标题栏窗口(皮肤自定义)"

这儿你可以回复上面这条便签

captcha