AYUDA CON TextField Y JButton
UN SALUDO:
ESTOY HACIENDO UNA CALCULADORA YA TERMINE EL CODIGO PERO NO LOGRO HACER ESTO:
1. QUIERO QUE CUANDO OPRIMA UN BOTON("LOS NUMEROS DE LA CALCULADORA)A PARTE DE MOSTRARMELO ME LO ACUMULE OSEA ME FORME UN String TextField PORQUE LO QUE HACE ES CAMBIARME EL NUMERO Y COLOCAR EL QUE HE OPRIMIDO Y NO DEBE FUNCIONAR ASI.
SI ALGUIEN QUE ME PUEDA AYUDAR EN ESTE
PROBLEMITA SE LO AGRADESCO.
ESTOY HACIENDO UNA CALCULADORA YA TERMINE EL CODIGO PERO NO LOGRO HACER ESTO:
1. QUIERO QUE CUANDO OPRIMA UN BOTON("LOS NUMEROS DE LA CALCULADORA)A PARTE DE MOSTRARMELO ME LO ACUMULE OSEA ME FORME UN String TextField PORQUE LO QUE HACE ES CAMBIARME EL NUMERO Y COLOCAR EL QUE HE OPRIMIDO Y NO DEBE FUNCIONAR ASI.
SI ALGUIEN QUE ME PUEDA AYUDAR EN ESTE
PROBLEMITA SE LO AGRADESCO.
Hola, pues debes coger el texto q hay en el textfield con su metodo getText():
String cadena=nombredelTextField.getText()+"nuevo texto"
y asi se mostrará:
nombredelTextField.setText(cadena);
Saludos
String cadena=nombredelTextField.getText()+"nuevo texto"
y asi se mostrará:
nombredelTextField.setText(cadena);
Saludos
SI FER ESTO LO HACE PERO EL PROBLEMA ES QUE COMO YO LE COLOCO UN EVENTO A LOS BOTONES DE LA CALCULADORA EL ME BORRA EL NUMERO ANTERIOR
Y ES ESO LO QUE NO QUIERO QUE OCURRA.
ESTE ES EL CODIGO.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class cal
{
static JButton A;
static JButton B;
static JButton C;
static JButton D;
static JButton E;
static JButton F;
static JButton G;
static JButton H;
static JButton I;
static JButton J;
static JButton K;
static JButton L;
static JButton M;
static JButton N;
static JButton O;
static JButton P;
static JTextField R;
static JTextField V;
static JTextField Q;
static int n;
static int o;
static int res;
public static void main(String[]arg)
{
int y;
JFrame m=new JFrame();
m.setTitle("CALCULADORA");
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
donde XXX ha de ser una de estas opciones:
CROSSHAIR_CURSOR
The crosshair cursor type.
CUSTOM_CURSOR
The type associated with all custom cursors.
DEFAULT_CURSOR
The default cursor type (gets set if no cursor is defined).
E_RESIZE_CURSOR
The east-resize cursor type.
HAND_CURSOR
The hand cursor type.
MOVE_CURSOR
The move cursor type.
N_RESIZE_CURSOR
The north-resize cursor type.
NE_RESIZE_CURSOR
The north-east-resize cursor type.
NW_RESIZE_CURSOR
The north-west-resize cursor type.
S_RESIZE_CURSOR
The south-resize cursor type.
The south-east-resize cursor type.
SW_RESIZE_CURSOR
The south-west-resize cursor type.
TEXT_CURSOR
The text cursor type.
W_RESIZE_CURSOR
The west-resize cursor type.
WAIT_CURSOR
The wait cursor type. */
JPanel a= new JPanel();
a.setLayout(new BoxLayout (a,BoxLayout.Y_AXIS));
JPanel b= new JPanel();
JLabel e=new JLabel ("ANALISTA: RAUL SOTO");
JPanel c= new JPanel();
c.setLayout(new BoxLayout (c,BoxLayout.Y_AXIS));
Q=new JTextField(15);
R=new JTextField(15);
V=new JTextField(15);
Q.setEditable(false);
Q.setBackground(Color.white);
R.setBackground(Color.YELLOW);
V.setBackground(Color.YELLOW);
c.add(Q);
c.add(R);
c.add(V);
JPanel d= new JPanel();
d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
d.setLayout(new GridLayout (4,4));
JButton P=new JButton("7");
P.setMnemonic('7');
d.add(P);
P.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Q.setText("7");
n=7;
}
}
);
A=new JButton("8");
d.add(A);
A.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=8;
Q.setText("8");
}
}
);
JButton B=new JButton("9");
d.add(B);
B.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=9;
Q.setText("9");
}
}
);
JButton C=new JButton("X");
d.add(C);
C.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
// actividad de por
o=1;
}
}
);
JButton D=new JButton("4");
d.add(D);
D.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=4;
Q.setText("4");
}
}
);
JButton E=new JButton("5");
d.add(E);
E.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=5;
Q.setText("5");
}
}
);
JButton F=new JButton("6");
d.add(F);
F.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=6;
Q.setText("6");
}
}
);
JButton G=new JButton("/");
d.add(G);
G.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=2;
}
}
);
JButton H=new JButton("1");
d.add(H);
H.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=1;
Q.setText("1");
}
}
);
JButton I=new JButton("2");
d.add(I);
I.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=2;
Q.setText("2");
}
}
);
JButton J=new JButton("3");
d.add(J);
J.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=3;
Q.setText("3");
}
}
);
JButton K=new JButton("-");
d.add(K);
K.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=3;
}
}
);
JButton L=new JButton(".");
d.add(L);
L.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Q.setText(".");
}
}
);
JButton M=new JButton("0");
d.add(M);
M.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int n=0;
Q.setText("0");
}
}
);
JButton O=new JButton("+");
d.add(O);
O.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=4;
}
}
);
JButton N=new JButton("=");
d.add(N);
N.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int y=n;
String z=Q.getText();
int w=Integer.parseInt(z);
Q.setText(""+o);
R.setText(""+y);
V.setText(""+z);
}
}
);
a.add(e);
a.add(c);
a.add(d);
a.add(b);
m.getContentPane().add(a);
m.pack();
m.setVisible(true);
}
}
NOTA: NO TE FIJES EN LOS TEXTFIELD AMARILLOS SOLO LOS COLOQUE PARA VER EL VALOR DE LAS VARIABLES.
TODO ESTO DEBE FONCIONAR AL OPRIMIR EL "=".
GARCIAS¡
Y ES ESO LO QUE NO QUIERO QUE OCURRA.
ESTE ES EL CODIGO.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class cal
{
static JButton A;
static JButton B;
static JButton C;
static JButton D;
static JButton E;
static JButton F;
static JButton G;
static JButton H;
static JButton I;
static JButton J;
static JButton K;
static JButton L;
static JButton M;
static JButton N;
static JButton O;
static JButton P;
static JTextField R;
static JTextField V;
static JTextField Q;
static int n;
static int o;
static int res;
public static void main(String[]arg)
{
int y;
JFrame m=new JFrame();
m.setTitle("CALCULADORA");
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
donde XXX ha de ser una de estas opciones:
CROSSHAIR_CURSOR
The crosshair cursor type.
CUSTOM_CURSOR
The type associated with all custom cursors.
DEFAULT_CURSOR
The default cursor type (gets set if no cursor is defined).
E_RESIZE_CURSOR
The east-resize cursor type.
HAND_CURSOR
The hand cursor type.
MOVE_CURSOR
The move cursor type.
N_RESIZE_CURSOR
The north-resize cursor type.
NE_RESIZE_CURSOR
The north-east-resize cursor type.
NW_RESIZE_CURSOR
The north-west-resize cursor type.
S_RESIZE_CURSOR
The south-resize cursor type.
The south-east-resize cursor type.
SW_RESIZE_CURSOR
The south-west-resize cursor type.
TEXT_CURSOR
The text cursor type.
W_RESIZE_CURSOR
The west-resize cursor type.
WAIT_CURSOR
The wait cursor type. */
JPanel a= new JPanel();
a.setLayout(new BoxLayout (a,BoxLayout.Y_AXIS));
JPanel b= new JPanel();
JLabel e=new JLabel ("ANALISTA: RAUL SOTO");
JPanel c= new JPanel();
c.setLayout(new BoxLayout (c,BoxLayout.Y_AXIS));
Q=new JTextField(15);
R=new JTextField(15);
V=new JTextField(15);
Q.setEditable(false);
Q.setBackground(Color.white);
R.setBackground(Color.YELLOW);
V.setBackground(Color.YELLOW);
c.add(Q);
c.add(R);
c.add(V);
JPanel d= new JPanel();
d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
d.setLayout(new GridLayout (4,4));
JButton P=new JButton("7");
P.setMnemonic('7');
d.add(P);
P.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Q.setText("7");
n=7;
}
}
);
A=new JButton("8");
d.add(A);
A.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=8;
Q.setText("8");
}
}
);
JButton B=new JButton("9");
d.add(B);
B.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=9;
Q.setText("9");
}
}
);
JButton C=new JButton("X");
d.add(C);
C.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
// actividad de por
o=1;
}
}
);
JButton D=new JButton("4");
d.add(D);
D.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=4;
Q.setText("4");
}
}
);
JButton E=new JButton("5");
d.add(E);
E.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=5;
Q.setText("5");
}
}
);
JButton F=new JButton("6");
d.add(F);
F.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=6;
Q.setText("6");
}
}
);
JButton G=new JButton("/");
d.add(G);
G.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=2;
}
}
);
JButton H=new JButton("1");
d.add(H);
H.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=1;
Q.setText("1");
}
}
);
JButton I=new JButton("2");
d.add(I);
I.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=2;
Q.setText("2");
}
}
);
JButton J=new JButton("3");
d.add(J);
J.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
n=3;
Q.setText("3");
}
}
);
JButton K=new JButton("-");
d.add(K);
K.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=3;
}
}
);
JButton L=new JButton(".");
d.add(L);
L.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Q.setText(".");
}
}
);
JButton M=new JButton("0");
d.add(M);
M.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int n=0;
Q.setText("0");
}
}
);
JButton O=new JButton("+");
d.add(O);
O.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
o=4;
}
}
);
JButton N=new JButton("=");
d.add(N);
N.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int y=n;
String z=Q.getText();
int w=Integer.parseInt(z);
Q.setText(""+o);
R.setText(""+y);
V.setText(""+z);
}
}
);
a.add(e);
a.add(c);
a.add(d);
a.add(b);
m.getContentPane().add(a);
m.pack();
m.setVisible(true);
}
}
NOTA: NO TE FIJES EN LOS TEXTFIELD AMARILLOS SOLO LOS COLOQUE PARA VER EL VALOR DE LAS VARIABLES.
TODO ESTO DEBE FONCIONAR AL OPRIMIR EL "=".
GARCIAS¡
Pues eso, haz como te digo, supongo q el TextField Q es el que va saliendo los numeros a operar no?, pues mira en vez de poner en cada actionlistener Q.setText("3"); pon Q.setText(Q.getText()+"3");
de esta forma coges lo q habia ya y lo vuelve a mostrar con el nuevo numero.
es eso a lo q te refieres?
de esta forma coges lo q habia ya y lo vuelve a mostrar con el nuevo numero.
es eso a lo q te refieres?
