I'm doing a college job where we have to do a kind of login screen, however, with array
and not Arraylist
.
My question is how do I pass information from JTexfield
to array
?
This array
needs to have the functions of adding a new student, delete and search and need to stay in another class, that is, I will have a class with the graphical interface and another with array
and I want to call this array
with all those functions that I mentioned earlier in this graphical interface.
My GUI code:
Main Class:
package pacote1;
import javax.swing.JFrame;
public class pricipal {
public static void main(String[] args) {
Janela janela = new Janela();
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
janela.setSize(550,300);
janela.setVisible(true);
janela.setLocationRelativeTo(null);
}
}
Graphical interface class:
package pacote1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javafx.scene.control.TextArea;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Janela extends JFrame {
private JTextField nomeField;
private JTextField matriculaField;
public Janela() {
super("Nome da janela");
criarFormulario();
}
private void criarFormulario() {
JTextArea textarea;
JButton button;
JLabel label;
setLayout(new BorderLayout());
setLayout(new FlowLayout());
JPanel panelTitulo = new JPanel();
panelTitulo.setLayout(new FlowLayout());
JLabel titulo = new JLabel("");
titulo.setFont(new Font("Verdana", Font.PLAIN, 16));
panelTitulo.add(titulo);
JPanel panelCadastro = new JPanel();
panelCadastro.setLayout(new FlowLayout());
JLabel nomeLabel = new JLabel("Nome");
nomeField = new JTextField(15);
JLabel matriculaLabel = new JLabel("Matricula");
matriculaField = new JTextField(15);
panelCadastro.add(nomeLabel);
panelCadastro.add(nomeField);
panelCadastro.add(matriculaLabel);
panelCadastro.add(matriculaField);
JPanel panelBotoes = new JPanel();
panelBotoes.setLayout(new FlowLayout());
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
add(panelTitulo, BorderLayout.NORTH);
add(panelCadastro, BorderLayout.CENTER);
add(panelBotoes, BorderLayout.SOUTH);
final JTextArea textArea;
final JTextField texto;
final JComboBox combo;
JButton btn1,btn2,btn3,btn4,btn5;
FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
FlowLayout JButton = new FlowLayout(FlowLayout.CENTER);
c.setLayout(layout);
c.setLayout(JButton);
textArea = new JTextArea(4, 25);
textArea.setLineWrap(true);
JScrollPane spDescricao = new JScrollPane( textArea );
this.getContentPane().add(spDescricao);
//texto = new JTextField(10);
String nomes[] = {"Ens.Médio","Universitário"};
combo = new JComboBox(nomes);
btn1 = new JButton("Novo aluno");
btn2 = new JButton("Excluir aluno");
btn3= new JButton("Consultar aluno");
btn4 = new JButton("fechar");
btn5 = new JButton("teste");
btn1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.append(nomeField.getText());
textArea.append(matriculaField.getText());
}});
btn4.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
combo.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//textArea.append(JComboBox.getSelectedItem());
}});
btn5.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int i = 1;
//textArea.append(valores[i].toPrint());
}});
// c.add(texto);
c.add(combo);
c.add(btn1);
c.add(btn2);
c.add(btn3);
c.add(btn4);
c.add(btn5);
c.add(textArea);
}
}