Вы находитесь на странице: 1из 2

import java.awt.

BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrador
*/
public final class Microndas extends JFrame {

boolean pulsado = true;

public JButton boton(final JButton b, final TextField t) {


b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.setText(t.getText().toString() + b.getText().toString());
}
});
return b;
}

public JButton botonStart(final JButton b, final TextField t) {


b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i;
try {
for (i = Integer.parseInt(t.getText().toString()); i >= 0; i--)
{
try {
t.setText(String.valueOf(i));
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("No se puede interrumpir");
}
}
} catch (NumberFormatException exc) {
i = 0;
}
}
});
return b;
}

public JButton botonStop(final JButton b, final TextField t) {


b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t.setText(t.getText().toString());
}
});
return b;
}

public Microndas() {
int i;
setLayout(new BorderLayout());
JPanel tecladoNum = new JPanel();
tecladoNum.setLayout(new GridLayout(4, 3));
JPanel panelIzquierdo = new JPanel();
panelIzquierdo.setLayout(new BorderLayout());
TextField t = new TextField();
for (i = 1; i < 10; i++) {
tecladoNum.add(boton(new JButton("" + i), t));
}
tecladoNum.add(boton(new JButton("" + 0), t));
tecladoNum.add(botonStart(new JButton("Start"), t));
//No se como hacer funcionar el stop ya que el boton start no para se queda
pulsado
tecladoNum.add(botonStop(new JButton("Stop"), t));

panelIzquierdo.add(t, BorderLayout.NORTH);
panelIzquierdo.add(tecladoNum, BorderLayout.CENTER);

add(new JButton("Comida Aqui"), BorderLayout.CENTER);


add(panelIzquierdo, BorderLayout.EAST);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Microndas();
}
}

Вам также может понравиться