java的JDesktopPane背景圖片自適應視窗大小示例
阿新 • • 發佈:2019-02-13
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author ZRL
* @date 2017/11/6
*/
public class AddBackgroundToJDesktopPane extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
AddBackgroundToJDesktopPane desktopPane = new AddBackgroundToJDesktopPane();
desktopPane.setVisible(true);
}
public AddBackgroundToJDesktopPane() {
super();
setTitle("桌面面板新增背景圖片示例");
setBounds(100, 100, 700, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//建立一個桌面面板
final JDesktopPane desktopPane = new JDesktopPane();
getContentPane().add(desktopPane, BorderLayout.CENTER);
//建立一個標籤元件,用於放置背景圖片
final JLabel backgroundLabel = new JLabel();
//需要自己匯入一張背景圖片到src目錄下
URL resource = this.getClass().getResource("/back.jpg");
ImageIcon icon = new ImageIcon(resource);
//壓縮背景圖片,使其適應視窗大小
icon.setImage(icon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT));
backgroundLabel.setIcon(icon);
backgroundLabel.setBounds(0, 0, this.getWidth(), this.getHeight());
desktopPane.add(backgroundLabel, new Integer(Integer.MIN_VALUE));
//當改變視窗大小時,自動調整背景圖片大小
getContentPane().addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int width = e.getComponent().getWidth();
int heigth = e.getComponent().getHeight();
icon.setImage(icon.getImage().getScaledInstance(width,heigth, Image.SCALE_DEFAULT));
backgroundLabel.setBounds(0, 0,width,heigth);
}
});
}
}
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author ZRL
* @date 2017/11/6
*/
public class AddBackgroundToJDesktopPane extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
AddBackgroundToJDesktopPane desktopPane = new AddBackgroundToJDesktopPane();
desktopPane.setVisible(true);
}
public AddBackgroundToJDesktopPane() {
super();
setTitle("桌面面板新增背景圖片示例");
setBounds(100, 100, 700, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//建立一個桌面面板
final JDesktopPane desktopPane = new JDesktopPane();
getContentPane().add(desktopPane, BorderLayout.CENTER);
//建立一個標籤元件,用於放置背景圖片
final JLabel backgroundLabel = new JLabel();
//需要自己匯入一張背景圖片到src目錄下
URL resource = this.getClass().getResource("/back.jpg");
ImageIcon icon = new ImageIcon(resource);
//壓縮背景圖片,使其適應視窗大小
icon.setImage(icon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT));
backgroundLabel.setIcon(icon);
backgroundLabel.setBounds(0, 0, this.getWidth(), this.getHeight());
desktopPane.add(backgroundLabel, new Integer(Integer.MIN_VALUE));
//當改變視窗大小時,自動調整背景圖片大小
getContentPane().addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int width = e.getComponent().getWidth();
int heigth = e.getComponent().getHeight();
icon.setImage(icon.getImage().getScaledInstance(width,heigth, Image.SCALE_DEFAULT));
backgroundLabel.setBounds(0, 0,width,heigth);
}
});
}
}