我写了这个Enum来配置我的程序参数...
好吧,我想翻译成一个班级。如果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 :(得分: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;
}
}
我希望这对其他人有用并帮助我准确地编写代码......