標籤:str exception mouse 第一個 ica 姓名 end http serial
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;public class Guess2 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; // 當前檔案的序號 String strPath = ""; //+目錄路徑String strFileName = ""; //+檔案名稱JLabel jlbImg = null; private int idImg = 1; 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 { Guess2 frame = new Guess2(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Guess2() { setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 645, 409); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); // 選擇目錄 button的處理常式 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); contentPane.add(btnDir); // 文字框。顯示目錄 tfDir = new JTextField(); tfDir.setEditable(false); tfDir.setBounds(125, 27, 363, 21); contentPane.add(tfDir); tfDir.setColumns(10); // 選擇班級 button的處理常式 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, 363, 21); contentPane.add(tfClass); tfClass.setColumns(10); // 標籤,顯示帶推測學生姓名 JLabel lbGuessName = new JLabel("\u59D3\u540D"); lbGuessName.setBounds(259, 91, 102, 23); contentPane.add(lbGuessName); // 標籤。顯示第一個學生相片 final JLabel lblImg1 = new JLabel("\u56FE\u72471"); lblImg1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { } }); lblImg1.setBounds(26, 21, 183, 200); contentPane.add(lblImg1); jlbImg1 = new JLabel(); jlbImg1.setBackground(Color.RED); jlbImg1.setBounds(26, 151, 181,201); this.add(jlbImg1); // 標籤,顯示第二個學生相片 final JLabel lblImg2 = new JLabel("\u56FE\u72472"); lblImg2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { } }); lblImg2.setForeground(Color.BLACK); lblImg2.setBackground(SystemColor.inactiveCaption); lblImg2.setBounds(241, 21, 200, 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("\u56FE\u72473"); lblImg3.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { } }); lblImg3.setBounds(434,21, 200, 200); contentPane.add(lblImg3); jlbImg3 = new JLabel(); jlbImg3.setBackground(Color.RED); jlbImg3.setBounds(434, 155, 185, 201); this.add(jlbImg3); // 再猜一次 button,點擊則更新對應的三張圖片 與 帶推測學生姓名 final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21"); btnGuessAgain.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource()==btnGuessAgain ){ //假設是nextbutton String strTmp = fileArray[index].toString(); index++; if(index==NUM_IMG) index = 0; jlbImg1.setIcon(new ImageIcon(strTmp)); index++; jlbImg2.setIcon(new ImageIcon(strTmp)); index++; if(e.getSource()==btnGuessAgain ){ //假設是nextbutton String strTmp2 = fileArray[index].toString(); index++; jlbImg2.setIcon(new ImageIcon(strTmp2)); index++; } } String strTmp1 = fileArray[index].toString(); index++; jlbImg3.setIcon(new ImageIcon(strTmp1)); } }); btnGuessAgain.setBounds(223, 337, 93, 23); contentPane.add(btnGuessAgain); }}
結果
昨天第二個文字框都還沒能都讀取得了。如今能夠了,
且能顯示三張圖片。
可是這樣的進度都不知道能不能在星期五前完畢猜猜看。。。。
《Java課程實習》日誌(周二)