0
I’m new to programming and built the image GUI from an example of the college book, but the save button has no functionality. And I ask help to implement on the button save some code that saves the informed data in a table.
// main class
import javax.swing.JFrame;
public class TestCap1{
public static void main(String[] args)
{
ClientFrame fClient = new ClientFrame();
fClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fClient.setSize(530,260);
fClient.setVisible(true);
}
}
// secondary class
import javax.swing.*;
import javax.swing.border.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;
public class ClientFrame extends JFrame {
private JMenuItem[] menuItemCliente;
private JMenuItem[] menuItemAjuda;
private JLabel[] labels;
private JTextField[] fields;
private JLabel titulo;
private JPanel panelTitulo, panelCadastro;
private JButton[] botoes;
private JPanel panelBotoes, panelBase;
private JLabel status;
public ClientFrame()
{
super("Cadastro de clientes");
createMenu();
createForm();
createBase();
configAction();
}
//Metodo para criar menu
private void createMenu()
{
//Cria menu cliente
JMenu menuCliente = new JMenu("Cliente");
menuCliente.setMnemonic('C');
menuItemCliente = new JMenuItem[3];
//Cria item novo do menu cliente
menuItemCliente[0] = new JMenuItem("Novo");
menuItemCliente[0].setMnemonic('N');
menuCliente.add(menuItemCliente[0]);
//Cria separador
menuCliente.addSeparator();
//Cria item salvar do cliente
menuItemCliente[1] = new JMenuItem("Salvar");
menuItemCliente[1].setMnemonic('S');
menuCliente.add(menuItemCliente[1]);
//Cria separador
menuCliente.addSeparator();
//Cria item fechar do cliente
menuItemCliente[2] = new JMenuItem("Fechar");
menuItemCliente[2].setMnemonic('F');
menuCliente.add(menuItemCliente[2]);
//Cria separador
menuCliente.addSeparator();
//Cria menu ajuda
JMenu menuAjuda = new JMenu("Ajuda");
menuAjuda.setMnemonic('A');
menuItemAjuda = new JMenuItem[1];
//Cria item novo do menu ajuda
menuItemAjuda[0] = new JMenuItem("Sobre...");
menuItemAjuda[0].setMnemonic('o');
menuAjuda.add(menuItemAjuda[0]);
//Cria barra de menu
JMenuBar barra = new JMenuBar();
setJMenuBar(barra);
barra.add(menuCliente);
barra.add(menuAjuda);
}
private void createForm()
{
//Cria layout
setLayout(new BorderLayout());
//cria panel título
panelTitulo = new JPanel();
panelTitulo.setLayout(new FlowLayout(FlowLayout.LEFT));
titulo = new JLabel("Cadastro de Cliente");
titulo.setFont(new Font("Dialog", Font.PLAIN, 16));
panelTitulo.add(titulo);
add(panelTitulo, BorderLayout.NORTH);
//Cria panel cadastro
panelCadastro = new JPanel();
panelCadastro.setLayout(new FlowLayout(FlowLayout.LEFT));
//Labels
labels = new JLabel[8];
labels[0] = new JLabel("Nome");
labels[1] = new JLabel("Endereco");
labels[2] = new JLabel("Bairro");
labels[3] = new JLabel("Cidade");
labels[4] = new JLabel("Estado");
labels[5] = new JLabel("Telefone");
labels[6] = new JLabel("Celular");
labels[7] = new JLabel("Email");
//Fields
fields = new JTextField[8];
fields[0] = new JTextField(42);
fields[1] = new JTextField(40);
fields[2] = new JTextField(13);
fields[3] = new JTextField(13);
fields[4] = new JTextField(6);
fields[5] = new JTextField(8);
fields[6] = new JTextField(8);
fields[7] = new JTextField(15);
for(int j = 0; j < fields.length; j++)
{
panelCadastro.add(labels[j]);
panelCadastro.add(fields[j]);
}
add(panelCadastro, BorderLayout.CENTER);
}
private void createBase()
{
//cria panel base
panelBase = new JPanel();
panelBase.setLayout(new BorderLayout());
//cria panel botoes
panelBotoes = new JPanel();
panelBotoes.setLayout(new FlowLayout(FlowLayout.RIGHT));
botoes = new JButton[2];
botoes[0] = new JButton("Salvar");
botoes[1] = new JButton("Fechar");
panelBotoes.add(botoes[0]);
panelBotoes.add(botoes[1]);
panelBase.add(panelBotoes, BorderLayout.EAST);
//Criar barra de status
status = new JLabel("Barra de status...");
status.setFont(new Font("Dialog", Font.PLAIN, 12));
status.setBorder(new BevelBorder(BevelBorder.LOWERED));
panelBase.add(status, BorderLayout.SOUTH);
add(panelBase, BorderLayout.SOUTH);
}
private class NovoHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
for(int i = 0; 1 < fields.length; i++)
{
fields[i].setText("");
}
}
}
private class SalvaHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
status.setText("Cliente salvo");
}
}
private class FechaHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
private class SobreHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null, "Construcao GUI", "Sobre",
JOptionPane.PLAIN_MESSAGE);
}
}
private void configAction()
{
SalvaHandler salvaHandler = new SalvaHandler();
FechaHandler fechaHandler = new FechaHandler();
NovoHandler novoHandler = new NovoHandler();
SobreHandler sobreHandler = new SobreHandler();
botoes[0].addActionListener(salvaHandler);
botoes[1].addActionListener(fechaHandler);
menuItemCliente[0].addActionListener(novoHandler);
menuItemCliente[1].addActionListener(salvaHandler);
menuItemCliente[2].addActionListener(fechaHandler);
menuItemAjuda[0].addActionListener(sobreHandler);
}
}
Hello, Bruno. Welcome. Enter the code you used to generate this screen.
– Pablo Almeida
I didn’t because I thought it would be too long.
– Bruno Silva
What do you really want to save? Your doubt is not very clear. Click [Dit] and elaborate better the text of the question, ta half confused.
– user28595
To add a function when the button is clicked, you have to take a look at
Listener
, has all kinds. One of them is for clicks.– DH.