《Java课程实习》日志(周二)
2015-06-30 10:50
846 查看
目标:实现“选择目录”和“选择班级”两个按钮的基本功能,在文本框显示所选目录的路径信息。以及增加“开始游戏”按钮,此按钮相关功能暂未完成实现。
还未完成实现的功能:三张图片的显示以及其中一张对应姓名的显示等。
源代码如下:
代码运行截图:
还未完成实现的功能:三张图片的显示以及其中一张对应姓名的显示等。
源代码如下:
<pre class="java" name="code">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.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; public class Guess01 extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField tfDir; private JTextField tfClass; /** * 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("选择目录"); btnDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub 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()); // 获取文件路径 与文件名 String strPath = file.getAbsolutePath(); } }); btnDir.setBounds(26, 26, 93, 23); contentPane.add(btnDir); String strPath = ""; //文件夹路径 String strFileName = ""; //文件名称 // 文本框,显示目录 tfDir = new JTextField("目录路径"); tfDir.setEditable(false);//不可编辑 tfDir.setBounds(125, 27, 363, 21); contentPane.add(tfDir); tfDir.setColumns(10); tfDir.setHorizontalAlignment(JTextField.CENTER); // 居中 this.add(tfDir); // 选择班级 按钮的处理程序 JButton btnClass = new JButton("选择班级"); btnClass.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub 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()); // 把文件路径显示在文本框中 tfClass.setText(file.getAbsolutePath()); // 获取文件路径 与文件名 String strPath = 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); tfClass.setHorizontalAlignment(JTextField.CENTER); // 居中 // 增加开始游戏按钮,点击按钮即随机显示三张图片,以及同时显示待猜测学生的姓名 final JButton btnStart= new JButton("开始游戏"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnStart.setBounds(26, 95, 93, 23); contentPane.add(btnStart); // 标签,显示待猜测学生姓名 JLabel lbGuessName = new JLabel("姓名"); lbGuessName.setBounds(300, 91, 102, 23); contentPane.add(lbGuessName); // 标签,显示第一个学生相片 JLabel lblImg1 = new JLabel("图片1"); lblImg1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { } }); lblImg1.setBounds(26, 151, 183, 178); contentPane.add(lblImg1); // 标签,显示第二个学生相片 JLabel lblImg2 = new JLabel("图片2"); lblImg2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { } }); lblImg2.setBackground(SystemColor.inactiveCaption); lblImg2.setBounds(241, 155, 183, 172); contentPane.add(lblImg2); // 标签,显示第三个学生相片 JLabel lblImg3 = new JLabel("图片3"); lblImg3.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { } }); lblImg3.setBounds(500, 155, 185, 172); contentPane.add(lblImg3); // 再猜一次 按钮,点击则更新相应的三张图片 与 待猜测学生姓名 JButton btnGuessAgain = new JButton("再猜一次"); btnGuessAgain.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnGuessAgain.setBounds(250,337, 120, 23); contentPane.add(btnGuessAgain); } }
代码运行截图:
相关文章推荐
- Tiny对JDK有什么要求么?
- 【MyEclipse 2015】 逆向破解实录系列【2】(纯研究)
- java 内置定时器实现定时任务和自定义定时任务
- JAVA File的创建及相对路径绝对路径
- 【java0008】多线程卖票(二)
- 【MyEclipse 2015】 逆向破解实录系列【1】(纯研究)
- Java 基础 2015_6_30
- Java生成MD5加密字符串代码实例
- Java中equals方法和“==”的比较
- WebService(2)-XML系列之Java和Xml之间相互转换
- Spring MVC-HandlerAdapter
- Java程序设计——第十六周周五:数据库的连接与随机数使用
- Eclipse SVN冲突解决
- Java数据导出功能之导出Excel文件实例
- Eclipse下切换Project的SVN地址
- struts2、servlet中返回json数据格式
- 【Java Swing探索之路系列】之三:Java Swing布局管理器组件
- Java数据导入功能之读取Excel文件实例
- 积跬步,聚小流------java获取图片的尺寸
- java中的深克隆和浅克隆