Monday, October 24, 2011
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


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
Offline mode (View only)
Nothing to copy
Clear all items
Web clipboard help
1.0
1.15
1.5
2.0
Add space before paragraph
Add space after paragraph
Arabic
Bulgarian
Catalan
Chinese (Simplified)
Chinese (Traditional)
Croatian
Czech
Danish
Dutch
English
Finnish
French
German
Greek
Hebrew
Hindi
Hungarian
Indonesian
Italian
Japanese
Korean
Latvian
Lithuanian
Norwegian
Polish
Portuguese
Romanian
Russian
Serbian
Slovak
Slovenian
Spanish
Swedish
Thai
Turkish
Ukrainian
Vietnamese
Share...
Publish to the Web...
See revision history
Language►
New►
⌘OOpen...
Rename...
Make a copy...
Download as►
Edit description...
Report abuse
Email collaborators...
Email as attachment...
Page setup...
Print settings...
Print preview
⌘PPrint
⌘ZUndo
⌘YRedo
⌘XCut
⌘CCopy
⌘VPaste
⌘ASelect all
⌘⇧ASelect none
Find and replace...
Document view►
Show ruler
Show equation toolbar
Show spelling suggestions
⌃⇧FCompact controls
Full screen
Image...
⌘KLink...
Equation...
Drawing...
⌘⌥MComment
⌘⌥FFootnote
Special characters...
Horizontal line
Page number►
Page count
Page break
Header
Footer
Bookmark
Table of contents
⌘BBold
⌘IItalic
⌘UUnderline
Strikethrough
⌘.Superscript
⌘,Subscript
Paragraph styles►
Align►
Line spacing►
List styles►
⌘\Clear formatting
Translate document...
⌘⇧YDefine...
⌘⇧CWord count
Preferences...
Insert table►
Insert row above
Insert row below
Insert column left
Insert column right
Delete table
Delete row
Delete column
Table properties...
Google Docs Help Center
Learn from other Google users
Report an issue
Use the classic look
⌘/Keyboard shortcuts
| Keyboard shortcuts | Open in a new window | Close |
|
|
Every change you make is automatically saved.
Private to only me | |
| Only you can access. |
« Close preview
Print
Why are some users anonymous? Learn more
7 Day Activity
Hide document stats ↑Views
Comments
ldanialy
Your +reply will add people to this post and send an email.
Comment
Cancel
Document stats ↓
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();
}
}
Subscribe to:
Posts (Atom)