《java实习》日志 周四
2015-07-02 11:48
260 查看
</pre><pre name="code" class="java">import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; 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.FlowLayout; 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.IOException; public class Guess01 extends JFrame { /** * */ private static final long serialVersionUID = 1L; private static final int IMAGE_NUMBER=10; private Image[] image=new Image[IMAGE_NUMBER]; private JPanel contentPane; private JTextField tfDir; private JTextField tfClass; //设置图片大小 private final static int WIDTH=200; private final static int HEIGHT=200; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Guess01 frame = new Guess01(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Guess01() { 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); // 选择目录 按钮的处理程序 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(); if(file.isDirectory()){ System.out.println("文件夹:"+file.getAbsolutePath()); }else if(file.isFile()){ System.out.println("文件:"+file.getAbsolutePath()); } System.out.println(jfc.getSelectedFile().getName()); // 把文件路径显示在文本框中 tfDir.setText(file.getAbsolutePath()); } }); 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); // 选择班级 按钮的处理程序 JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7"); btnClass.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); 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); this.setLayout(new FlowLayout()); for(int i=1;i<=IMAGE_NUMBER;i++){ try{ java.io.File file=new java.io.File("D:/photos/"+java.lang.String.valueOf(i)+".jpg"); image[i-1]=javax.imageio.ImageIO.read(file); } catch(IOException ex){ ex.printStackTrace(); } } for(int i=0;i<3;i++){ java.util.Random random = new java.util.Random(); ImageIcon oneIcon=new ImageIcon(image[random.nextInt(IMAGE_NUMBER)]); add(new JLabel(oneIcon)); oneIcon.setImage(oneIcon.getImage().getScaledInstance(Guess01.WIDTH,Guess01.HEIGHT,Image.SCALE_DEFAULT)); } // 标签,显示第一个学生相片 JLabel lblImg1 = new JLabel("\u56FE\u72471"); lblImg1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { } }); lblImg1.setBounds(26, 151, 183, 178); contentPane.add(lblImg1); // 标签,显示第二个学生相片 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, 155, 183, 172); contentPane.add(lblImg2); // 标签,显示第三个学生相片 JLabel lblImg3 = new JLabel("\u56FE\u72473"); lblImg3.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { } }); lblImg3.setBounds(434, 155, 185, 172); contentPane.add(lblImg3); // 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名 JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21"); btnGuessAgain.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnGuessAgain.setBounds(223, 337, 93, 23); contentPane.add(btnGuessAgain); } }
相关文章推荐
- 5、JAVA数据库连接池实现
- JDK的动态代理
- struts2没配置的访问方式
- struts2中如何使用servlet
- java.util.Date与java.sql.Date的区别
- 好大滴坑, Spring MVC覆盖了Trsaction
- Spring与数据库
- scala集合类型与java的转换
- 《Java课程实习》日志(周四)
- [图解教程]Eclipse不可不知的用法之一:自动生成Getter、Setter和构造方法
- java多线程实例-模拟购票
- 稀疏矩阵乘法加法等的java实现
- Spring JMX demo 例子
- spring mvc拦截器和<mvc:annotation-driven />的详解
- Java中只有按值传递,没有按引用传递!
- Eclipse: The method of type must override a superclass method解决方式
- SSh框架结构(Struts2.1+Hibernate4.0+Spring3.1)
- springmvc是 spring的一个组件,他们各自的配置文件详解
- Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
- Spring与Ehcache简单自定义监听器配置