標籤:
項目二:圖片顯示目的:瞭解GUI的映像顯示的常用方式。目標:利用項目一,獲得需要顯示的影像檔,顯示在介面上(參考:java的swing中用誰什麽控制項來裝一張圖片?,《Java程式設計》第14周實驗作業:GUI編程初步)
package com.liang;import java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileFilter;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;class myFileFilter implements FileFilter{ @Override public boolean accept(File pathname) { String filename = pathname.getName().toLowerCase(); if(filename.contains(".jpg")){ return false; }else{ return true; } } } public class FileChooser extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; JButton open=null; JTextField jtfPath = null; JLabel jlbImg = null; JButton btnNext = null; String strPath = ""; //檔案夾路徑 String strFileName = ""; //檔案名稱 File[] fileArray; // 檔案夾下所有檔案 int NUM_IMG = 0; // 檔案總數目 int index = 0; // 當前檔案的序號 public static void main(String[] args) { new FileChooser(); } public FileChooser(){ this.setTitle("Week16"); // 設定布局方式 this.setLayout(new FlowLayout()); // 按鈕初始化 open=new JButton("選擇目錄"); // 添加監聽 open.addActionListener(this); // 把按鈕添加到JFrame容器中 this.add(open); // 添加文字框控制項 jtfPath = new JTextField("選擇的檔案",40); jtfPath.setEditable(false); // 不可編輯 jtfPath.setHorizontalAlignment(JTextField.CENTER); // 置中 this.add(jtfPath); // 顯示下一張圖片 btnNext = new JButton("顯示下一張"); this.add(btnNext); btnNext.addActionListener(this); // 添加顯示Image的JLabel控制項 jlbImg = new JLabel(); jlbImg.setBackground(Color.RED); jlbImg.setBounds(100, 100, 200, 200); this.add(jlbImg); // 設定JFrame的大小,可顯示,預設關閉按鈕 this.setBounds(400, 200, 700, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //多個組件需要監聽的情況下,事件響應的編碼方式 if(e.getSource()==open){ //如果是open按鈕 JFileChooser jfc=new JFileChooser(); //jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); jfc.showDialog(new JLabel(), "選擇"); File file=jfc.getSelectedFile(); if(file.isDirectory()){ System.out.println("檔案夾:"+file.getAbsolutePath()); }else if(file.isFile()){ System.out.println("檔案:"+file.getAbsolutePath()); } System.out.println(jfc.getSelectedFile().getName()); // 把檔案路徑顯示在文字框中 jtfPath.setText(file.getAbsolutePath()); //jlbImg.setIcon(new ImageIcon(file.getAbsolutePath())); // 擷取檔案路徑 與檔案名稱 strPath = file.getAbsolutePath(); strFileName = jfc.getSelectedFile().getName(); if(file!=null && file.isDirectory()){ // 參考: java中File.listFiles(FileFilter) FileFilter的使用 // http://zhouzaibao.iteye.com/blog/347557 ; // 擷取檔案夾下所有的檔案 fileArray = file.listFiles(); NUM_IMG = fileArray.length; } } if(e.getSource()==btnNext){ //如果是next按鈕 String strTmp = fileArray[index].toString(); index++; if(index==NUM_IMG) index = 0; jlbImg.setIcon(new ImageIcon(strTmp)); } }}
第二部分:作業部落格要求1. 在作業部落格中,完成上述3個項目中至少2個,並把運行結果、代碼寫到部落格中。
2. 在作業部落格中,回答以下三個問題:
(1)簡述Java中,GUI的事件驅動模式。事件處理模式,各種事件監聽器。。。。
(2)簡述Java中,字串轉化為數值、數值轉化為字串的常用方式。字串型轉換成各種數字類型: String s = "169"; byte b = Byte.parseByte( s ); short t = Short.parseShort( s ); int i = Integer.parseInt( s ); long l = Long.parseLong( s ); Float f = Float.parseFloat( s ); Double d = Double.parseDouble( s );
數字類型轉換成字串型: 法一:String s = String.valueOf( value); // 其中 value 為任意一種數字類型。 法二:String aa = 1+"";
(3) 簡述Java中,JFileChooser使用的核心代碼。
import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class FileChooserTest { public static void main(String [] args) {
《Java程式設計》第16周周四:GUI編程及檔案對話方塊的使用 任務二