示例#1
0
  public void actionPerformed(ActionEvent even) {

    Object respuesta = even.getSource(); // se esta diciendo que respuesta sea un objeto.

    if (respuesta == b1) {

      txt = new JTextField(4 + 4);
      setTitle("sumar");
    } else if (respuesta == b2) {

      // txt=new JTextField (4-1);

      txt.setActionCommand("4");
      setTitle("Restar");
    }

    // cambiar el boton b2

    b2.setActionCommand("LOL");
    b2.setLabel(b2.getActionCommand());

    repaint();
  }
  private void initComponents() {
    handler = new CharacterEventHandler();
    displayCanvas = new AnimatedCharacterDisplayCanvas();
    feedbackCanvas = new CharacterDisplayCanvas(this);
    quitButton = new JButton();
    startButton = new JButton();
    stopButton = new JButton();
    score = new ScoreLabel(null, this);

    Container pane = getContentPane();
    pane.add(displayCanvas, BorderLayout.NORTH);
    pane.add(feedbackCanvas, BorderLayout.CENTER);

    JPanel p1 = new JPanel();
    p1.setLayout(new BorderLayout());
    score.setText("     ");
    score.setFont(new Font("MONOSPACED", Font.BOLD, 30));
    p1.add(score, BorderLayout.CENTER);

    JPanel p2 = new JPanel();
    startButton.setLabel("Start");
    stopButton.setLabel("Stop");
    stopButton.setEnabled(false);
    quitButton.setLabel("Quit");
    p2.add(startButton);
    p2.add(stopButton);
    p2.add(quitButton);
    p1.add(p2, BorderLayout.EAST);
    pane.add(p1, BorderLayout.SOUTH);

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            quit();
          }
        });

    feedbackCanvas.addKeyListener(
        new KeyAdapter() {
          public void keyPressed(KeyEvent ke) {
            char c = ke.getKeyChar();
            if (c != KeyEvent.CHAR_UNDEFINED) newCharacter((int) c);
          }
        });
    startButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            producer = new RandomCharacterGenerator();
            displayCanvas.setCharacterSource(producer);
            score.resetGenerator(producer);
            producer.start();
            displayCanvas.setDone(false);
            Thread t = new Thread(displayCanvas);
            t.start();
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            feedbackCanvas.setEnabled(true);
            feedbackCanvas.requestFocus();
            score.resetScore();
          }
        });
    stopButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            producer.setDone();
            displayCanvas.setDone(true);
            feedbackCanvas.setEnabled(false);
          }
        });
    quitButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            quit();
          }
        });
    pack();
  }