2014-01-22 70 views
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; 
} 
} 
+0

如果你重構它,它會產生幾個類,每個類表示一個const來自enum的螞蟻。你確定要這麼做嗎? –

+0

我不想有幾個類,我相信它會是資源昂貴... – Tilens

+0

我只是想嘗試重寫它有一個類或其他類型,我可以使用(更改參數字段...)我的下一個配置步驟有...傳感器屬性,例如... – Tilens

回答

0

讀了一些關於後和Singleton in Java使用Netbeans各種測試之後...... 我創造了這個圖書館是爲我工作好,似乎不是很沉重......

package com.tilens; 

public class Configurador{ 
    private static Configurador INSTANCIA; 
    private final static Object sync=new Object(); 
    private static List<Campo>campos; 
    private Configurador(){ 
    campos=new ArrayList<>(); 
    System.out.println(Thread.currentThread().getName()+"> Creando Instancia..."); 
    } 
    public static Configurador getInstance(){ 
    synchronized(sync){ 
     if(INSTANCIA==null)INSTANCIA=new Configurador(); 
     else System.out.println(Thread.currentThread().getName()+"> Obteniendo Instancia..."); 
    } 
    return INSTANCIA; 
    } 
    public void addCampo(String caption,String value){ 
    Campo tmp=new Campo(caption,value); 
    campos.add(tmp); 
    } 
    public synchronized int show(JFrame padre){ 
    JPanel pnlContenedor,pnlControles; 
    GridBagConstraints constraints; 

    pnlControles=new JPanel(new GridBagLayout()); 
    pnlControles.setBorder(BorderFactory.createTitledBorder("Campos:")); 

    constraints=new GridBagConstraints(); 
    constraints.gridwidth=1; 
    constraints.gridheight=1; 
    constraints.ipadx=4; 
    constraints.ipady=4; 

    for(int i=0;i<campos.size();i++){ 
     Campo cmp=campos.get(i); 
     constraints.gridx=0; 
     constraints.gridy=i; 
     constraints.anchor=GridBagConstraints.LINE_END; 
     pnlControles.add(cmp.getLabel(),constraints); 
     constraints.gridx=1; 
     constraints.anchor=GridBagConstraints.LINE_START; 
     pnlControles.add(cmp.getField(),constraints); 
     //if(cmpFocus==null) cmpFocus=campo.field; 
    } 

    pnlContenedor=new JPanel(new BorderLayout()); 
    pnlContenedor.setMaximumSize(new Dimension(500,500)); 
    //pnlContenedor.add(new JScrollPane(pnlControles)); 
    pnlContenedor.add((pnlControles)); 
    return JOptionPane.showConfirmDialog(padre,pnlContenedor,"titulo",JOptionPane.OK_CANCEL_OPTION); 
    } 
} 
class Campo{ 
    private final String label; 
    private final Component field; 
    public Campo(String caption,String value){ 
    field=new JTextField(value); 
    label=caption; 
    } 
    public String getCaption(){ 
    return label+": "; 
    } 
    public JLabel getLabel(){ 
    return new JLabel(getCaption()); 
    } 
    public String getFieldClass(){ 
    return field.getClass().getSimpleName(); 
    } 
    public Component getField(){ 
    return field; 
    } 
} 

我希望能爲別人有用,幫助我準確的代碼...

相關問題