您的位置:首页 > 编程语言 > Java开发

《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);
}
}


 

代码运行截图:



 



 



 



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: