Java 自定义窗口风格 大家有的时候是不是会觉得java 默认的窗口风格,很单一、古板,不好看,希望自己搞些漂亮的风格(如 qq 登陆界面)。 看下面两张图: Java 默认的窗口风格: 本程序实现的风格: 很多时候,我们是需要下面的风格的,而 java 又不提供现成的方法,这就要我们自己去写代码实现。要想自己定义java 窗口的风格,首先应取消 java 自带的默认风格,用 JFrame 里的setUndecorated(true);方法可以取消默认风格,然后自己重画最大、最小、关闭按钮,需要注意的是,取消默认风格后,要想再移动窗口,需要自己再编写类来处理事件,下面粘贴完整的代码,给那些想自定义窗口风格的童鞋以帮助,希望高手勿喷。 1.主程序: package MyPanel; import javax.swing.*; public class MyFrame extends JFrame { public MyFrame() { this.setUndecorated(true); this.setBounds(300, 200, 348, 265);; init(); this.validate(); } public void init() { this.setTitle("自定义窗口风格"); MyPanel pane = new MyPanel(MyPanel.CLOSE_MIN_MAX, "picture/picture.jpg"); pane.setFrame(this); MyListener moveListener = new MyListener(this); this.addMouseListener(moveListener); this.addMouseMotionListener(moveListener); this.setContentPane(pane); this.setVisible(true); } public static void main(String[] args) { new MyFrame(); } } 2.事件处理类: package MyPanel; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class MyListener extends MouseAdapter { private Point lastPoint = null; private MyFrame window = null; public MyListener(MyFrame window) { this.window = window; } public void mousePressed(MouseEvent e) { lastPoint = e.getLocationOnScreen(); } public void mouseDragged(MouseEvent me) { Point point = me.getLocationOnScreen(); int offsetX = point.x - lastPoint.x; int offsetY = point.y - lastPoint.y; Rectangle bounds = window.getBounds(); bounds.x += offsetX; bounds.y += offsetY; window.setBounds(bounds); lastPoint = point;...