Tuesday, October 11, 2011

Action Listener Code

A simple program to create a “frame”, “button”, & Action Listener
Names: The Frame is called “simpleframe” and the Button is called “simplebutton”. You can change these names to anything that makes sense to you, make sure the names are all consistent.

import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;

public class ButtonPressDemo { public static void main(String[] args){
Button simplebutton; //declare b to be a button ActionListener a = new MyActionListener1(); //declare a to be a method that listens to actions Frame simpleframe = new Frame("Java Action Listener Example"); // declare f to be a frame and
name it simpleframe.setSize(400, 200); // Set the size for your window simpleframe.setBackground(Color.green); // Change the color of the background simplebutton = new Button("Click me"); // Create a button simpleframe.add(simplebutton, BorderLayout.NORTH); // adding a button to a frame and positioning
it at the top simplebutton.addActionListener(a); // Calling the variable to trigger the action
//simpleframe.pack(); // Makes everything visible in the most efficient layout; Ignores the set size simpleframe.setVisible(true); // Makes the frame visible
}
}
// Triggers an action as to what happens when the button is clicked

class MyActionListener1 implements ActionListener {
public void actionPerformed(ActionEvent ae) { System.out.println("You have triggered an action by clicking the button"); // Prints the wordswhen the button is pushed
}
}
Enable screen reader

Monday, October 10, 2011

Action Listener Code

import java.awt.*; 
import java.awt.event.*; 
 
public class ButtonPressDemo { 
  public static void main(String[] args){ 
  Button b; 
  ActionListener a = new MyActionListener(); 
  Frame f = new Frame("Java Action Example"); 
  f.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
  b.setActionCommand("Good Morning"); 
  b.addActionListener(a); 
  f.add(b = new Button("Good Day"), BorderLayout.CENTER); 
  b.addActionListener(a); 
  f.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
  b.setActionCommand("Exit"); 
  b.addActionListener(a); 
  f.pack(); 
  f.show(); 
  } 
} 
 
class MyActionListener implements ActionListener { 
  public void actionPerformed(ActionEvent ae) {
  String s = ae.getActionCommand(); 
  if (s.equals("Exit")) { 
  System.exit(0); 
  } 
  else if (s.equals("Bonjour")) { 
  System.out.println("Good Morning"); 
  } 
  else { 
  System.out.println(s + " clicked"); 
  } 
  } 
}

Sunday, October 9, 2011

Practice & Explore

A good way to learn how a program works and create your own, is to take some code and explore it. Change some lines and see how it affects the results.

Thursday, October 6, 2011

Code for attaching Action to Buttons

 
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonSettingBackground extends JPanel implements ActionListener {

  private JButton yellowButton = new JButton("Yellow");

  private JButton blueButton = new JButton("Blue");

  private JButton redButton = new JButton("Red");

  public ButtonSettingBackground() {
    add(yellowButton);
    add(blueButton);
    add(redButton);

    yellowButton.addActionListener(this);
    blueButton.addActionListener(this);
    redButton.addActionListener(this);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    Color color = getBackground();
    if (source == yellowButton)
      color = Color.yellow;
    else if (source == blueButton)
      color = Color.blue;
    else if (source == redButton)
      color = Color.red;
    setBackground(color);
    repaint();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("ButtonTest");
    frame.setSize(300, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new ButtonSettingBackground());

    frame.show();
  }
}