[Resolvido] JMenuItem não quer abrir o JFrame, qual será o problema?

Painel principal:

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class PainelFrame extends javax.swing.JFrame {
	
	public PainelFrame(){
		initComponents();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);
		setTitle("Fox Home Vídeo");
		
	}

	private void initComponents() {
	JMenuBar barra = new javax.swing.JMenuBar();
	JMenu manutencaoMenu = new javax.swing.JMenu("Manutenção");
	barra.add(manutencaoMenu);
	JMenuItem cadastroclienteMenu = new javax.swing.JMenuItem("Cadastro de cliente");
	manutencaoMenu.add(cadastroclienteMenu);
	
	
	setJMenuBar(barra);
	JDesktopPane lc = new javax.swing.JDesktopPane();
	add(lc);
		
	}
	
	@SuppressWarnings("unused")
	private void cadastroclienteMenuActionPerformed(java.awt.event.ActionEvent evt){
		new CadastroClienteFrame().setVisible(true);
	
	}
	
	public static void main(String [] args){
		java.awt.EventQueue.invokeLater(new Runnable(){
			public void run (){
				new PainelFrame().setVisible(true);
				
			}
		});
	}
}

JFrame secundário:



@SuppressWarnings("serial")
public class CadastroClienteFrame extends javax.swing.JFrame {
	
	public CadastroClienteFrame(){
		initComponents();
		setLocationRelativeTo(null);
		setResizable(false);
		setSize(500, 500);
		setVisible(true);
		setTitle("Cadastro de cliente");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}

	private void initComponents() {
			
	}
	
	public static void CadastroFrame(){
		java.awt.EventQueue.invokeLater(new Runnable(){
			public void run(){
				new CadastroClienteFrame().setVisible(true);
			}
			
		});
		
	}
}

bem to tentando abrir o JFrame secundário, mais não vai, jah add o ActionEvent e tudo, mais nada acontece, qual será o possivel problema???

faltou você adicionar um ActionLister no seu objeto JMenuItem cadastroclienteMenu.

segue um exemplo.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class PainelFrame extends javax.swing.JFrame {
	
	private class MyEventListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent evt) {
			
			if(evt.getSource() == cadastroclienteMenu)
				new CadastroClienteFrame();
		}
		
	}
	
	private JDesktopPane lc = new JDesktopPane();
	private JMenuBar barra = new javax.swing.JMenuBar();
	private JMenu manutencaoMenu = new javax.swing.JMenu("Manutenção");
	private JMenuItem cadastroclienteMenu = new JMenuItem("Cadastro de cliente");
	private MyEventListener evtListener = new MyEventListener();
	

	public PainelFrame() {
		initComponents();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 400);
		setTitle("Fox Home Vídeo");

	}

	private void initComponents() {
		getCadastroclienteMenu().addActionListener(evtListener);
		getBarra().add(manutencaoMenu);
		getManutencaoMenu().add(cadastroclienteMenu);
		
		this.setJMenuBar(barra);
		this.add(lc);
	}

	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new PainelFrame().setVisible(true);
			}
		});
	}

	public JDesktopPane getLc() {
		return lc;
	}

	public void setLc(JDesktopPane lc) {
		this.lc = lc;
	}

	public JMenuBar getBarra() {
		return barra;
	}

	public void setBarra(JMenuBar barra) {
		this.barra = barra;
	}

	public JMenu getManutencaoMenu() {
		return manutencaoMenu;
	}

	public void setManutencaoMenu(JMenu manutencaoMenu) {
		this.manutencaoMenu = manutencaoMenu;
	}

	public JMenuItem getCadastroclienteMenu() {
		return cadastroclienteMenu;
	}

	public void setCadastroclienteMenu(JMenuItem cadastroclienteMenu) {
		this.cadastroclienteMenu = cadastroclienteMenu;
	}
	
}

[quote=maiconhc]faltou você adicionar um ActionLister no seu objeto JMenuItem cadastroclienteMenu.

segue um exemplo.

[code]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings(“serial”)
public class PainelFrame extends javax.swing.JFrame {

private class MyEventListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent evt) {
		
		if(evt.getSource() == cadastroclienteMenu)
			new CadastroClienteFrame();
	}
	
}

private JDesktopPane lc = new JDesktopPane();
private JMenuBar barra = new javax.swing.JMenuBar();
private JMenu manutencaoMenu = new javax.swing.JMenu("Manutenção");
private JMenuItem cadastroclienteMenu = new JMenuItem("Cadastro de cliente");
private MyEventListener evtListener = new MyEventListener();


public PainelFrame() {
	initComponents();
	setLocationRelativeTo(null);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setSize(400, 400);
	setTitle("Fox Home Vídeo");

}

private void initComponents() {
	getCadastroclienteMenu().addActionListener(evtListener);
	getBarra().add(manutencaoMenu);
	getManutencaoMenu().add(cadastroclienteMenu);
	
	this.setJMenuBar(barra);
	this.add(lc);
}

public static void main(String[] args) {
	java.awt.EventQueue.invokeLater(new Runnable() {
		public void run() {
			new PainelFrame().setVisible(true);
		}
	});
}

public JDesktopPane getLc() {
	return lc;
}

public void setLc(JDesktopPane lc) {
	this.lc = lc;
}

public JMenuBar getBarra() {
	return barra;
}

public void setBarra(JMenuBar barra) {
	this.barra = barra;
}

public JMenu getManutencaoMenu() {
	return manutencaoMenu;
}

public void setManutencaoMenu(JMenu manutencaoMenu) {
	this.manutencaoMenu = manutencaoMenu;
}

public JMenuItem getCadastroclienteMenu() {
	return cadastroclienteMenu;
}

public void setCadastroclienteMenu(JMenuItem cadastroclienteMenu) {
	this.cadastroclienteMenu = cadastroclienteMenu;
}

}

[/code][/quote]

o que seria realmente esse MyEventListener???

É a classe que vai “escutar” que seu evento foi clicado. Você também poderia declara-lo de forma anônima:

getCadastroclienteMenu().addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { new CadastroClienteFrame().setVisible(true); } } );

É a classe que vai “escutar” que seu evento foi clicado. Você também poderia declara-lo de forma anônima:

getCadastroclienteMenu().addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { new CadastroClienteFrame().setVisible(true); } } ); [/quote]

aqui tah dando erro, implemento no método do initComponents ou errei tudo???

… que erro?

… que erro?[/quote]

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


@SuppressWarnings("serial")
public class PainelFrame extends javax.swing.JFrame {
	
	private JMenuBar barra = new javax.swing.JMenuBar();
	private JMenu manutencaoMenu = new javax.swing.JMenu("Manutenção");
	private JMenuItem CadastroclienteMenu = new javax.swing.JMenuItem("Cadastro de cliente");
	private JDesktopPane lc = new javax.swing.JDesktopPane();
		
	public PainelFrame() {
		initComponents();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);
		setTitle("Fox Home Vídeo");
	}
		
	private void initComponents() {
	barra.add(manutencaoMenu);
	manutencaoMenu.add(CadastroclienteMenu);
	setJMenuBar(barra);
	add(lc);
	}
	
	@SuppressWarnings("unused")
	private void CadastroclienteMenu (){
		getCadastroclienteMenu().addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent evt){
						new CadastroClienteFrame().setVisible(true);
					}
				});
		
	}
	
	public static void main(String [] args){
		java.awt.EventQueue.invokeLater(new Runnable(){
			public void run (){
				new PainelFrame().setVisible(true);
			}
		});
	}
}

está acusando de erro no “getCadastroclienteMenu”, o que falta???

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


@SuppressWarnings("serial")
public class PainelFrame extends javax.swing.JFrame {
   private JMenuBar barra = new javax.swing.JMenuBar();
   private JMenu manutencaoMenu = new javax.swing.JMenu("Manutenção");
   private JMenuItem cadastroClienteMenu = new javax.swing.JMenuItem("Cadastro de cliente");
   private JDesktopPane lc = new javax.swing.JDesktopPane();
		
   public PainelFrame() {
      initComponents();
      setLocationRelativeTo(null);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setExtendedState(MAXIMIZED_BOTH);
      setTitle("Fox Home Vídeo");
   }
		
   private void initComponents() {
      barra.add(manutencaoMenu);
      manutencaoMenu.add(cadastroClienteMenu);
      cadastroClienteMenu.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent evt){
            new CadastroClienteFrame().setVisible(true);
         }
      });
      setJMenuBar(barra);
      add(lc);
   }
	
   public static void main(String [] args){
      java.awt.EventQueue.invokeLater(new Runnable(){
         public void run (){
            new PainelFrame().setVisible(true);
         }
      });
   }
}

[quote=ViniGodoy][code]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings(“serial”)
public class PainelFrame extends javax.swing.JFrame {
private JMenuBar barra = new javax.swing.JMenuBar();
private JMenu manutencaoMenu = new javax.swing.JMenu(“Manutenção”);
private JMenuItem cadastroClienteMenu = new javax.swing.JMenuItem(“Cadastro de cliente”);
private JDesktopPane lc = new javax.swing.JDesktopPane();

public PainelFrame() {
initComponents();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setTitle(“Fox Home Vídeo”);
}

private void initComponents() {
barra.add(manutencaoMenu);
manutencaoMenu.add(cadastroClienteMenu);
cadastroClienteMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
new CadastroClienteFrame().setVisible(true);
}
});
setJMenuBar(barra);
add(lc);
}

public static void main(String [] args){
java.awt.EventQueue.invokeLater(new Runnable(){
public void run (){
new PainelFrame().setVisible(true);
}
});
}
}
[/code][/quote]

vlw, agora funcionou!!!