㈠ 在java中qq界面中怎么按enter键发送消息
方法一:
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
{
privateJButtonb1=newJButton("Click");
publicButtonTest(){
this.getContentPane().add(b1);
b1.addKeyListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.pack();
}
publicvoidkeyReleased(KeyEventke){
}
publicvoidkeyPressed(KeyEventke){
if(ke.getKeyChar()==ke.VK_ENTER){
System.out.println("ok................");
}
}
publicvoidkeyTyped(KeyEventke){
}
publicstaticvoidmain(String[]args){
newButtonTest().show();
}
}
方法二:
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
{
privateJButtonb1=newJButton("Click");
publicButtonTest(){
this.getContentPane().add(b1);
b1.addKeyListener(newKeyAdapter(){
publicvoidkeyPressed(KeyEventke){
if(ke.getKeyChar()==ke.VK_ENTER){
System.out.println("ok..............");
}
}
}
);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.pack();
}
publicstaticvoidmain(String[]args){
newButtonTest().show();
}
}
㈡ 急求JAVA 编辑类似QQ样子的聊天窗口代码!!
/**
*网络聊天工具
*左下角输入框输入对方的ip地址
*右下角输入框输入要发送的消息
*端口号:3000
*接收的消息在上方对话框中显示,新消息在上面
*/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class Chat extends Frame
{
List lst = new List(8); //最多显示六条
TextField tfIP = new TextField(13); //IP地址输入文本框
TextField tfData = new TextField(50); //定义输入消息文本框
DatagramSocket ds = null;
public Chat() {
try
{
ds = new DatagramSocket(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
this.add(lst,"Center"); //增加列表框
Panel p = new Panel();
this.add(p,"South");
p.setLayout(new BorderLayout());
p.add(tfIP,"West"); //IP输入
p.add(tfData,"East"); //消息输入
new Thread(new Runnable()
{
public void run()
{
byte buf [] =new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,1024);
//显示消息
while(true)
{
try
{
ds.receive(dp);
lst.add(new String(buf,0,dp.getLength())+
" -->IP:"+dp.getAddress().getHostAddress()+" Port:"+dp.getPort(),0);
//新消息指定显示在第一行
//显示格式:消息from ip地址:端口号
}
catch (Exception e)
{
if(!ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
tfData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//取出消息及ip文本框中的内容
byte [] buf;
buf =tfData.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,
InetAddress.getByName(tfIP.getText()),3000);
ds.send(dp);
}
catch (Exception ex)
{
ex.printStackTrace();
}
//清空
tfData.setText("");
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
ds.close();
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting Chat……");
Chat mainFrame = new Chat();
mainFrame.setSize(500,500);
mainFrame.setTitle("迷你聊天工具");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
}
㈢ qq聊天界面怎么写啊,用java语言写
爱应用团队为你解答
网络资料
package cn.myself.myproject.FrameProject;import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;import cn.myself.myproject.employeepj.view.common.CenterWindow;
/**
* 程序功能:QQ登陆面板
* 学习内容:GridBagLayout布局方式的学习
* 以GridBagLayout方式布局的容器,其容器中的每个组件必须由一个GridBagConstrains类的实例对象进行大小,位置等约束。
* @author huliu 2009-06-26
* 难题:a.帐号后面是什么框?
* b.图片的相对路径怎么设置?
*/
public class QQRegistBoard extends JFrame{
JPanel p1;
GridBagLayout gb1;
GridBagConstraints gbc1;
JButton btn1,btn2;
JLabel label0,label1,label2,label3,label4,label5;
JTextField text1,text2;
JComboBox box1,box2;
JCheckBox check1,check2;
JList list1;
/**
* 构造方法
*/
public QQRegistBoard(){
super("2009正式版(huliu)");
p1=new JPanel();
gb1=new GridBagLayout();
gbc1=new GridBagConstraints();
p1.setLayout(gb1);//GridBagLayout布局。网袋布局
getContentPane().add(p1); //取得当前容器对象
this.setSize(350,250);
CenterWindow.centerW(this);
Icon icon1 = new ImageIcon("./QQ2.jpg");
// Icon icon1 = new ImageIcon("./QQ.jpg"); //加载图片,当前目录下的QQ.jpg
// Icon icon1 = new ImageIcon("src/cn/mysef/images/QQ1.jpg");
label0=new JLabel(icon1);
label1=new JLabel("帐号:");
label2=new JLabel("注册新帐号");
label3=new JLabel("密码:");
label4=new JLabel("取回密码");
label5=new JLabel("状态:");
text1=new JTextField(10);
text2=new JTextField(10);
String[] str1={"313558851","313857401","690442763"};
box1=new JComboBox(str1);
box1.setEditable(true);//设置ComboBox字段值是否为可编辑
box2=new JComboBox();
check1=new JCheckBox("记住密码",true);
check2=new JCheckBox("自动登录");
btn1=new JButton("设置");
btn2=new JButton("登录");
p1.add(label0,GBC(0,0,3,1,new Insets(5,2,2,4)));//图片
p1.add(label1,GBC(1,0,1,1,new Insets(4,2,2,4)));
p1.add(box1, GBC(1,1,1,1,new Insets(4,2,2,0)));
//p1.add(text2,GBC(1,1,1,1));
p1.add(label2,GBC(1,2,1,1,new Insets(4,2,2,3)));
p1.add(label3,GBC(2,0,1,1,new Insets(4,2,2,3)));
p1.add(text1, GBC(2,1,1,1,new Insets(5,2,2,3)));
p1.add(label4,GBC(2,2,1,1,new Insets(4,2,2,3)));
p1.add(label5,GBC(3,0,1,1,new Insets(4,2,2,3)));
p1.add(check1,GBC(3,1,1,1,new Insets(4,2,2,3)));
p1.add(check2,GBC(3,2,1,1,new Insets(4,2,2,3)));
p1.add(btn1 ,GBC(4,0,1,1,new Insets(4,2,2,3)));
p1.add(btn2 ,GBC(4,2,1,1,new Insets(4,2,2,3)));
}
/**
* GBC方法:功能是设计以GridBagLayout方式布局的容器(如Panel容器对象)内的组件的位置,大小等约束的。
* @param gridy
* @param gridx
* @param gridwidth
* @param gridheight
* @return GridBagStraints实对象
* Insets(int top, int left, int bottom, int right),与其它组件之间距离(上,左,下,右)
*/
public GridBagConstraints GBC(int gridy,int gridx,int gridwidth,int gridheight,Insets insets){
GridBagConstraints gbc1=new GridBagConstraints();
gbc1.gridx=gridx; //列
gbc1.gridy=gridy; //行
gbc1.gridwidth=gridwidth;//宽度
gbc1.gridheight=gridheight; //高度
//insets=new Insets(1,1,1,1);
gbc1.insets=insets;
return gbc1;
}
public static void main(String[] args){
new QQRegistBoard().setVisible(true);
}}
望采纳