Exibir item do Jcombo atraves do Jbutton

5 respostas
F

oq eu quero é simplesmente exibir a opçao selecionado no JcomboBox quando eu clicar no botão sortear
ta dando pau na linha
public class SorteGrande extedns JFrame implementes ActionListener
e eu num to conseguindo arrumar se alguem tiver uma dica eu agradeço
fui

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class   SorteGrande extends JFrame implements ActionListener
{

	private  JButton   sorteio,sair;
	private  JLabel    label;
	private  JPanel    painelnorte,painelcenter,painelsul;	
	private  JComboBox combo;
	public SorteGrande()
	{
		super ("Sorte Grande v.01")	;
		setLayout(new BorderLayout());
				
		
		//tratamento JComboBox
	
		String[] vetor={"Dupla Sena","Loto Fácil","Loto Mania","Mega Sena","Quina"};
		
	    JComboBox escolha= new JComboBox(vetor);
		escolha.addActionListener(this);	
	
		//Tratamento botão sair e sortear com evento
		sair    = new JButton("Sair");
		sair exit =new sair();
		sair.addActionListener(exit);
		//
		sorteio    = new JButton("Sortear");
		sortear sort =new sortear();
		sorteio.addActionListener(sort);
		
			
		//tramento  Painel
		painelcenter =new JPanel();
		painelnorte  =new JPanel();
		painelsul    =new JPanel();
		
		//Tratamento do label
		label   =new JLabel("Escolha a opção para gerar o palpite");
		
		//Adicionando paineis
		add(painelnorte,  BorderLayout.NORTH);
		add(painelcenter, BorderLayout.CENTER);
		add(painelsul,    BorderLayout.SOUTH);
		
		//painel nort
		painelnorte.  add(label);
		painelcenter.  add(escolha);
	
		//painel sul
		painelsul.    add(sorteio);
		painelsul.    add(sair);
		pack();
		
		
	   setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
/*
    //inicio tratamento evento jcombobox
    public void actionPerformed(ActionEvent e){
    
    JComboBox cb=(JComboBox)e.getSource();
    String escolha=(String)cb.getSelectedItem();	
   	JOptionPane.showMessageDialog(null,"a opção escolhida foi "+escolha);
    }
     
  */  
   
	public class sair implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{	
		String opcoes[] = {"Sim","Não"};
		int     opc     = JOptionPane.showOptionDialog(null,"Confirma o encerramento?","Confirmação",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE,null,opcoes,opcoes[1]);
		//
		if ( opc == 0 )
			System.exit(0);
	}
	
}
	
	public class sortear implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			if (e.getSource()==sorteio)
			{
				JComboBox cb=(JComboBox)e.getSource();
				String escolha=(String)cb.getSelectedItem();	
				JOptionPane.showMessageDialog(null,"a opção escolhida foi "+escolha);
		    }
		}
	}


	public static void main(String args[])
	{
	
	SorteGrande ref = new SorteGrande();
	
	}	

	
}

5 Respostas

Tomassoni

Amigo coloca o erro ai para facilitar. :wink:

F

olha o erro ai

The type SorteGrande must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

e aponta para a linha

public class SorteGrande extends JFrame implements ActionListener

fui

M

Opa, beleza?Você não precisa implementar actionlistener à classe SorteGrande!!
Segue um exemplo de como fazer:

botaoSair.addActionListener(new sair());
F

não testei ainda se funciona mas como funciona este negcio de
hora usa implement action listener outra hora ja não precisa não entendo nada disto no meu modo de ver tudo que vc usa um action listerner deveria colocar implements mas não é bem assim que funciona alguem me da uma luz??

F

é sua sujestão não funcionou
e o botão em questão é o sortear!!! o sair funciona redondo
so pra frizar ai

oq eu quero é que seja exibido a oção escolhida quando eu clicar em sortear.

Criado 14 de setembro de 2007
Ultima resposta 17 de set. de 2007
Respostas 5
Participantes 3