Fórum Como eu faco 2 botões com 2 ActionListener/actionPerformed? #567609
09/04/2009
0
package backdrive;
import javax.swing.*;
import java.awt.event.*;
public class Backdrive implements ActionListener {
// Componentes
private JLabel label;
private JTextField textField;
private JButton button;
private JButton button2;
// Painel (aonde fica os componentes)
private JPanel panel;
// Frame/Janela (aonde fica os paineis)
private JFrame frame;
private void Show() {
// Inicia componentes.
label = new JLabel("Seu nome:");
textField = new JTextField(20);
button = new JButton("Exibir");
button2 = new JButton("Botao 2");
// Eventos.
button.addActionListener(this);
button2.addActionListener(this);
// Adiciona componentes ao painel.
panel = new JPanel();
panel.add(label);
panel.add(textField);
panel.add(button);
panel.add(button2);
// Adiciona painel a janela.
frame = new JFrame("Janela");
frame.add(panel);
// Prepara para exibicao.
frame.pack();
// Colocamos como visivel.
frame.setVisible(true);
// Caso o usuario clique X, sai.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Botão1 ?!?!
public void actionPerformed(ActionEvent e) {
String texto = textField.getText();
JOptionPane.showMessageDialog(frame, texto);
}
// Botão2 ?!?1
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Teste");
}
public static void main(String[] args) {
new Backdrive().Show();
}
}
Erros do Ecplise : Duplicate method actionPerformed(ActionEvent) in type Backdrive Backdrive/backdrive Backdrive.java line 50 Duplicate method actionPerformed(ActionEvent) in type Backdrive Backdrive/backdrive Backdrive.java line 56 The type Backdrive must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) Backdrive/backdrive Backdrive.java
Eduardounder
Curtir tópico
+ 0Posts
09/04/2009
Ricardo Silva
package br.com.learning;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HelloButton extends JFrame implements ActionListener {
private JButton botao1;
private JButton botao2;
public HelloButton(){
initComponents();
configEvents();
configLayout();
}
private void configLayout(){
this.setLayout(new FlowLayout());
this.add(botao1);
this.add(botao2);
this.setSize(100,200);
this.setVisible(true);
}
private void initComponents(){
botao1 = new JButton("Botão 001");
botao2 = new JButton("Botão 002");
}
private void configEvents(){
botao1.addActionListener(this);
botao2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Realiza o tratamento dos eventos dos botões.
*/
public void actionPerformed(ActionEvent e) {
//trata os eventos para o botao "botao1"
if( e.getSource() == botao1){
JOptionPane.showMessageDialog(null, "Clicou sobre o botão 01");
}
// trata os eventos para o botao "botao2"
if (e.getSource() == botao2){
JOptionPane.showMessageDialog(null, "Clicou sobre o botão 02");
}
}
public static void main(String[] args) {
HelloButton hf = new HelloButton();
}
}if(...){}
package br.com.learning;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HelloButton2 extends JFrame {
private JButton botao1;
private JButton botao2;
public HelloButton2(){
initComponents();
configEvents();
configLayout();
}
private void configLayout(){
this.setLayout(new FlowLayout());
this.add(botao1);
this.add(botao2);
this.setSize(100,200);
this.setVisible(true);
}
private void initComponents(){
botao1 = new JButton("Botão 001");
botao2 = new JButton("Botão 002");
}
private void configEvents(){
botao1.addActionListener(new RegraBotao1());
botao2.addActionListener(new RegraBotao2());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
HelloButton2 hf = new HelloButton2();
}
class RegraBotao1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Clicou sobre o botão 01");
}
}
class RegraBotao2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Clicou sobre o botão 02");
}
}
}
Gostei + 0
09/04/2009
Eduardounder
package main;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.JButton;
public class Program {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="59,28"
private JPanel jContentPane = null;
private JButton jButton = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(373, 184));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJButton(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Gostei + 0
09/04/2009
Ricardo Silva
Gostei + 0
22/01/2014
António Freitas
Gostei + 0
28/01/2014
André Pereira
Gostei + 0