简体   繁体   English

如何将此代码转换为类并重用?

[英]How to convert this code to class and reuse it?


I have written this Enum to configure my program parameters... 我已经写了这个Enum来配置程序参数...
Well i want to translate as a class. 好吧,我想翻译成一类。 Better if a SingletonClass. 如果使用SingletonClass更好。 My big problem is that i don´t know how to set on the run time the parameters values... 我的大问题是我不知道如何在运行时设置参数值...

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;
 }
 }

After reading some post about Singleton in Java and various tests with Netbeans... I had created this Library that work well for me, and seems not to be very heavy... 在阅读了有关Java中的Singleton以及使用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;
  }
}

I hope that can be useful for others and helps me to accurate the code... 我希望这对其他人有用,并帮助我更准确地编写代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM