Como fasso para mostrar o valor que tenho em um determinado botao para dentro de um JTextField.
Ex: Uma calculadora quando eu clicar no 1 mostrar o numero 1 la no JTextField…
Como fasso para mostrar o valor que tenho em um determinado botao para dentro de um JTextField.
Ex: Uma calculadora quando eu clicar no 1 mostrar o numero 1 la no JTextField…
cria um evento no botão para ir adicionando conteudo no textfiled
mais ou menos assim, cria um evento, pega o valor do campo e concatena com o valor 1…flw
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// aqui vai a ação
jtextfiled.setText(jtextfield.getText()+"1");
}
});
tive que fazer isso quando fui implementar um teclado virtual para o meu programa
a solução que achei foi essa
crie uma class que implemente ActionListener
private class Inner implements ActionListener {
public void actionPerformed(ActionEvent event){
JButton fezAcao = (JButton)(event.getSource()); // retorna o objeto no qual o evento ocorreu inicialmente.
campo.setTest(fezAcao.getText()); // seta o valor do seu JTextField com o texto do botão que acionou o evento
}
}
adicione a seus botoes um ActionListener que resebe como parametro um objeto Inner
seuBotao.addActionListener(new Inner());
Esse tópico também vai ser útil para você:
http://www.guj.com.br/posts/list/140986.java
Eu ja tinha feito isso, mas o nome do textfield não reconhecia…quando eu coloco dentro do Action…
Olha o meu exemplo
Está como o InSeOfKn, porém todo o código 
import javax.swing.JButton;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Janela2.java
*
* Created on 11/10/2010, 11:16:24
*/
/**
*
* @author thiago.correa
*/
public class Janela2 extends javax.swing.JFrame {
/** Creates new form Janela2 */
public Janela2() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jTextField1.setColumns(10);
getContentPane().add(jTextField1);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
mostraTexto(evt.getSource());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
mostraTexto(evt.getSource());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
mostraTexto(evt.getSource());
}
private void mostraTexto(Object o) {
if (o instanceof JButton) {
JButton button = (JButton) o;
String texto = jTextField1.getText();
texto += button.getText();
jTextField1.setText(texto);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Janela2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
public void actionPerformed(ActionEvent numeros) {
JButton botaonum = (JButton) (numeros.getSource());
[b] tela[/b].setText( botaonum.getText() );
}
“tela” é o nome do JTextField, que está dentro de um JFrame ae eu criei essa Action para todos os botões da calculadoras. Ae esse “tela” fica dando erro…
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class calculadora extends JFrame implements ActionListener{
public calculadora(){
super("Calculadorinha");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(240,320);
this.setLayout(null);
JLabel nome = new JLabel();
nome.setSize(150,20);
nome.setLocation(60, 10);
nome.setText("Calculadorinha (^.^)");
this.add(nome);
JTextField tela = new JTextField();
tela.setSize(195, 20);
tela.setLocation(20, 40);
this.add(tela);
JButton botao7 = new JButton("7");
botao7.setSize(45,30);
botao7.setLocation(20, 80);
botao7.addActionListener(this);
this.add(botao7);
JButton botao8 = new JButton("8");
botao8.setSize(45,30);
botao8.setLocation(70, 80);
botao8.addActionListener(this);
this.add(botao8);
JButton botao9 = new JButton("9");
botao9.setSize(45,30);
botao9.setLocation(120, 80);
botao9.addActionListener(this);
this.add(botao9);
JButton botaosoma = new JButton("+");
botaosoma.setSize(45,30);
botaosoma.setLocation(170, 80);
this.add(botaosoma);
JButton botao4 = new JButton("4");
botao4.setSize(45,30);
botao4.setLocation(20, 120);
botao4.addActionListener(this);
this.add(botao4);
JButton botao5 = new JButton("5");
botao5.setSize(45,30);
botao5.setLocation(70, 120);
botao5.addActionListener(this);
this.add(botao5);
JButton botao6 = new JButton("6");
botao6.setSize(45,30);
botao6.setLocation(120, 120);
botao6.addActionListener(this);
this.add(botao6);
JButton botaomenos = new JButton("-");
botaomenos.setSize(45,30);
botaomenos.setLocation(170, 120);
this.add(botaomenos);
JButton botao1 = new JButton("1");
botao1.setSize(45,30);
botao1.setLocation(20, 160);
botao1.addActionListener(this);
this.add(botao1);
JButton botao2 = new JButton("2");
botao2.setSize(45,30);
botao2.setLocation(70, 160);
botao2.addActionListener(this);
this.add(botao2);
JButton botao3 = new JButton("3");
botao3.setSize(45,30);
botao3.setLocation(120, 160);
botao3.addActionListener(this);
this.add(botao3);
JButton botaomult = new JButton("*");
botaomult.setSize(45,30);
botaomult.setLocation(170, 160);
this.add(botaomult);
JButton botao0 = new JButton("0");
botao0.setSize(45,30);
botao0.setLocation(20, 200);
botao0.addActionListener(this);
this.add(botao0);
JButton botaoc = new JButton("C");
botaoc.setSize(45,30);
botaoc.setLocation(70, 200);
this.add(botaoc);
JButton botaoigual = new JButton("=");
botaoigual.setSize(45,30);
botaoigual.setLocation(120, 200);
this.add(botaoigual);
JButton botaobarra = new JButton("/");
botaobarra.setSize(45,30);
botaobarra.setLocation(170, 200);
this.add(botaobarra);
setVisible(true);
}
public static void main(String[] args){
calculadora c = new calculadora();
}
@Override
public void actionPerformed(ActionEvent numeros) {
JButton botaonum = (JButton) (numeros.getSource());
JTextField t = (JTextField) (botaonum.setText(tela));
t.setText( botaonum.getText() );
}
}
Galera ta dando erro na linha 125, quando eu indico o JTextField que eu fiz..que no caso é o "tela".
n sei mais como fasso..quero simplismente inserir dentro do JTextField o valor que tiver no botao...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class calculadora extends JFrame implements ActionListener{
public JTextField tela; // se declara na class para se poder acessar de qualquer método
public calculadora(){
super("Calculadorinha");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(240,320);
this.setLayout(null);
JLabel nome = new JLabel();
nome.setSize(150,20);
nome.setLocation(60, 10);
nome.setText("Calculadorinha (^.^)");
this.add(nome);
tela = new JTextField();
tela.setSize(195, 20);
tela.setLocation(20, 40);
this.add(tela);
JButton botao7 = new JButton("7");
botao7.setSize(45,30);
botao7.setLocation(20, 80);
botao7.addActionListener(this);
this.add(botao7);
JButton botao8 = new JButton("8");
botao8.setSize(45,30);
botao8.setLocation(70, 80);
botao8.addActionListener(this);
this.add(botao8);
JButton botao9 = new JButton("9");
botao9.setSize(45,30);
botao9.setLocation(120, 80);
botao9.addActionListener(this);
this.add(botao9);
JButton botaosoma = new JButton("+");
botaosoma.setSize(45,30);
botaosoma.setLocation(170, 80);
this.add(botaosoma);
JButton botao4 = new JButton("4");
botao4.setSize(45,30);
botao4.setLocation(20, 120);
botao4.addActionListener(this);
this.add(botao4);
JButton botao5 = new JButton("5");
botao5.setSize(45,30);
botao5.setLocation(70, 120);
botao5.addActionListener(this);
this.add(botao5);
JButton botao6 = new JButton("6");
botao6.setSize(45,30);
botao6.setLocation(120, 120);
botao6.addActionListener(this);
this.add(botao6);
JButton botaomenos = new JButton("-");
botaomenos.setSize(45,30);
botaomenos.setLocation(170, 120);
this.add(botaomenos);
JButton botao1 = new JButton("1");
botao1.setSize(45,30);
botao1.setLocation(20, 160);
botao1.addActionListener(this);
this.add(botao1);
JButton botao2 = new JButton("2");
botao2.setSize(45,30);
botao2.setLocation(70, 160);
botao2.addActionListener(this);
this.add(botao2);
JButton botao3 = new JButton("3");
botao3.setSize(45,30);
botao3.setLocation(120, 160);
botao3.addActionListener(this);
this.add(botao3);
JButton botaomult = new JButton("*");
botaomult.setSize(45,30);
botaomult.setLocation(170, 160);
this.add(botaomult);
JButton botao0 = new JButton("0");
botao0.setSize(45,30);
botao0.setLocation(20, 200);
botao0.addActionListener(this);
this.add(botao0);
JButton botaoc = new JButton("C");
botaoc.setSize(45,30);
botaoc.setLocation(70, 200);
this.add(botaoc);
JButton botaoigual = new JButton("=");
botaoigual.setSize(45,30);
botaoigual.setLocation(120, 200);
this.add(botaoigual);
JButton botaobarra = new JButton("/");
botaobarra.setSize(45,30);
botaobarra.setLocation(170, 200);
this.add(botaobarra);
setVisible(true);
}
public static void main(String[] args){
calculadora c = new calculadora();
}
@Override
public void actionPerformed(ActionEvent numeros) {
JButton botaonum = (JButton) (numeros.getSource()); //retorna o JButton que disparou o evento
tela.setText(tela.getText()+botaonum.getText()); //pega o texto da da tela e acrescenta o texto do botaonum
}
}
consertei seu código
você tinha declarado tela como local no construtor por isso não conseguia acessar do método actionPerformed
[color=darkred]
edit: tinha me esquecido de comentar o código :D [/color]
vlw parçero…consegui resolver
estamos aqui para isso compartilhar conhecimento!
se estais satisfeito com a resposta coloque um [resolvido] no titulo
até
olha la que bacana, uma calculadora prontinha, da para estudar os métodos… e resolver isto ai. mas acho que ja deu aí.