0
我已經寫了這個枚舉來配置我的程序參數...
那麼我想翻譯爲一個類。如果是SingletonClass,則更好。 我的大問題是,我不知道如何在運行時的參數值設置...如何將此代碼轉換爲類並重用它?
package GUI;
public enum ConfigGeneral{
DEBUG("Modo Depuración",false),
frtFechasInfo("Formato Fechas GUI",new String[]{"yyyy-MM-dd HH:mm:ss","dd/MM/yyyy HH:mm:ss"},"dd/MM/yyyy HH:mm:ss"),
timeZoneInfo("Zona Horaria Fechas GUI",TimeZone.getAvailableIDs(),"Europe/Madrid"),
pathDBFile("Ruta Base Datos","SidmarSenseData.db");
private final String label;
private final Component field;
ConfigGeneral(String caption,boolean valor){
label=caption;
field=new JCheckBox("",valor);
}
ConfigGeneral(String caption,String valor){
label=caption;
field=new JTextField(valor,30);
}//fin constructor tipo String
ConfigGeneral(String caption,String[] valor){
label=caption;
field=new JComboBox<>(valor);
}//fin constructor tipo Array
ConfigGeneral(String caption,String[] valor,String pordefecto){
label=caption;
field=new JComboBox<>(valor);
((JComboBox)this.field).setSelectedItem(pordefecto);
}//fin constructor tipo Array
public int show(JFrame padre,boolean nueva){
JPanel pnlCampos;
Component cmpFocus;
GridBagConstraints constraints;
cmpFocus=null;
constraints=new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.ipadx=4;
constraints.ipady=4;
pnlCampos=new JPanel(new GridBagLayout());
pnlCampos.setBorder(BorderFactory.createTitledBorder("Parámetros Generales..."));
int fila=0;
for(ConfigGeneral campo:ConfigGeneral.values()){
JLabel caption=new JLabel(campo.label);
constraints.gridx=0;
constraints.gridy=fila;
constraints.anchor=GridBagConstraints.LINE_END;
pnlCampos.add(caption,constraints);
constraints.gridx=1;
constraints.anchor=GridBagConstraints.LINE_START;
pnlCampos.add(campo.field,constraints);
if(cmpFocus==null) cmpFocus=campo.field;
fila++;
}
// establece el foco en el campo indicado
// begin workaround
cmpFocus.addHierarchyListener(new java.awt.event.HierarchyListener(){
@Override public void hierarchyChanged(java.awt.event.HierarchyEvent e){
final Component c=e.getComponent();
if(c.isShowing() && (e.getChangeFlags() & java.awt.event.HierarchyEvent.SHOWING_CHANGED)!=0){
javax.swing.SwingUtilities.invokeLater(new Runnable(){@Override public void run(){c.requestFocus();}});
}
}
});
// end workaround
// Un panel para contenerlo todo y disponerlo de forma adecuado
JPanel panelPrincipal=new JPanel(new BorderLayout());
panelPrincipal.add(new JLabel("<html>Parámetros Generales de configuración.</html>"),java.awt.BorderLayout.NORTH);
panelPrincipal.add(pnlCampos);
int resp=JOptionPane.showConfirmDialog(padre,panelPrincipal,"Configuración General",JOptionPane.OK_CANCEL_OPTION);
return resp;
}
}
如果你重構它,它會產生幾個類,每個類表示一個const來自enum的螞蟻。你確定要這麼做嗎? –
我不想有幾個類,我相信它會是資源昂貴... – Tilens
我只是想嘗試重寫它有一個類或其他類型,我可以使用(更改參數字段...)我的下一個配置步驟有...傳感器屬性,例如... – Tilens