0
我有一些問題。我不知道這是什麼叫。java - 無法獲得具有不同字段的初始化
class test{
JButton button=new JButton("button");
JFileChooser fc=new JFileChooser() {
@Override
public void approveSelection(){
File f = getSelectedFile();
if(f.exists() && getDialogType() == SAVE_DIALOG){
int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
switch(result){
case JOptionPane.YES_OPTION:
super.approveSelection();
return;
case JOptionPane.NO_OPTION:
cancelSelection();
return;
case JOptionPane.CLOSED_OPTION:
return;
case JOptionPane.CANCEL_OPTION:
// cancelSelection();
return;
}
}
super.approveSelection();
}
};
void test()
{
}
}
所以,我必須使用反射來獲取現場:
Class cls=Class.forName("test");
Field[]field=cls.getDeclaredFields();
for(Field f : field) {
System.out.println (f.getType().getSimpleName()+ " : "+ f.getType());
}
輸出如下:
JButton : class javax.swing.JButton
JFileChooser : class javax.swing.JFileChooser
那麼,如何獲得所有對象類內初始化的JFileChooser像File f
,JOptionPane
等?
你將打印在類聲明的所有字段的代碼。類中的代碼訪問的類與聲明的字段無關。 –
那麼,我該怎麼做才能讓他們? – newbie
你想達到什麼目的? –