1. 程式人生 > >JFrame實現無邊框,實現拖動,實現關閉按鈕,開啟實現儲存檔案到本地,實現頭像放大功能,實現圖片瀏覽器

JFrame實現無邊框,實現拖動,實現關閉按鈕,開啟實現儲存檔案到本地,實現頭像放大功能,實現圖片瀏覽器

package SwingTest3;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;




public class FrameTest extends JFrame implements ActionListener, MouseListener,
		MouseWheelListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel jp;
	JLabel jl;
	JButton jb1, jb2, jb3, jb4;

	ImageIcon imgIco, showii;
	float width;
	float height;

	void init() {
		jp = new JPanel();
		jp.setLayout(null);
		jp.setBackground(new Color(0, 0, 0, 50));

		jb1 = new JButton();
		jb1.setIcon(new ImageIcon("img/bigger.png"));
		jb1.addActionListener(this);
		jb1.setBounds(280, 475, 42, 28);
		jb2 = new JButton();
		jb2.setIcon(new ImageIcon("img/smaller.png"));
		jb2.addActionListener(this);
		jb2.setBounds(320, 475, 42, 28);
		jb3 = new JButton();
		jb3.setIcon(new ImageIcon("img/save.png"));
		jb3.addActionListener(this);
		jb3.setBounds(360, 475, 42, 28);
		// 關閉按鈕
		jb4 = new JButton();
		jb4.setIcon(new ImageIcon("img/closeButton.png"));
		jb4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		jb4.setBounds(675, 0, 25, 25);

		jl = new JLabel("", JLabel.CENTER);
		jl.setBounds(50, 50, 600, 400);
		jl.addMouseWheelListener(this);
		//jl.addMouseListener(this);
		 jp.addMouseListener(this);
		 //jp.addMouseMotionListener(this);

		jp.add(jb1);
		jp.add(jb2);
		jp.add(jb3);
		jp.add(jb4);
		jp.add(jl);

		

		this.add(jp);
		// 去邊框
		this.setUndecorated(true);
		// 必須先去邊框再新增背景色
		this.setBackground(new Color(0, 0, 0, 50));
		this.setSize(700, 500);
		this.setLocation(300, 200);
		this.setVisible(true);
		// 下面這行程式碼如果解除安裝上面註釋的位置會報錯

	}

	public static void main(String[] args) {

		FrameTest ft = new FrameTest();
		ft.init();
		ft.imgIco = ft.getAvatar("zhujiannan", 0);
		ft.ShowImg();
	}

	// 拖動邊框

	int xOld = 0;
	int yOld = 0;

	public FrameTest() {
		// this.setLayout(null);
		// System.out.println("aaa");

		// 處理拖動事件
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				xOld = e.getX();
				yOld = e.getY();
			}
		});
		this.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				int xOnScreen = e.getXOnScreen();
				int yOnScreen = e.getYOnScreen();
				int xx = xOnScreen - xOld;
				int yy = yOnScreen - yOld;
				FrameTest.this.setLocation(xx, yy);
			}
		});

	}
	
	
	
	

	

	public void ShowImg() {
		if (imgIco.getIconWidth() > 695 || imgIco.getIconHeight() > 465) {
			width = 695;
			height = 675 * (float) imgIco.getIconHeight()
					/ imgIco.getIconWidth();
			// System.out.println((float)ii.getIconHeight()/ii.getIconWidth());
		} else {
			width = imgIco.getIconWidth();
			height = imgIco.getIconHeight();
		}
		showii = new ImageIcon(imgIco.getImage().getScaledInstance((int) width,
				(int) height, 0));
		this.jl.setText("");
		this.jl.setIcon(showii);

	}

	public void Zoom(int flag) {
		//System.out.println(width + "  " + flag);
		if ((width < 5000 && height < 4000) && (width > 5 && height > 4)) {
			if (flag == 0) {
				// 放大
				width *= 1.3;
				height *= 1.3;
			} else if (flag == 1) {
				// 縮小
				width *= 0.7;
				height *= 0.7;
			}
			this.jl.setIcon(null);
			showii = new ImageIcon(imgIco.getImage().getScaledInstance(
					(int) width, (int) height, 0));
			//todo
			this.jl.setIcon(showii);

		} else if (width > 5000 || height > 4000) {
			width *= 0.8;
			height *= 0.8;
			JOptionPane.showMessageDialog(this, "再放大會失真");
		} else if (width < 5 || height < 3) {
			width *= 1.2;
			height *= 1.2;
			JOptionPane.showMessageDialog(this, "再縮小就看不見");
		}
	}

	public void Save() {
		// 彈出檔案選擇框
		JFileChooser chooser = new JFileChooser();

		// 下面的方法將阻塞,直到【使用者按下儲存按鈕且“檔名”文字框不為空】或【使用者按下取消按鈕】
		int option = chooser.showSaveDialog(null);
		if (option == JFileChooser.APPROVE_OPTION) { // 假如使用者選擇了儲存
			File file = chooser.getSelectedFile();
			downUrlTxt("",
					"",
					file.getPath());
		}
	}

	public void downUrlTxt(String fileName, String fileUrl, String downPath) {
		File savePath = new File(downPath);
		if (!savePath.exists()) {
			savePath.mkdir();
		}
		String[] urlname = fileUrl.split("/");
		int len = urlname.length - 1;
		String uname = urlname[len];// 獲取檔名
		try {
			File file = new File(savePath + "/" + "x_" + uname);// 建立新檔案
			if (file != null && !file.exists()) {
				file.createNewFile();
			}
			OutputStream oputstream = new FileOutputStream(file);
			URL url = new URL(fileUrl);
			HttpURLConnection uc = (HttpURLConnection) url.openConnection();
			uc.setDoInput(true);// 設定是否要從 URL 連線讀取資料,預設為true
			uc.connect();
			InputStream iputstream = uc.getInputStream();
			// System.out.println("file size is:"+uc.getContentLength());//列印檔案長度
			byte[] buffer = new byte[4 * 1024];
			int byteRead = -1;
			while ((byteRead = (iputstream.read(buffer))) != -1) {
				oputstream.write(buffer, 0, byteRead);
			}
			oputstream.flush();
			iputstream.close();
			oputstream.close();
		} catch (Exception e) {
			System.out.println("讀取失敗!");
			e.printStackTrace();
		}

	}

	//imagUrl裡面有是個網址,然後這個網址裡面有張圖片

	public ImageIcon getAvatar(final String barejid, final int size) {
		String imageUrl = "";		
		try {
			return new ImageIcon(new URL(imageUrl));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imgIco;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == jb1) {

			Zoom(0);

		} else if (e.getSource() == jb2) {
			
			
			// 縮小
			Zoom(1);
		} else if (e.getSource() == jb3) {
			// 儲存
			Save();
		}
	}

	//

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		// TODO Auto-generated method stub
		if (e.getWheelRotation() == 1) {
			Zoom(0);
		} else if (e.getWheelRotation() == -1) {
			Zoom(1);
		}

	}

	@Override
	public void mouseClicked(MouseEvent mouseevent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent mouseevent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent mouseevent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent mouseevent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent mouseevent) {
		// TODO Auto-generated method stub

	}

}