A simple Swing GUI example for Observer Design Pattern
This example shows how to create a Swing GUI example, and explain why it is an example usage of Observer Design Pattern.
Complete Code
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; public class SimpleSwingExample { public static void main(String[] args) { JFrame frame = new JFrame("Frame Title"); final JTextArea comp = new JTextArea(); JButton btn = new JButton("click"); frame.getContentPane().add(comp, BorderLayout.CENTER); frame.getContentPane().add(btn, BorderLayout.SOUTH); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { comp.setText("Button has been clicked"); } }); int width = 300; int height = 300; frame.setSize(width, height); frame.setVisible(true); } } |
Step-by-step explanation
Firstly, we need a container like a Frame, a Window, or an Applet to display components like panels, buttons, text areas etc.
JFrame frame = new JFrame("Frame Title"); |
Create some components such as panels, buttons, text areas etc.
final JTextArea comp = new JTextArea(); JButton btn = new JButton("click"); |
Add components to display area and arrange its layout using the LayoutManagers.
frame.getContentPane().add(comp,BorderLayout.CENTER); frame.getContentPane().add(btn, BorderLayout.SOUTH); |
Attach a listener to the button component. Interacting with a Component causes an Event to occur. To associate a user action with a component, attach a listener to it.
Here addActionListener method is the subject's register observer method. For a complete example of Observer design pattern, go to Observer example.
btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ comp.setText("Button has been clicked"); } }); |
public interface ActionListener extends EventListener
The listener interface is for receiving action events. The class (Main, in this case) that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
Show the Frame.
int width = 300; int height = 300; frame.setSize(width, height); frame.setVisible(true); |
<pre><code> String foo = "bar"; </code></pre>
Pingback: Most Widely Used Java Libraries | bakinotes()
Pingback: 观察者模式 | DreamCore()
Pingback: 讲故事,学(Java)设计模式—观察者模式 - Noblog其他 | Noblog()
Pingback: 最常用的Java库一览 | 其他 | Noblog()
Pingback: 免费Simple Java (非常简单的英文) - IT新闻()
Pingback: 免费Simple Java (非常简单的英文) | | Evolution Unit 进化Evolution Unit 进化()
Pingback: 免费Simple Java (非常简单的英文) | 我爱互联网()
Pingback: Simple Java Questions | Need 2 Remember()