I need to find a way to handle the object array to send it to the other frame “Start” I’ve tried various ways but I can’t, I’m just a beginner in the field of OOP, I’ve tried creating methods but still I haven’t been able to solve the program. I use Eclipse
I’ve tried inserting methods and initializing the object on different sides, but I need a method
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;
import javax.swing.SwingConstants;
public class proyecto extends JFrame {
//Delia Indira Pérez Moo
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField txtnombre;
private JTextField txtedad;
private JTextField txtpeso;
private JTextField txtalt;
public int pacientes = 0;
String nombre = "";
int edad = 0;
int peso = 0;
float altura =0;
String sexo ="";
String ID = "";
double IGC = 0;
paciente objPaciente =new paciente(nombre, edad, peso, altura, sexo,ID,IGC);
//arrPacienteObjetos
public paciente arrPaciente [] =new paciente [5];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
proyecto frame = new proyecto();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public proyecto() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 470, 489);
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 128, 128));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Nombre :");
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("Arial", Font.PLAIN, 16));
lblNewLabel.setBounds(57, 69, 113, 13);
contentPane.add(lblNewLabel);
txtnombre = new JTextField();
txtnombre.setBounds(195, 67, 128, 19);
contentPane.add(txtnombre);
txtnombre.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Edad :");
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 16));
lblNewLabel_1.setBounds(57, 130, 113, 13);
contentPane.add(lblNewLabel_1);
txtedad = new JTextField();
txtedad.setBounds(195, 128, 128, 19);
contentPane.add(txtedad);
txtedad.setColumns(10);
txtpeso = new JTextField();
txtpeso.setBounds(195, 189, 128, 19);
contentPane.add(txtpeso);
txtpeso.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Peso (en KG) :");
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("Arial", Font.PLAIN, 16));
lblNewLabel_2.setBounds(57, 191, 128, 13);
contentPane.add(lblNewLabel_2);
JLabel txtaltura = new JLabel("Altura (en Metros) :");
txtaltura.setForeground(new Color(255, 255, 255));
txtaltura.setFont(new Font("Arial", Font.PLAIN, 16));
txtaltura.setBounds(57, 245, 142, 19);
contentPane.add(txtaltura);
txtalt = new JTextField();
txtalt.setBounds(195, 243, 128, 20);
contentPane.add(txtalt);
txtalt.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("Sexo :");
lblNewLabel_4.setForeground(new Color(255, 255, 255));
lblNewLabel_4.setFont(new Font("Arial", Font.PLAIN, 16));
lblNewLabel_4.setBounds(57, 311, 113, 19);
contentPane.add(lblNewLabel_4);
JRadioButton radiobtnhombre = new JRadioButton("Hombre");
radiobtnhombre.setForeground(new Color(255, 255, 255));
radiobtnhombre.setBackground(new Color(0, 128, 128));
radiobtnhombre.setFont(new Font("Arial", Font.PLAIN, 16));
radiobtnhombre.setBounds(176, 311, 85, 23);
contentPane.add(radiobtnhombre);
JRadioButton radiobtnmujer = new JRadioButton("Mujer");
radiobtnmujer.setForeground(new Color(255, 255, 255));
radiobtnmujer.setBackground(new Color(0, 128, 128));
radiobtnmujer.setFont(new Font("Arial", Font.PLAIN, 16));
radiobtnmujer.setBounds(274, 311, 70, 23);
contentPane.add(radiobtnmujer);
JButton btnsave = new JButton("Guardar Paciente");
btnsave.setFont(new Font("Arial", Font.PLAIN, 16));
btnsave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nombre = txtnombre.getText().trim();
edad = Integer.parseInt(txtedad.getText().trim());
peso = Integer.parseInt(txtpeso.getText().trim());
altura = Float.parseFloat(txtalt.getText().trim());
sexo = radiobtnhombre.isSelected() ? "Hombre" : "Mujer";
ID = "";
IGC = 0;
// Añadir el objeto paciente al arreglo
arrPaciente[pacientes] = objPaciente;
// Comprobar que todos los campos están completos
if (!nombre.isEmpty() && !txtedad.getText().trim().isEmpty() &&
!txtpeso.getText().trim().isEmpty() && !txtalt.getText().trim().isEmpty()) {
if (pacientes < 5) {
// Crear el ID del paciente
ID = (nombre.substring(0, Math.min(nombre.length(), 3)) +
Integer.toString(edad).substring(0, Math.min(Integer.toString(edad).length(), 2))).toUpperCase();
// arr objetos(paciente)
arrPaciente[pacientes] = objPaciente;
// Mostrar el ID al usuario
JOptionPane.showMessageDialog(null, "Tu ID es " + ID, "Mensaje", JOptionPane.INFORMATION_MESSAGE);
pacientes++;
// Limpiar los campos de texto y los radio buttons
txtnombre.setText("");
txtedad.setText("");
txtpeso.setText("");
txtalt.setText("");
radiobtnhombre.setSelected(false);
radiobtnmujer.setSelected(false);
} else {
JOptionPane.showMessageDialog(null, "Se ha alcanzado el límite máximo de pacientes.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "Por favor completa todos los campos.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
btnsave.setBounds(119, 371, 204, 32);
contentPane.add(btnsave);
JLabel lblNewLabel_7 = new JLabel("REGISTRO");
lblNewLabel_7.setForeground(new Color(255, 255, 255));
lblNewLabel_7.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_7.setFont(new Font("Arial", Font.PLAIN, 25));
lblNewLabel_7.setBounds(10, 24, 434, 23);
contentPane.add(lblNewLabel_7);
JButton btnRegresar = new JButton("Regresar");
btnRegresar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
arrPaciente[pacientes] = objPaciente;
INICIO INICIO1 = new INICIO();
INICIO1.arrP = arrPaciente;
INICIO1.setVisible(true); // Mostrar el frame INICIO1
dispose(); // Cerrar el frame actual (proyecto)
//INICIO INICIO1 = new INICIO(arrPaciente);
//INICIO1.arrP = arrPaciente;
//INICIO1.setVisible(true);
}
});
btnRegresar.setBounds(355, 416, 89, 23);
contentPane.add(btnRegresar);
}
}
2nd class
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.MutableComboBoxModel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class INICIO extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
//declarar el arreglo
public paciente arrP[];
private DefaultComboBoxModel<String> modelComboBox=new DefaultComboBoxModel<>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
INICIO frame = new INICIO();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public INICIO() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 471, 486);
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 128, 128));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btncalc = new JButton("Calcular y Mostrar IGC");
btncalc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*String IDseleccionado = (String) comboBox.getSelectedItem();
//menor
for (int i = 0; i < arrPaciente.length; i++) { //may
if (IDseleccionado.equals((arrPaciente[i].getNombre().substring(0, 3) + arrPaciente[i].getEdad()).toUpperCase())) {
try {
double pesoPaciente = Double.parseDouble(arrPaciente[i].getPeso());
double alturaPaciente = Double.parseDouble(arrPaciente[i].getAltura());
int edadPaciente = arrPaciente[i].getEdad();
double sexoPaciente = arrPaciente[i].getSexo().equals("Hombre") ? 1.0 : 0.0;
double IGC = (1.20 * pesoPaciente / (alturaPaciente * alturaPaciente)) + (0.23 * edadPaciente) - (10.8 * sexoPaciente) - 5.4;
arrPaciente[i].setIGC(IGC);
JOptionPane.showMessageDialog(null, "Tu IGC es " + IGC, "Mensaje", JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Error al calcular el IGC. Verifica los datos.", "Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}*/
}
});
btncalc.setFont(new Font("Arial", Font.PLAIN, 16));
btncalc.setBounds(81, 235, 296, 36);
contentPane.add(btncalc);
JLabel lblNewLabel_5 = new JLabel("Seleccionar ID paciente");
lblNewLabel_5.setForeground(new Color(255, 255, 255));
lblNewLabel_5.setFont(new Font("Arial", Font.PLAIN, 16));
lblNewLabel_5.setBounds(134, 126, 191, 36);
contentPane.add(lblNewLabel_5);
JButton btnmostrar = new JButton("Mostrar dietas de paciente seleccionado");
btnmostrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnmostrar.setFont(new Font("Arial", Font.PLAIN, 16));
btnmostrar.setBounds(68, 304, 323, 32);
contentPane.add(btnmostrar);
JLabel lblNewLabel_3 = new JLabel("QUE DIETA NECESITAS?");
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("Arial", Font.PLAIN, 25));
lblNewLabel_3.setBounds(78, 60, 302, 50);
contentPane.add(lblNewLabel_3);
JButton btnNewButton = new JButton("Crear ID");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
proyecto proyecto1 = new proyecto();
proyecto1.setVisible(true);
}
});
btnNewButton.setBounds(356, 399, 89, 23);
contentPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("Si no tienes un ID, Crealo:");
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setBounds(216, 403, 138, 14);
contentPane.add(lblNewLabel);
JComboBox<String> comboBox_1 = new JComboBox();
comboBox_1.setBounds(84, 173, 307, 22);
contentPane.add(comboBox_1);
//for lleno el modelo
for(int a=0; a<arrP.length; a++) {
comboBox_1.addItem(arrP[a].getID());
comboBox_1.setModel(modelComboBox);
}
}
}
public class paciente {
private String nombre;
private int edad;
private int peso;
private float altura;
private String sexo;
public String ID;
private double IGC;
public paciente (String nombre,int edad, int peso,float altura, String sexo,String ID, double IGC) {
this.nombre=nombre;
this.edad=edad;
this.peso=peso;
this.altura=altura;
this.sexo=sexo;
this.ID=ID;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public int getPeso() {
return peso;
}
public void setPeso(int peso) {
this.peso = peso;
}
public float getAltura() {
return altura;
}
public void setAltura(float altura) {
this.altura = altura;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public double getIGC() {
return IGC;
}
public void setIGC(double iGC) {
IGC = iGC;
}
}
I would like the data of my fix to go to the “INICIO” combo box but it is not possible because it marks me that the fix is as “null”
BigDaddyX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.