今天是补16周周四作业GUI编程的文本框使用的作业,同样的感觉编程之路真的不易,不过还好能完美的完成作业,这也真是极好的
项目一:文件及选择
目的:了解文件对话框的使用方法,熟悉GUI中事件驱动的核心步骤。
1. 阅读博文 “
JFileChooser (Java Swing提供的文件选择对话框)”,了解博文大意。
2. 在IDE中输入(非复制、粘贴)博文中的代码,调试并运行。
3.
在上述界面中,增加如下控件:
JLabel,显示文本为“所选文件or文件夹:”;
JTextField,当选择了文件or文件夹后,显示所选择的文件or文件夹。
4. 完成上述功能。
项目二:图片显示
目的:了解GUI的图像显示的常用方式。
目标:利用项目一,获得需要显示的图像文件,显示在界面上(参考:
java的swing中用谁什麽控件来装一张图片?,
《Java程序设计》第14周实验作业:GUI编程初步)
正如我们所看到,我要完成的是目标一跟目标二
目标一,让我们按照老师所给的代码添加控件JLabel和JTexField。。。。。。。。
好吧,第二个JTexField真的有点难倒我了,添加了控件之后根本不知道怎么显示选择的文件or文件夹,后来看到老师给的代码中最后一行的注释写着把文件路径显示在文本框中,突然灵机一闪,想到能不能稍微改一改让它显示文件名呢?
然后我就作死的把那个代码改来改去的。。。。终于,我搞定了!!!!普天同庆!!举国欢呼!!
好吧,话不多说,上代码
<pre name="code" class="java"><span style="font-size:14px;">package cjy;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
public class FileChooser extends JFrame implements ActionListener{
JButton open=null;
JTextField jtfPath = null;
private JTextField txtor;
public static void main(String[] args) {
new FileChooser();
}
public FileChooser(){
// 按钮初始化
open=new JButton("open");
open.setBounds(187, 5, 72, 23);
// 添加监听
open.addActionListener(this);
getContentPane().setLayout(null);
// 把按钮添加到JFrame容器中
getContentPane().add(open);
// 添加文本框控件
jtfPath = new JTextField("选择的文件",40);
jtfPath.setBounds(264, 6, 246, 21);
jtfPath.setEditable(false); // 不可编辑
jtfPath.setHorizontalAlignment(JTextField.CENTER); // 居中
getContentPane().add(jtfPath);
JLabel lblNewLabel = new JLabel("所选文件or文件夹");
lblNewLabel.setBounds(140, 63, 119, 15);
lblNewLabel.setBackground(new Color(128, 128, 128));
lblNewLabel.setForeground(new Color(0, 0, 0));
getContentPane().add(lblNewLabel);
txtor = new JTextField();
txtor.setText("\u6240\u9009\u6587\u4EF6or\u6587\u4EF6\u5939");
txtor.setBounds(279, 60, 356, 21);
getContentPane().add(txtor);
txtor.setColumns(10);
// 设置JFrame的大小,可显示,默认关闭按钮
this.setBounds(400, 200, 900, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
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());
// 把文件路径显示在文本框中
jtfPath.setText(file.getAbsolutePath());
txtor.setText("文件:"+jfc.getSelectedFile().getName());
}
}</span>
接下来是运行效果图
千辛万苦,我的目标一已完成!
下面是目标二
首先是老师给的代码
<span style="font-size:14px;">import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class myFileFilter implements FileFilter{
@Override
public boolean accept(File pathname) {
String filename = pathname.getName().toLowerCase();
if(filename.contains(".jpg")){
return false;
}else{
return true;
}
}
}
public class FileChooser extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JButton open=null;
JTextField jtfPath = null;
JLabel jlbImg = null;
JButton btnNext = null;
String strPath = ""; //文件夹路径
String strFileName = ""; //文件名称
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
public static void main(String[] args) {
new FileChooser();
}
public FileChooser(){
this.setTitle("Week16");
// 设置布局方式
this.setLayout(new FlowLayout());
// 按钮初始化
open=new JButton("选择目录");
// 添加监听
open.addActionListener(this);
// 把按钮添加到JFrame容器中
this.add(open);
// 添加文本框控件
jtfPath = new JTextField("选择的文件",40);
jtfPath.setEditable(false); // 不可编辑
jtfPath.setHorizontalAlignment(JTextField.CENTER); // 居中
this.add(jtfPath);
// 显示下一张图片
btnNext = new JButton("显示下一张");
this.add(btnNext);
btnNext.addActionListener(this);
// 添加显示Image的JLabel控件
jlbImg = new JLabel();
jlbImg.setBackground(Color.RED);
jlbImg.setBounds(100, 100, 200, 200);
this.add(jlbImg);
// 设置JFrame的大小,可显示,默认关闭按钮
this.setBounds(400, 200, 700, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//多个组件需要监听的情况下,事件响应的编码方式
if(e.getSource()==open){ //如果是open按钮
JFileChooser jfc=new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
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());
// 把文件路径显示在文本框中
jtfPath.setText(file.getAbsolutePath());
//jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));
// 获取文件路径 与文件名
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory()){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用
// http://zhouzaibao.iteye.com/blog/347557 ;
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
if(e.getSource()==btnNext){ //如果是next按钮
String strTmp = fileArray[index].toString();
index++;
if(index==NUM_IMG)
index = 0;
jlbImg.setIcon(new ImageIcon(strTmp));
}
}
}</span>
运行结果
好了,目标二也完成了