Swing 设置无边框Frame
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class LogANA extends JFrame { 6 7 //背景图片bgImage 8 private ImageIcon bgImage = new ImageIcon("src/main/images/bg.jpg"); 9 //用于处理拖动事件,表示鼠标按下时的坐标,相对于JFrame 10 int xOld = 0; 11 int yOld = 0; 12 13 private ImageIcon bt1mage = new ImageIcon("src/main/images/mini.png"); 14 private ImageIcon bt1mage_enable = new ImageIcon("src/main/images/mini_enable.png"); 15 private ImageIcon bt2mage = new ImageIcon("src/main/images/close.png"); 16 private ImageIcon bt2mage_enable = new ImageIcon("src/main/images/close_enable.png"); 17 18 19 20 public LogANA() { 21 22 getContentPane().setLayout(new BorderLayout()); 23 this.setLocationRelativeTo(null); 24 this.setSize(bgImage.getIconWidth(), bgImage.getIconHeight()); 25 26 //处理拖动事件---去掉默认边框后,不能拖动了,具体实现如下 27 this.addMouseListener(new MouseAdapter() { 28 @Override 29 public void mousePressed(MouseEvent e) { 30 xOld = e.getX();//记录鼠标按下时的坐标 31 yOld = e.getY(); 32 } 33 }); 34 35 this.addMouseMotionListener(new MouseMotionAdapter() { 36 @Override 37 public void mouseDragged(MouseEvent e) { 38 int xOnScreen = e.getXOnScreen(); 39 int yOnScreen = e.getYOnScreen(); 40 int xx = xOnScreen - xOld; 41 int yy = yOnScreen - yOld; 42 LogANA.this.setLocation(xx, yy);//设置拖拽后,窗口的位置 43 } 44 }); 45 46 47 JPanel mainPanel = new JPanel(); 48 mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 49 mainPanel.setSize(bgImage.getIconWidth(), 100); 50 mainPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10)); 51 52 //关闭按钮 53 JButton miniBtn = new MyIconButton(bt1mage, bt1mage_enable, bt1mage, ""); 54 mainPanel.add(miniBtn); 55 56 //关闭按钮 57 JButton closeBtn = new MyIconButton(bt2mage, bt2mage_enable, bt2mage, ""); 58 mainPanel.add(closeBtn); 59 60 61 getContentPane().add(mainPanel, BorderLayout.CENTER); 62 63 closeBtn.addActionListener(new ActionListener() { 64 @Override 65 public void actionPerformed(ActionEvent e) { 66 System.exit(0); 67 } 68 }); 69 70 miniBtn.addActionListener(new ActionListener() { 71 @Override 72 public void actionPerformed(ActionEvent e) { 73 setExtendedState(JFrame.ICONIFIED);//最小化窗体 74 } 75 }); 76 77 setUndecorated(true); 78 setLocationRelativeTo(null); 79 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 80 } 81 82 public static void main(String[] args) { 83 LogANA j = new LogANA(); 84 j.setVisible(true); 85 } 86 87 }