在对象间“广播消息”
来源:互联网 发布:千牛淘宝助手在哪里 编辑:程序博客网 时间:2024/04/30 05:08
最近在看金旭日老师的Java课程,为后期的通过Android实现某个项目做准备:
目标:完成 在对象之间“广播消息”http://www.jinxuliang.com/course/PPT/Show/5469992f137e420664f62752
参考:http://www.jinxuliang.com/course/PPT/Show/54699909137e420664f62751
界面依然用的是Swing Designer设计的:
//功能:在对象间“广播消息”//http://www.jinxuliang.com/course/PPT/Show/5469992f137e420664f62752import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class GUITest2 extends JFrame {private JPanel contentPane;/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {GUITest2 frame = new GUITest2();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public GUITest2() {setTitle("\u4E3B\u7A97\u4F53");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JButton btnNewButton_Counter = new JButton("\u70B9\u51FB\u589E\u52A0\u8BA1\u6570\u503C");btnNewButton_Counter.setFont(new Font("微软雅黑", Font.PLAIN, 16));btnNewButton_Counter.setBounds(93, 121, 246, 48);contentPane.add(btnNewButton_Counter);JButton btnNewButton = new JButton("\u70B9\u51FB\u65B0\u5EFA\u4E00\u4E2A\u4ECE\u7A97\u4F53\u5BF9\u8C61");btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {BlankFrame frame=new BlankFrame(btnNewButton_Counter);//这里是要点:新的窗体的构造函数接收一个JButton对象frame.setVisible(true);//显示}});btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));btnNewButton.setBounds(93, 36, 246, 48);contentPane.add(btnNewButton);}/** A blank frame that can be countered by clicking a button.*/static class BlankFrame extends JFrame{public BlankFrame(JButton Counter){counterTitle++;setTitle("从窗体"+counterTitle);setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);setLocation(SPACING * counterTitle, SPACING * counterTitle); JTextField Text=new JTextField();Text.setEditable(false);//不能编辑add(Text);//添加文本框String str=Integer.toString(counterNumber);Text.setText(str);//设置文本框默认值counterListener=new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubcounterNumber++;String str=Integer.toString(counterNumber);Text.setText(str);//修改按钮响应事件}};Counter.addActionListener(counterListener);}private ActionListener counterListener;private static int counterTitle = 0;private int counterNumber = 0;public static final int DEFAULT_WIDTH = 200;public static final int DEFAULT_HEIGHT = 150; public static final int SPACING = 40;}}结果:
0 0
- 在对象间“广播消息”
- 简单演示不同对象间的消息发送和接收(广播消息中心)
- C#对象间的协作和信息交换(五)利用委托(Delegate)广播消息
- C#对象间的协作和信息交换(六)利用事件(Event)广播消息
- 广播消息
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- android fragment间的广播消息接收
- C#对象间的协作和信息交换(四)主窗体向多个子窗体广播消息
- Spring消息广播机制
- 实现COM消息广播
- 实现COM消息广播
- 实现COM消息广播
- 发送广播消息zz
- 实现COM消息广播
- C#广播消息处理
- dictHsCode is not mapped
- Leetcode 298Binary Tree Longest Consecutive Sequence
- struts2标签
- 博客开通,留个纪念
- NOIP 2015 D1 T2 信息传递
- 在对象间“广播消息”
- HDU 3085 Nightmare Ⅱ(双向BFS)
- Leetcode 156Binary Tree Upside Down
- PropertyNotFoundException: Property 'dFlag' not found
- 【NYOJ】[94]cigarettes
- Ugly Number II
- source Insight 注册码
- Android中Context详解 ---- 你所不知道的Context
- 【Python】给pypy安装第三方库