发 帖  
原厂入驻New

JAVA实例程序: 控件的相互控制与消息传递

2008-12-6 12:32:53  1782
分享
<p><font face="Verdana">JAVA实例程序: 控件的相互控制与消息传递</font><br/></p>
<p><font face="Verdana">import java.awt.*;<br/>import java.awt.event.*;<br/>import javax.swing.*;</font></p>
<p><font face="Verdana">//控件的相互控制与消息传递</font></p>
<p><font face="Verdana">public class ActionDemo extends JFrame{<br/>&nbsp;JTextField jtfName;&nbsp; //名字输入域<br/>&nbsp;JTextArea jtaChat;&nbsp; //显示聊天信息<br/>&nbsp;JTextArea jtaInput;&nbsp; //输入消息<br/>&nbsp;JButton jbSend;&nbsp; //发送消息按钮<br/>&nbsp;JButton jbClear;&nbsp; //清除输入消息域</font></p>
<p><font face="Verdana">&nbsp;public ActionDemo(){<br/>&nbsp;&nbsp;super("控件的相互控制");&nbsp; //调用父类构造函数</font></p>
<p><font face="Verdana">&nbsp;&nbsp;Container container=this.getContentPane();&nbsp; //得到容器<br/>&nbsp;&nbsp;JPanel p=new JPanel();&nbsp; //初始化一个面板<br/>&nbsp;&nbsp;jtfName=new JTextField(10);&nbsp; //初始化名字输入域<br/>&nbsp;&nbsp;p.add(new JLabel("主题:Write Once, Run Anywhere.&nbsp;&nbsp;&nbsp;&nbsp; "));&nbsp; //增加聊天题标签<br/>&nbsp;&nbsp;p.add(new JLabel("昵称"));&nbsp; //增加昵称标签<br/>&nbsp;&nbsp;p.add(jtfName);&nbsp; //增加名字输入域<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.add(p,BorderLayout.NORTH);&nbsp; //在容器上增加面板</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jtaChat=new JTextArea();&nbsp; //初始化信息显示文本框<br/>&nbsp;&nbsp; &nbsp;container.add(new JScrollPane(jtaChat),BorderLayout.CENTER);&nbsp; //在容器上增加信息显示文本框</font></p>
<p><font face="Verdana">&nbsp;&nbsp;Box box=new Box(BoxLayout.X_AXIS);&nbsp; //初始化一个Box<br/>&nbsp;&nbsp;jtaInput=new JTextArea(3,20);&nbsp; //初始化消息输入域<br/>&nbsp;&nbsp;jbSend=new JButton();&nbsp; //初始化发送按钮<br/>&nbsp;&nbsp;jbClear=new JButton();&nbsp;&nbsp; //初始化清除按钮<br/>&nbsp;&nbsp;jbClear.setText("清除");&nbsp; //设置按钮文本<br/>&nbsp;&nbsp;box.add(new JScrollPane(jtaInput));&nbsp; //增加消息输入域<br/>&nbsp;&nbsp;box.add(jbClear);&nbsp; //增加按钮<br/>&nbsp;&nbsp;box.add(jbSend);<br/>&nbsp;&nbsp;container.add(box,BorderLayout.SOUTH);&nbsp; //在容器上增加box</font></p>
<p><font face="Verdana">&nbsp;&nbsp;jbClear.addActionListener(new ActionListener(){&nbsp; //清除按钮事件处理<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;public void actionPeRFormed(ActionEvent e){<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;jtaInput.setText("");&nbsp; //点击清除按钮时清除输入域的信息<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; Action sendMessage = new AbstractAction() {&nbsp; //发送消息Action<br/>&nbsp;&nbsp;&nbsp; &nbsp;public void actionPerformed(ActionEvent e){<br/>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;replaceMessage();&nbsp; //更新消息显示框<br/>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}<br/>&nbsp;&nbsp;};<br/>&nbsp;&nbsp;jtaInput.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"send");&nbsp; //键盘事件处理,按受回车事件<br/>&nbsp;&nbsp;jtaInput.getActionMap().put("send",sendMessage);&nbsp; //回车时的处理(调用发送消息Action)</font></p>
<p><font face="Verdana">&nbsp;&nbsp;jbSend.setAction(sendMessage);&nbsp; //设置命令为发送消息<br/>&nbsp;&nbsp;jbSend.setText("发送");&nbsp; //设置按钮文本</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; setSize(400,200);&nbsp; //设置窗口尺寸<br/>&nbsp;&nbsp;&nbsp; setVisible(true);&nbsp; //设置窗口可视<br/>&nbsp;&nbsp;&nbsp; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; //关闭窗口时退出程序<br/>&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;private void replaceMessage(){<br/>&nbsp;&nbsp;&nbsp; String message=jtfName.getText()+"&gt; "+jtaInput.getText()+"\n";&nbsp; //得到消息文本<br/>&nbsp;&nbsp;&nbsp; jtaChat.insert(message,jtaChat.getDocument().getLength());&nbsp; //插入消息到显示域未端<br/>&nbsp;&nbsp;&nbsp; jtaInput.setText("");&nbsp; //清空输入消息域<br/>&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;&nbsp; public static void main(String[] args){<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new ActionDemo();<br/>&nbsp;&nbsp; }<br/>}</font></p>
oJ52c4wS.rar (10.13 KB, 下载次数: 0)
<br/>
0
2008-12-6 12:32:53   评论 分享淘帖

只有小组成员才能发言,加入小组>>

135个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /7 下一条

快速回复 返回顶部 返回列表