(高分求解)AWT中FileDialog表示位置的问题 FileDialog表示的时候直接表示在左上边了,如何定位FileDialog的初始表示位置。最好能够有个例子。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 setLocationpublic void setLocation(int x,int y) 调一下setLocationRelativeTo试试看,参数用父窗口。 用DefaultToolkitjava.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();然后可以 setBounds((screenSize.width-宽度)/2, (screenSize.height-高度)/2, 宽度, 高度);试试看吧! 上面这些都式过了,没有用,一样不好用,不知道是不是Java的bug 楼上的朋友能提供一下代码吗?我去sun的网站看过,也有这样的问题不过也没有结决。而且说这不是jdk的bug。不知道对策了。 你创建FileDialog对象的时候,有没有明确指定他们父窗口是谁?然后在显求出来之前有没有调用一下setLocationRelativeTo? 现在你可以用不FileDialog了,可以用javax.swing.JFileChooser,它比FileDialog功能更强大 楼主看看我的试试: 一个文件分割程序 点 打开 可以有 FileDialogimport java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;class Fen{ String fileName; int size; public Fen(String fileName,String size){ this.fileName = fileName; this.size = Integer.parseInt(size)*1024; } public void cut()throws Exception{ int maxx = 0; File inFile = new File(fileName); int fileLength = (int)inFile.length(); //取得文件的大小 int value; //取得要分割的个数 RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件 value = fileLength/size; int i=0; int j=0; //根据要分割的数目输出文件 for (;j<value;j++){ File outFile = new File(inFile.getName()+j+"zzii"); RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); maxx+=size; for (;i<maxx;i++){ outt.write(inn.read()); } outt.close(); } File outFile = new File(inFile.getName()+j+"zzii"); RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); for(;i<fileLength;i++){ outt.write(inn.read()); } outt.close(); inn.close(); }} class He{ String fileName; String filterName; He(String fileName,String filterName){ this.fileName = fileName; this.filterName = filterName; } public void unite()throws Exception{ String [] tt; File inFile = new File("."); //在当前目录下的文件 File outFile = new File(fileName); //取得输出名 RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); //取得符合条件的文件名 tt = inFile.list(new FilenameFilter(){ public boolean accept(File dir,String name){ String rr = new File(name).toString(); return rr.endsWith(filterName); } }); //打印出取得的文件名 for (int i = 0;i<tt.length;i++){ System.out.println(tt[i]); } //打开所有的文件再写入到一个文件里 for(int i=0;i<tt.length;i++){ inFile = new File(tt[i]); RandomAccessFile inn= new RandomAccessFile(inFile,"r"); int c; while((c=inn.read())!=-1) outt.write(c); } outt.close(); } }public class fenge extends JFrame implements ActionListener { Fen cutt; He hee; int size; String finalfilename; String sameend; File myfile; JPanel jp1; JPanel jp2; JLabel jl1; JTextField jt; JButton btn_1 ; JLabel jl2; JTextField jt1; JButton btn_2; JLabel jl3; JTextField jt2; JLabel jl4; JTextField jt3; JButton btn_3; fenge() { super("文件分割器"); setLayout(new GridLayout(2,1)); jp1=new JPanel(new FlowLayout()); jp2=new JPanel(new FlowLayout()); jl1=new JLabel("请选择要分割的文件"); jt=new JTextField(10); btn_1=new JButton("打开"); jl2=new JLabel("请输入分割后每个文件大小(k)"); jt1=new JTextField(4); btn_2=new JButton("开始分割"); jl3=new JLabel("请输入合并后的文件名:"); jt2=new JTextField(10); jl4=new JLabel("请输入要合并文件的同样后缀名"); jt3=new JTextField(10); btn_3=new JButton("开始合并"); jp1.add(jl1); jp1.add(jt); jp1.add(btn_1); jp1.add(jl2); jp1.add(jt1); jp1.add(btn_2); jp2.add(jl3); jp2.add(jt2); jp2.add(jl4); jp2.add(jt3); jp2.add(btn_3); getContentPane().add(jp1); getContentPane().add(jp2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600,400); setLocation(100,100); btn_1.addActionListener(this); btn_2.addActionListener(this); btn_3.addActionListener(this); show(); } public static void main(final String [] args){ fenge fg=new fenge(); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="打开") { FileDialog fd=new FileDialog(this,"打开文件",FileDialog.LOAD); fd.show(); jt.setText(fd.getFile()); myfile=new File(fd.getDirectory()+fd.getFile()); } else if(e.getActionCommand()=="开始分割") { size=Integer.parseInt(jt1.getText()); cutt=new Fen(myfile.toString(),""+size); try{ cutt.cut(); }catch(Exception ie){System.out.println(ie);} } else if(e.getActionCommand()=="开始合并") { finalfilename=jt2.getText(); sameend=jt3.getText(); try{ He hee = new He(finalfilename,sameend); hee.unite(); }catch(Exception ie){System.out.println(ie);} } }} 楼上的朋友,谢谢你的代码,不过想象是一样的,还是显示在主窗体的左上角。其他的朋友的说法我也都试验过了,但是都不行,大家可以自己试验一下,不管怎样设置,都是显示在Parent的左上角。我去sun的网站看了,说这个不是bug,不知道怎样设定才能定位FileDialog。 关于正则表达式的问题 简单的一小段代码,有看不懂的地方,求讲解 要想一道题目的原题 环境变量下的classpath 与 project 中的 external jar 是那个先查找相应类 新手...为什么这个applet程序能够成功编译,但是浏览器中却没能输出? 关于面向接口编程的一点疑惑 信阳广播电视大学这样的学校还有必要再读下去吗? jsp初级问题,jsp+tomcat+数据库? 一个最简单的服务器与客户机的乱码问题 关于jbuilder5注册问题? 关于关闭GUI 请问使用NIO怎样将内存中的非byte类型数据写到硬盘上?
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
然后可以 setBounds((screenSize.width-宽度)/2, (screenSize.height-高度)/2, 宽度, 高度);试试看吧!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Fen{
String fileName;
int size; public Fen(String fileName,String size){
this.fileName = fileName;
this.size = Integer.parseInt(size)*1024;
} public void cut()throws Exception{
int maxx = 0;
File inFile = new File(fileName); int fileLength = (int)inFile.length(); //取得文件的大小
int value; //取得要分割的个数 RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
value = fileLength/size; int i=0;
int j=0; //根据要分割的数目输出文件
for (;j<value;j++){
File outFile = new File(inFile.getName()+j+"zzii");
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
maxx+=size;
for (;i<maxx;i++){
outt.write(inn.read());
}
outt.close();
}
File outFile = new File(inFile.getName()+j+"zzii");
RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
for(;i<fileLength;i++){ outt.write(inn.read());
}
outt.close(); inn.close();
}
}
class He{
String fileName;
String filterName; He(String fileName,String filterName){
this.fileName = fileName;
this.filterName = filterName;
}
public void unite()throws Exception{
String [] tt;
File inFile = new File("."); //在当前目录下的文件
File outFile = new File(fileName); //取得输出名
RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); //取得符合条件的文件名
tt = inFile.list(new FilenameFilter(){
public boolean accept(File dir,String name){
String rr = new File(name).toString();
return rr.endsWith(filterName);
}
});
//打印出取得的文件名
for (int i = 0;i<tt.length;i++){
System.out.println(tt[i]);
} //打开所有的文件再写入到一个文件里
for(int i=0;i<tt.length;i++){
inFile = new File(tt[i]);
RandomAccessFile inn= new RandomAccessFile(inFile,"r");
int c;
while((c=inn.read())!=-1)
outt.write(c);
} outt.close();
}
}
public class fenge extends JFrame implements ActionListener
{
Fen cutt;
He hee;
int size;
String finalfilename;
String sameend;
File myfile;
JPanel jp1;
JPanel jp2;
JLabel jl1;
JTextField jt;
JButton btn_1 ;
JLabel jl2;
JTextField jt1;
JButton btn_2;
JLabel jl3;
JTextField jt2;
JLabel jl4;
JTextField jt3;
JButton btn_3;
fenge()
{
super("文件分割器");
setLayout(new GridLayout(2,1));
jp1=new JPanel(new FlowLayout());
jp2=new JPanel(new FlowLayout());
jl1=new JLabel("请选择要分割的文件");
jt=new JTextField(10);
btn_1=new JButton("打开");
jl2=new JLabel("请输入分割后每个文件大小(k)");
jt1=new JTextField(4);
btn_2=new JButton("开始分割");
jl3=new JLabel("请输入合并后的文件名:");
jt2=new JTextField(10);
jl4=new JLabel("请输入要合并文件的同样后缀名");
jt3=new JTextField(10);
btn_3=new JButton("开始合并");
jp1.add(jl1);
jp1.add(jt);
jp1.add(btn_1);
jp1.add(jl2);
jp1.add(jt1);
jp1.add(btn_2);
jp2.add(jl3);
jp2.add(jt2);
jp2.add(jl4);
jp2.add(jt3);
jp2.add(btn_3);
getContentPane().add(jp1);
getContentPane().add(jp2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
setLocation(100,100);
btn_1.addActionListener(this);
btn_2.addActionListener(this);
btn_3.addActionListener(this);
show(); }
public static void main(final String [] args){
fenge fg=new fenge();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
{
FileDialog fd=new FileDialog(this,"打开文件",FileDialog.LOAD);
fd.show();
jt.setText(fd.getFile());
myfile=new File(fd.getDirectory()+fd.getFile());
}
else if(e.getActionCommand()=="开始分割")
{
size=Integer.parseInt(jt1.getText());
cutt=new Fen(myfile.toString(),""+size);
try{
cutt.cut();
}catch(Exception ie){System.out.println(ie);}
}
else if(e.getActionCommand()=="开始合并")
{
finalfilename=jt2.getText();
sameend=jt3.getText();
try{
He hee = new He(finalfilename,sameend);
hee.unite();
}catch(Exception ie){System.out.println(ie);}
} }
}
其他的朋友的说法我也都试验过了,但是都不行,大家可以自己试验一下,不管怎样
设置,都是显示在Parent的左上角。我去sun的网站看了,说这个不是bug,不知道怎样设定才能定位FileDialog。