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

8 respostas
alexandref93

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???

8 Respostas

M

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;
	}
	
}
alexandref93
maiconhc:
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;
	}
	
}

o que seria realmente esse MyEventListener???

ViniGodoy

É 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); } } );

alexandref93

É 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); } } );

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

ViniGodoy

… que erro?

alexandref93

… que erro?

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???

ViniGodoy
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);
         }
      });
   }
}
alexandref93
ViniGodoy:
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);
         }
      });
   }
}

vlw, agora funcionou!!!

Criado 24 de junho de 2010
Ultima resposta 25 de jun. de 2010
Respostas 8
Participantes 3