Java - consulta

 
Vista:

consulta

Publicado por Andrés (3 intervenciones) el 13/11/2006 15:37:27
Hola alguien sabe como hago para cargar desde una JTextField a un JList.

Yo hice esto pero me da error:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class formulario extends JFrame {

JLabel LNumero1;
JTextField Numero1;
JButton AgregarNum1;
JList Lista1;
DefaultListModel Modelo;
JScrollPane panel1;
Container Principal;

public formulario() {
super("Formulario");
LNumero1=new JLabel("Número");
Numero1=new JTextField("");
AgregarNum1=new JButton("Agregar");
Modelo=new DefaultListModel();
Lista1= new JList(Modelo);
Lista1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
InicializarFrame();
CargarControles();
AgregarNum1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
agrega();
}
});

FinalizarFrame();

}
public void InicializarFrame() {

Principal=getContentPane();
Principal.setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public void CargarControles() {
panel1= new JScrollPane(Lista1);
Principal.add(LNumero1);
Principal.add(Numero1);
Principal.add(AgregarNum1);
Principal.add(panel1);
LNumero1.setBounds(20,20,80,20);
Numero1.setBounds(120,20,110,20);
AgregarNum1.setBounds(50,80,100,20);
panel1.setBounds(20,120,150,150);
}
public void FinalizarFrame() {
pack();
setSize(500,400);
setVisible(true);
}
public void agrega() {
Lista1.add(Numero1.getNumero1());


public static void main(String[]args ) {
new formulario();
}

}

Me da error en el procedimiento agrega,como puedo hacer?
Gracias.-
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:consulta

Publicado por neossoftware (622 intervenciones) el 13/11/2006 16:20:17
Tu problema es sencillo de resolver:

Solo que te recomiendo que mejores tus practicas de codificación por ejemplo los nombre de objetos no deben comenzar con mayusculas ;)

Echale un ojo a esta pagina

http://www.programacion.com/java/tutorial/convenciones/

Ademas te recomiendo que leas el tutorial de Sun.

http://java.sun.com/docs/books/tutorial/uiswing/index.html

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Boston, MA 02111.
* */

package org.neos.spring;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Formulario extends JFrame {

JLabel LNumero1;

JTextField Numero1;

JButton AgregarNum1;

JList Lista1;

DefaultListModel Modelo;

JScrollPane panel1;

Container Principal;

public Formulario() {
super("Formulario");
LNumero1 = new JLabel("Número");
Numero1 = new JTextField("");
AgregarNum1 = new JButton("Agregar");
Modelo = new DefaultListModel();
Lista1 = new JList(Modelo);
Lista1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
InicializarFrame();
CargarControles();
AgregarNum1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
agrega();
}
});

FinalizarFrame();

}

public void InicializarFrame() {

Principal = getContentPane();
Principal.setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void CargarControles() {
panel1 = new JScrollPane(Lista1);
Principal.add(LNumero1);
Principal.add(Numero1);
Principal.add(AgregarNum1);
Principal.add(panel1);
LNumero1.setBounds(20, 20, 80, 20);
Numero1.setBounds(120, 20, 110, 20);
AgregarNum1.setBounds(50, 80, 100, 20);
panel1.setBounds(20, 120, 150, 150);
}

public void FinalizarFrame() {
pack();
setSize(500, 400);
setVisible(true);
}

public void agrega() {

Modelo.addElement(Numero1.getText());

}

public static void main(String[] args) {
new Formulario();
}

}

SAludos comunidad Open Source
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar