I'm new to programming and built the image GUI from an example from the college book, but the save button has no functionality. And I ask you to implement the button to save some code that will save the data entered in a table.
//mainclass
importjavax.swing.JFrame;publicclassTestCap1{publicstaticvoidmain(String[]args){ClientFramefClient=newClientFrame();fClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fClient.setSize(530,260);fClient.setVisible(true);}}
//Secondaryclass
importjavax.swing.*;importjavax.swing.border.*;importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.event.*;publicclassClientFrameextendsJFrame{privateJMenuItem[]menuItemCliente;privateJMenuItem[]menuItemAjuda;privateJLabel[]labels;privateJTextField[]fields;privateJLabeltitulo;privateJPanelpanelTitulo,panelCadastro;privateJButton[]botoes;privateJPanelpanelBotoes,panelBase;privateJLabelstatus;publicClientFrame(){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);
}
}