感谢大佬:https://blog.csdn.net/ltx06/article/details/28996839
最近在书上看到这篇文章,觉得挺有意思。与大家分享一下,具体可以参见明日科技出版的《Java经典编程300例》。
默认情况下,我们用java设计的界面窗体是这样的:
默认的标题栏、最大化、最小化、关闭按钮、窗体边框。
代码:
首先是带背景图片面板类:
package cn.chengmaoning.customframe;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class BackgroundPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -898997756345637309L;
private Image image;
/**
* Constructor
*/
public BackgroundPanel() {
super();
setOpaque(false);
setLayout(null);
}
/**
* @param image
* 要设置的 image
*/
public void setImage(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
if (image != null) {
int width = getWidth();// 获取组件大小
int height = getHeight();
g.drawImage(image, 0, 0, width, height, this);// 绘制图片与组件大小相同
}
super.paintComponent(g);// 执行超类方法
}
}
其次再是主类:
package cn.chengmaoning.customframe;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.jws.Oneway;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class ControlFormStatus extends JFrame {
/**
*
*/
private static final long serialVersionUID = -2214759764324605473L;
private JPanel contentPane;
private Point pressedPoint;
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO 自动生成的方法存根
try {
ControlFormStatus frame = new ControlFormStatus();
frame.setVisible(true);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
/**
* Constructor
*/
public ControlFormStatus(){
setUndecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
BackgroundPanel topPanel = new BackgroundPanel();
topPanel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e){
do_topPanel_mouseDragged(e);
}
});
topPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
do_topPanel_mousePressed(e);
}
});
Image centerImage = new ImageIcon(getClass().getResource("frameTop.png")).getImage();
Dimension dimension = new Dimension(centerImage.getWidth(this), centerImage.getHeight(this));
topPanel.setPreferredSize(dimension);
topPanel.setImage(centerImage);
contentPane.add(topPanel,BorderLayout.NORTH);
topPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(60, 22));
panel.setOpaque(false);
topPanel.add(panel);
panel.setLayout(new GridLayout(1, 0, 0, 0));
JButton button = new JButton("");
button.setRolloverIcon(new ImageIcon(ControlFormStatus.class.getResource("minBH.jpg")));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
do_button_actionPerformed(e);
}
});
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setIcon(new ImageIcon(ControlFormStatus.class.getResource("minB.jpg")));
panel.add(button);
JToggleButton button_1 = new JToggleButton("");
button_1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成的方法存根
do_button_1_itemStateChanged(e);
}
});
button_1.setRolloverIcon(new ImageIcon(ControlFormStatus.class.getResource("maxBH.jpg")));
button_1.setRolloverSelectedIcon(new ImageIcon(ControlFormStatus.class.getResource("maxBH.jpg")));
button_1.setIcon(new ImageIcon(getClass().getResource("maxB.jpg")));
button_1.setContentAreaFilled(false);
button_1.setBorderPainted(false);
button_1.setFocusPainted(false);
panel.add(button_1);
JButton button_2 = new JButton("");
button_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
do_button_2_actionPerformed(e);
}
});
button_2.setRolloverIcon(new ImageIcon(getClass().getResource("closeBH.jpg")));
button_2.setIcon(new ImageIcon(getClass().getResource("closeB.jpg")));
button_2.setFocusPainted(false);
button_2.setContentAreaFilled(false);
button_2.setBorderPainted(false);
panel.add(button_2);
BackgroundPanel baclBackgroundPanel_1 = new BackgroundPanel();
Image topImage = new ImageIcon(getClass().getResource("frameCenter.png")).getImage();
baclBackgroundPanel_1.setImage(topImage);
contentPane.add(baclBackgroundPanel_1, BorderLayout.CENTER);
}
protected void do_button_2_actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
dispose();
}
protected void do_button_1_itemStateChanged(ItemEvent e) {
// TODO 自动生成的方法存根
if (e.getStateChange() == ItemEvent.SELECTED) {
setExtendedState(MAXIMIZED_BOTH);
} else {
setExtendedState(NORMAL);
}
}
protected void do_button_actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
setExtendedState(ICONIFIED);
}
protected void do_topPanel_mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
pressedPoint = e.getPoint();
}
protected void do_topPanel_mouseDragged(MouseEvent e) {
// TODO 自动生成的方法存根
Point point = e.getPoint();
Point locationPoint = getLocation();
int x = locationPoint.x + point.x - pressedPoint.x;
int y = locationPoint.y + point.y - pressedPoint.y;
setLocation(x, y);
}
}
代码中用到的图片如下:(我不知道有什么更好的方法可以把图片保存到这篇文章里,所以只有贴到这里了,试验的同学可以直接另存图片到本机,放置到与.java相同路径下即可)