首页 > 代码库 > day 3: QQMain.java

day 3: QQMain.java

技术分享

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class QQMain extends JFrame implements KeyListener,ActionListener{
//用于传递数据的组件
JTextField txtMess = new JTextField();
JTextArea txtContent = new JTextArea();
//构造方法
QQMain(){
//窗体基本信息
this.setSize(800 , 900);
this.setLocationRelativeTo(null);
this.setTitle("Talking.GHOUL");
//logo设置
//设置窗体LOGO
this.setIconImage(this.getToolkit().getImage("D:/lab_2/psb (11).jpg"));

//new组件
txtMess.setFont(new Font("", 0, 36));
txtContent.setFont(new Font("", 0, 36));
txtContent.setEditable(false);

JButton btnSend = new JButton("Send");
btnSend.setFont(new Font("Courier", Font.BOLD, 26));

JComboBox cmbUser = new JComboBox();

//注册事件监听
btnSend.addActionListener(this);

//设置滚动条
JScrollPane spContent = new JScrollPane(txtContent);

//布置小面板
JPanel panSmall = new JPanel();
panSmall.setLayout(new GridLayout(1 , 2));

panSmall.add(cmbUser);
panSmall.add(btnSend);

//布置大面板
JPanel panBig = new JPanel();
panBig.setLayout(new GridLayout(2 , 1));

panBig.add(txtMess);
panBig.add(panSmall);
//设置大面板
panBig.setPreferredSize(new Dimension(200 , 100));


//布置窗体
this.setLayout(new BorderLayout());

this.add(spContent , BorderLayout.CENTER);
this.add(panBig , BorderLayout.SOUTH);
}
public static void main(String[] args){
QQMain w = new QQMain();
w.setVisible(true);
}

@Override
public void keyPressed(KeyEvent arg0){

}
@Override
public void keyReleased(KeyEvent arg0){}
@Override
public void keyTyped(KeyEvent arg0){}

@Override
public void actionPerformed(ActionEvent arg0){
//System.out.println(arg0);
if(arg0.getActionCommand().equals("Send")){
String mess = txtMess.getText();
txtMess.setText("");
txtContent.append(‘ ‘+mess+‘\n‘);
}
}
}

day 3: QQMain.java