標籤:
import java.awt.EventQueue;import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.AbstractButton; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.Color; import java.awt.Image; import java.awt.SystemColor; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Random; //+import javax.swing.JOptionPane; //+public class Guess3 extends JFrame { /** * */ private static final long serialVersionUID = 1L;private JPanel contentPane;private JTextField tfDir;private JTextField tfClass;File[] fileArray; // 檔案夾下所有檔案int NUM_IMG = 0; // 檔案總數目int index = 0; // 當前檔案的序號int i = 0;String strPath = ""; //+檔案夾路徑String strFileName = ""; //+檔案名稱JLabel jlbImg = null;JLabel jlbImg1 = null;JLabel jlbImg2 = null;JLabel jlbImg3 = null;/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Guess3 frame = new Guess3();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public Guess3() {setTitle("猜猜看TYHban");//左上方標題setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉介面,如果不寫這句,介面是沒了,但程式還駐留在記憶體中沒有結束setBounds(100, 100, 645, 510);//setBounds(x,y,width,height); x:組件在容器X軸上的起點 y:組件在容器Y軸上的起點 width:組件的長度 height:組件的高度contentPane = new JPanel();//面板contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//設定面板的邊界,Border描述了面板四周的邊界(屬於面板內部),EmptyBorder是一個空白的邊界;//語句的意思是讓contentPane內部邊框為空白,並且有5個像素的厚度,如果直接在contentPane上面添加一個按鈕(設定為充滿),那麼按鈕將鋪滿除了邊框之外的內部矩形setContentPane(contentPane);contentPane.setLayout(null);//介面排版// 選擇目錄 按鈕的處理常式JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");btnDir.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(), "選擇"); //目錄選擇 File file=jfc.getSelectedFile(); tfDir.setText(file.getAbsolutePath());if(file!=null && file.isDirectory()){// 擷取檔案夾下所有的檔案,顯示該檔案夾的所有檔案fileArray = file.listFiles();NUM_IMG = fileArray.length;}}});btnDir.setBounds(26, 26, 93, 23);//選擇目錄一,setBounds(x,y,width,height)contentPane.add(btnDir);//選擇目錄1// 文字框,顯示目錄tfDir = new JTextField();//建一個文字框,文字框能輸入的字元為無窮大tfDir.setEditable(false);//設定不可編輯tfDir.setBounds(125, 27, 450, 21);//setBounds(x,y,width,height)contentPane.add(tfDir);//文字框//tfDir.setColumns(10);// 選擇班級 按鈕的處理常式JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");btnClass.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(), "選擇"); File file=jfc.getSelectedFile();tfClass.setText(file.getAbsolutePath());}});btnClass.setBounds(26, 59, 93, 23);contentPane.add(btnClass);//選擇目錄二// 文字框,顯示班級檔案tfClass = new JTextField();tfClass.setEditable(false);tfClass.setBounds(125, 60, 450, 21);contentPane.add(tfClass);//文字框tfClass.setColumns(10);// 標籤,顯示帶猜測學生姓名final JLabel lbGuessName = new JLabel("姓名");lbGuessName.setBounds(259, 91,140, 23);contentPane.add(lbGuessName);//姓名顯示// 標籤,顯示第一個學生相片final JLabel lblImg1 = new JLabel("圖片一");lblImg1.addMouseListener(new MouseAdapter () {@Overridepublic void mouseClicked(MouseEvent arg0) {if(arg0.getSource()==lblImg1){//+if(( lblImg1.getText().equals(lbGuessName.getText()))){JOptionPane.showMessageDialog(null,"哇,你猜對了","提示",JOptionPane.PLAIN_MESSAGE);}else {JOptionPane.showMessageDialog(null,"no,猜錯了","錯誤",JOptionPane.ERROR_MESSAGE);} }} });lblImg1.setBounds(26,155, 150, 200);contentPane.add(lblImg1);//圖片一顯示jlbImg1 = new JLabel();jlbImg1.setBackground(Color.RED);jlbImg1.setBounds(26, 151, 181,201);//JLabel在javax.swing Label在java.awtthis.add(jlbImg1);// 標籤,顯示第二個學生相片final JLabel lblImg2 = new JLabel("圖片二");lblImg2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent arg1) {if(arg1.getSource()==lblImg2){//+if(( lblImg2.getText().equals(lbGuessName.getText()))){JOptionPane.showMessageDialog(null,"哇,你猜對了","提示",JOptionPane.PLAIN_MESSAGE);}else {JOptionPane.showMessageDialog(null,"no,猜錯了","錯誤",JOptionPane.ERROR_MESSAGE);} }}});lblImg2.setForeground(Color.BLACK);lblImg2.setBackground(SystemColor.inactiveCaption);lblImg2.setBounds(241,155, 150, 200);contentPane.add(lblImg2);//圖片二顯示jlbImg2 = new JLabel();jlbImg2.setBackground(Color.RED);jlbImg2.setBounds(240, 155, 183, 201);this.add(jlbImg2);// 標籤,顯示第三個學生相片final JLabel lblImg3 = new JLabel("圖片三");lblImg3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent arg2) {if(arg2.getSource()==lblImg3){//+if(( lblImg3.getText().equals(lbGuessName.getText()))){JOptionPane.showMessageDialog(null,"哇,你猜對了","提示",JOptionPane.PLAIN_MESSAGE);}else {JOptionPane.showMessageDialog(null,"no,猜錯了","錯誤",JOptionPane.ERROR_MESSAGE);} }}});lblImg3.setBounds(434,155, 150, 200);contentPane.add(lblImg3);jlbImg3 = new JLabel();jlbImg3.setBackground(Color.RED);jlbImg3.setBounds(434, 155, 185, 201);this.add(jlbImg3);// 再猜一次 按鈕,點擊則更新相應的三張圖片 與 帶猜測學生姓名final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");btnGuessAgain.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(e.getSource()==btnGuessAgain ){ //如果是next按鈕Random random = new Random(System.currentTimeMillis());//隨機數ImageIcon icon;for(int i=0;i<3;i++){index = random.nextInt(NUM_IMG);//隨機數調用String strTmp = fileArray[index].toString();//圖片數組String filename1=fileArray[index].getName();//名字數組try {icon = new ImageIcon(ImageIO.read(new File(strTmp)));// 從圖表中擷取到圖片Image image = icon.getImage(); // 縮放映像Image smallImage = image.getScaledInstance(150,200,Image.SCALE_FAST);//把Image檔案轉化為ImageIconicon = new ImageIcon(smallImage);if(index==NUM_IMG)index = 0;switch(i){case 0:System.out.println(fileArray[index].getName());lblImg1.setIcon(icon);lblImg1.setText(filename1);break;case 1:System.out.println(fileArray[index].getName()); lblImg2.setIcon(icon);lblImg2.setText(filename1);break;case 2:System.out.println(fileArray[index].getName()); lblImg3.setIcon(icon);lblImg3.setText(filename1);break;}} catch (IOException e1) {e1.printStackTrace();}}}@SuppressWarnings("unused")Random random1 = new Random(index);System.out.println(fileArray[index].getName());//輸出圖片的名字}});//==+btnGuessAgain.setBounds(223, 400, 93, 23);//setBounds(x,y,width,height)contentPane.add(btnGuessAgain);//next}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
《Java課程實習》日誌(周四)猜猜看注釋版