您的位置:首页 > 产品设计 > UI/UE

GUI编程及文件对话框的使用

2015-06-25 09:43 393 查看
<pre name="code" class="python">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 Myjava 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 Myjava();

}
public Myjava(){
this.setTitle("Week16");
this.setLayout(new FlowLayout());
open=new JButton("选择目录");
open.addActionListener(this);
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);

jlbImg=new JLabel();
jlbImg.setBackground(Color.red);
jlbImg.setBounds(100,100,200,200);
this.add(jlbImg);

this.setBounds(400,200,400,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
@Override

public void actionPerformed(ActionEvent e){
if(e.getSource()==open){
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(),"查看");
File file=jfc.getSelectedFile();

if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
jtfpath.setText(file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());

}System.out.println(jfc.getSelectedFile().getName());
jtfpath.setText(file.getAbsolutePath());
strPath=file.getAbsolutePath();
strFileName=jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory()){
fileArray=file.listFiles();
NUM_IMG=fileArray.length;

}
}
if(e.getSource()==btnNext){
String strTmp=fileArray[index].toString();
index++;
if(index==NUM_IMG)
index=0;
jlbImg.setIcon(new ImageIcon(strTmp));

}

}
}






第二部分:作业博客要求
1. 在作业博客中,完成上述3个项目中至少2个,并把运行结果、代码写到博客中。2. 在作业博客中,回答以下三个问题:(1)简述Java中,GUI的事件驱动模式。

事件处理模式,各种事件监听器。。。。

(2)简述Java中,字符串转化为数值、数值转化为字符串的常用方式。

字符串型转换成各种数字类型:    String s = "169";   byte b = Byte.parseByte( s );  short t = Short.parseShort( s );  int i = Integer.parseInt( s );  long l = Long.parseLong( s );  Float f = Float.parseFloat(
s );  Double d = Double.parseDouble( s ); 

数字类型转换成字符串型:    法一:String s = String.valueOf( value); // 其中 value 为任意一种数字类型。  法二:String aa = 1+"";

(3) 简述Java中,JFileChooser使用的核心代码。

   import java.io.File;  import javax.swing.JFileChooser;  import javax.swing.filechooser.FileFilter; public class FileChooserTest {  public static void main(String [] args) {


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