olá
ao clicar no submenu Inserir, eu queria que ele puxasse o a classe Inserir, assim irei fazer Inserir/Editar/Excluir etc, mas ele dá um null point exception
segue o codigo:
public class Principal {
public static void main(String[] args) {
new Index();
}
}[/code]
[code]import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Index extends JFrame{
JMenuBar barra;
JMenu menu1;
JMenuItem item1;
Inserir insere;
public Index()
{
JFrame frame = new JFrame();
frame.setSize(500, 250);
this.barra = new JMenuBar();
this.menu1 = new JMenu("Ações");
this.item1 = new JMenuItem ("Inserir");
barra.add(menu1);
menu1.add(item1);
frame.setJMenuBar(barra);
item1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
insere = new Inserir();
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[code]import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Inserir{
private static final long serialVersionUID = 1L;
Pessoa pessoa = new Pessoa();
JButton ok, limpa;
JFrame frame;
ButtonGroup bgroup;
JRadioButton sexoM ,sexoF, sexoI;
JTextField nome, telefone, endereco, idade;
JLabel nomeLabel, telefoneLabel, enderecoLabel, idadeLabel, sexoLabel;
public Inserir(){
super();
Container cont = frame.getContentPane();
this.ok = new JButton("OK");
this.limpa = new JButton("Limpar");
this.sexoM = new JRadioButton("Masculino");
this.sexoF = new JRadioButton("Feminino");
this.sexoI = new JRadioButton("Indeciso");
this.nome = new JTextField(20);
this.telefone = new JTextField(20);
this.endereco = new JTextField(20);
this.idade = new JTextField(5);
this.sexoLabel = new JLabel("Sexo:");
this.nomeLabel = new JLabel("Nome:");
this.enderecoLabel = new JLabel("Endereço:");
this.telefoneLabel = new JLabel("Telefone:");
this.idadeLabel = new JLabel("Idade:");
cont.setLayout(null);
idade.setBounds(120,40,30,20);
idadeLabel.setBounds(20,40,100,20);
sexoLabel.setBounds(20, 60, 100, 20);
sexoM.setBounds(120, 60, 100, 20);
sexoF.setBounds(220, 60, 100, 20);
sexoI.setBounds(320, 60, 100, 20);
nomeLabel.setBounds(20,20,100,20);
nome.setBounds(120, 20, 180, 20);
telefoneLabel.setBounds(20,80,100,20);
telefone.setBounds(120,80,100,20);
enderecoLabel.setBounds(20,100,100,20);
endereco.setBounds(120,100,200,20);
ok.setBounds(90,150,80,25);
limpa.setBounds(250,150,120,25);
this.bgroup = new ButtonGroup();
bgroup.add(sexoM);
bgroup.add(sexoF);
bgroup.add(sexoI);
cont.add(sexoLabel);
cont.add(sexoM);
cont.add(sexoF);
cont.add(sexoI);
cont.add(ok);
cont.add(limpa);
cont.add(nomeLabel);
cont.add(nome);
cont.add(enderecoLabel);
cont.add(endereco);
cont.add(telefoneLabel);
cont.add(telefone);
cont.add(idade);
cont.add(idadeLabel);
ok.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
pessoa.setNome(nome.getText());
pessoa.setEndereco(endereco.getText());
int idadeNumero = Integer.parseInt(idade.getText());
pessoa.setIdade(idadeNumero);
if (sexoM.isSelected())
{
pessoa.setSexo("Masculino");
}
else
{
if (sexoF.isSelected())
pessoa.setSexo("Feminino");
else
pessoa.setSexo("Sexo Indeciso");
}
int telefoneNumero = Integer.parseInt(telefone.getText());
pessoa.setTelefone(telefoneNumero);
//System.out.println(pessoa.getNome());
//System.out.println(pessoa.getIdade());
//System.out.println(pessoa.getSexo());
//System.out.println(pessoa.getTelefone());
//System.out.println(pessoa.getEndereco());
}
});
limpa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
endereco.setText("");
telefone.setText("");
nome.setText("");
idade.setText("");
bgroup.clearSelection();
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[/code]
creio que estou fazendo algo errado com relação a Containers e Frames, mas não consigo enchergar o que.
obrigado