True e False

2 respostas
U

Prezados,

No codigo abaixo como faço para deixar o criaMostraGUI() como true quando for gerado um evento em um JMenuItem.

private void criaMostraGui() {
		JFrame frame = new JFrame("Hello");
        JLabel label = new JLabel("Hello, Swing World");
        frame.getContentPane().add(label);    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(false);
	}    
	
	private void createAndShowGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setJMenuBar(createMenuBar());
		JScrollPane scrollPane = new JScrollPane(textArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		getContentPane().add(scrollPane);
		setLocation(150, 200);
		setSize(450, 260);
		setVisible(true);
		
	}

	public static void main(String[] args) {
		ExemploJMenu exemploJMenu = new ExemploJMenu("Quina Muita Sorte 2007");
		exemploJMenu.createAndShowGUI();
		exemploJMenu.criaMostraGui();
	}

	public void actionPerformed(ActionEvent e) {
		// checar se o evento é de JMenuItem
		if (e.getSource() instanceof JMenuItem) {
			JMenuItem menuItem = (JMenuItem) e.getSource();
			String texto = menuItem.getText();
			textArea.append("=> Evento gerado para JMenuItem " + texto);
			textArea.append("\n");
			if ("Sair".equals(texto)) {
				int opcao = JOptionPane.showConfirmDialog(this,
						"Deseja realmente sair?", "Sair",
						JOptionPane.YES_NO_OPTION);
				if (opcao == JOptionPane.OK_OPTION) {
					System.exit(0);
				}
			}
		}
	}

2 Respostas

_Renatu

hm, eu nao entendi muito bem sua pergunta…

O que voce quer deixar como true?

voce quer exibir aquele GUI?

voce pode simplesmente fazer frame.setVisible(true);

o problema que talvez voce tenha ai é de escopo…
esta variavel “frame” precisa estar disponivel num escopo fora do metodo criaMostraGui, para que possa ser acessada de outro metodo, como o actionPerformed, por exemplo…

P
Cara eu tb nao entendi mas pelo q eu entendi eu acho q vc deve criar uma variavel tipo:
public void actionPerformed(ActionEvent e) {   
        // checar se o evento é de JMenuItem   
        if (e.getSource() instanceof JMenuItem) {   
            JMenuItem menuItem = (JMenuItem) e.getSource();   
            String texto = menuItem.getText();   
            textArea.append("=> Evento gerado para JMenuItem " + texto);   
            textArea.append("\n");   
            if ("Sair".equals(texto)) {   
                int opcao = JOptionPane.showConfirmDialog(this,   
                        "Deseja realmente sair?", "Sair",   
                        JOptionPane.YES_NO_OPTION);   
                if (opcao == JOptionPane.OK_OPTION) {   
                    System.exit(0);   
                }   
            }   
        }   
    }  
private void criaMostraGui() {
		JFrame frame = new JFrame("Hello");
        JLabel label = new JLabel("Hello, Swing World");
        frame.getContentPane().add(label);    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(false);
	}    
	
	private void createAndShowGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setJMenuBar(createMenuBar());
		JScrollPane scrollPane = new JScrollPane(textArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		getContentPane().add(scrollPane);
		setLocation(150, 200);
		setSize(450, 260);
		setVisible(true);
		
	}

	public static void main(String[] args) {
		ExemploJMenu exemploJMenu = new ExemploJMenu("Quina Muita Sorte 2007");
		exemploJMenu.createAndShowGUI();
		exemploJMenu.criaMostraGui();
	}

	public void actionPerformed(ActionEvent e) {
		// checar se o evento é de JMenuItem
                            // cria uma variavel de classe aiai
                            aiai=true;
		if (e.getSource() instanceof JMenuItem) {
			JMenuItem menuItem = (JMenuItem) e.getSource();
			String texto = menuItem.getText();
			textArea.append("=> Evento gerado para JMenuItem " + texto);
			textArea.append("\n");
			if ("Sair".equals(texto)) {
				int opcao = JOptionPane.showConfirmDialog(this,
						"Deseja realmente sair?", "Sair",
						JOptionPane.YES_NO_OPTION);
				if (opcao == JOptionPane.OK_OPTION) {
					System.exit(0);
				}
			}
		}else aiai=false;
	}  
 
 public boolean aiaiai()
{
 return aiai;
}

Eu devo ta viajando .........tb eu to bebado hehehehehehehe

Criado 10 de novembro de 2007
Ultima resposta 11 de nov. de 2007
Respostas 2
Participantes 3