题目:构建聊天客户界面,包括消息显示文本框、消息发送文本框、选择好友的选择框、发送按钮。并为发送按钮添加事件,把消息显示在消息窗口中。
1.创建窗体与按钮、添加实训需要的组件
JTextField text ;//文本框JButton button ;//按钮JTextArea area ;//显示区域ButtonGroup group ;//创建ButtonGroup组对象@SuppressWarnings("rawtypes")//参数化对象框JComboBox box;//对象框@SuppressWarnings("unchecked")public JTextFrame(String title){super(title);//设置窗体标题//创建组件JPanel panel = new JPanel();JLabel label = new JLabel("聊天信息");text = new JTextField(20);button = new JButton("发 送");area = new JTextArea(15,20);JPanel panel2 = new JPanel();group = new ButtonGroup();//创建ButtonGroup组对象String[] str = {"张华", "李文"};box = new JComboBox(str);//添加组件//panel.add(label);panel.add(text);panel.add(box);panel.add(button);//定义并建立关系分类的树DefaultMutableTreeNode friend = new DefaultMutableTreeNode( " 好友 ",true );DefaultMutableTreeNode family = new DefaultMutableTreeNode( " 家人 ");DefaultMutableTreeNode schoolmate = new DefaultMutableTreeNode( " 朋友 ");family.add(new DefaultMutableTreeNode("张华"));schoolmate.add(new DefaultMutableTreeNode("李文"));friend.add(family);friend.add(schoolmate);JTree tree = new JTree(friend);JScrollPane sp1= new JScrollPane(tree);//区域划分this.setLayout(new BorderLayout());this.add(sp1,BorderLayout.EAST);this.add(panel,BorderLayout.SOUTH);this.add(area,BorderLayout.CENTER);//增加其他功能(未实现,实训内容可加可不加)JMenuBar menuBar = new JMenuBar();JMenu menu1 = new JMenu("管理");JMenu menu2 = new JMenu("帮助");JMenuItem item11 = new JMenuItem("退出");JMenuItem item21 = new JMenuItem("帮助");menu1.add(item11);menu2.add(item21);menuBar.add(menu1);menuBar.add(menu2);this.setJMenuBar(menuBar);//设置窗口尺寸this.setSize(600,400);this.setLocation(300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//注册动作事件监听button.addActionListener(new ButtonListener(this));}
2.按钮监听,显示发送的内容
class ButtonListener implements ActionListener{JTextFrame jf;public ButtonListener(JTextFrame jf) {this.jf=jf;}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub String str = jf.text.getText();if(str!=null&&!str.trim().equals("")) { //判断字符串不为空并且除去空格也不为空jf.area.append("我对"+jf.box.getSelectedItem()+"说:"+str+"\n");}}
}
以下是限定输入字符只能为数字(可加可不加,加则需要添加相应的import)
//确保输入的为数值,其它输不进去text.addKeyListener(new KeyAdapter(){public void keyTyped(KeyEvent e) {// TODO 自动生成的方法存根int ch = e.getKeyChar();if(ch>=KeyEvent.VK_0&&ch<=KeyEvent.VK_9){}else e.consume();}});
全代码如下:
/** @author:诗&影* 功能:简单的聊天发送端* 声明:如有不当之处,可以交流,加以改正。(内容简单 供参考)* */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;class ButtonListener implements ActionListener{JTextFrame jf;public ButtonListener(JTextFrame jf) {this.jf=jf;}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub String str = jf.text.getText();if(str!=null&&!str.trim().equals("")) { //判断字符串不为空并且除去空格也不为空jf.area.append("我对"+jf.box.getSelectedItem()+"说:"+str+"\n");}}
}class JTextFrame extends JFrame{JTextField text ;//文本框JButton button ;//按钮JTextArea area ;//显示区域ButtonGroup group ;//创建ButtonGroup组对象@SuppressWarnings("rawtypes")//参数化对象框JComboBox box;//对象框@SuppressWarnings("unchecked")public JTextFrame(String title){super(title);//设置窗体标题//创建组件JPanel panel = new JPanel();JLabel label = new JLabel("聊天信息");text = new JTextField(20);button = new JButton("发 送");area = new JTextArea(15,20);JPanel panel2 = new JPanel();group = new ButtonGroup();//创建ButtonGroup组对象String[] str = {"张华", "李文"};box = new JComboBox(str);//添加组件//panel.add(label);panel.add(text);panel.add(box);panel.add(button);//定义并建立关系分类的树DefaultMutableTreeNode friend = new DefaultMutableTreeNode( " 好友 ",true );DefaultMutableTreeNode family = new DefaultMutableTreeNode( " 家人 ");DefaultMutableTreeNode schoolmate = new DefaultMutableTreeNode( " 朋友 ");family.add(new DefaultMutableTreeNode("张华"));schoolmate.add(new DefaultMutableTreeNode("李文"));friend.add(family);friend.add(schoolmate);JTree tree = new JTree(friend);JScrollPane sp1= new JScrollPane(tree);//区域划分this.setLayout(new BorderLayout());this.add(sp1,BorderLayout.EAST);this.add(panel,BorderLayout.SOUTH);this.add(area,BorderLayout.CENTER);//增加其他功能(未实现,实训内容可加可不加)JMenuBar menuBar = new JMenuBar();JMenu menu1 = new JMenu("管理");JMenu menu2 = new JMenu("帮助");JMenuItem item11 = new JMenuItem("退出");JMenuItem item21 = new JMenuItem("帮助");menu1.add(item11);menu2.add(item21);menuBar.add(menu1);menuBar.add(menu2);this.setJMenuBar(menuBar);//设置窗口尺寸this.setSize(600,400);this.setLocation(300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//注册动作事件监听button.addActionListener(new ButtonListener(this));}
}public class chat {public static void main(String []args){new JTextFrame("微信聊天");}
}