📄 talkfrm.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.event.*;
public class TalkFrm extends JFrame implements ActionListener{
private MyEditorPane showPane;
private JEditorPane editorPane;
private JPanel downP;
private JPanel upP;
private JPanel fontSetP;
private JButton fontBtn,faceBtn;
private FaceFrm faceFrm;
private Font currentFont;
private MyFont myFont;
private boolean isEnter= false;
private String myIp, talkerIp,talkerNickname,myNickname;
private DatagramSocket socketS;
private DatagramPacket packetS;
public TalkFrm(String talkerNickname,
String myNickname,
String talkerIp){
buildGUI(myNickname,talkerNickname);
init(talkerIp);
hookEvent();
}
private void showTalkMsg(String s){
StringTokenizer st = new StringTokenizer(s,"$");
//显示图像时候
if(st.countTokens()==1){
this.showPane.appendImage(s, false);
}
else {
//显示文字时候,前一部分为消息,后部分为字体信息
this.showPane.appendText(st.nextToken(),
MyFont.createMyFont(st.nextToken()),
false);
}
}
private void buildGUI(String myNickname,String talkerNickname){
this.myNickname = myNickname;
this.talkerNickname = talkerNickname;
Container c = this.getContentPane();
Color color = new Color(255,200,200);
Border thinBorder = BorderFactory.createLineBorder(color);
Border boldBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED,Color.pink,color);
showPane = new MyEditorPane(myNickname,talkerNickname);
showPane.setBorder(boldBorder);
editorPane = new JEditorPane();
editorPane.setBorder(boldBorder);
JPanel toolBar = new JPanel();
toolBar.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
fontBtn = new JButton();
fontBtn.setIcon(new ImageIcon("Icon/font.gif"));
fontBtn.setPreferredSize(new Dimension(20,20));
fontBtn.setBorderPainted(false);
fontBtn.setBackground(Color.pink);
fontBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontSetP.setVisible(!fontSetP.isVisible());
}
});
faceBtn = new JButton();
faceBtn.setBorderPainted(false);
faceBtn.setBackground(Color.pink);
faceBtn.setPreferredSize(new Dimension(20,20));
faceBtn.setIcon(new ImageIcon("Icon/face.gif"));
faceBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
faceFrm.setVisible(!faceFrm.isVisible());
}
});
toolBar.add(fontBtn);
toolBar.add(faceBtn);
toolBar.setBackground(Color.pink);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(1,2));
JPanel leftP = new JPanel();
leftP.setBackground(Color.pink);
leftP.setLayout(new FlowLayout(FlowLayout.LEFT,2,0));
JButton talkLogBtn = new JButton("聊天记录");
JButton clearBtn = new JButton("清除");
talkLogBtn.addActionListener(this);
clearBtn.addActionListener(this);
leftP.add(talkLogBtn);
leftP.add(clearBtn);
JPanel rightP = new JPanel();
rightP.setBackground(Color.pink);
rightP.setLayout(new FlowLayout(FlowLayout.RIGHT,2,0));
JButton sendBtn = new JButton("发送");
JButton keysetBtn = new JButton("快捷键");
sendBtn.addActionListener(this);
keysetBtn.addActionListener(this);
rightP.add(sendBtn);
rightP.add(keysetBtn);
buttonPane.add(leftP);
buttonPane.add(rightP);
downP = new JPanel();
downP.setLayout(new BorderLayout());
downP.add(toolBar,BorderLayout.NORTH);
downP.add(editorPane,BorderLayout.CENTER);
downP.add(buttonPane,BorderLayout.SOUTH);
downP.setPreferredSize(new Dimension(500,200));
fontSetP = new JPanel();
fontSetP.setBackground(Color.pink);
fontSetP.setBorder(thinBorder);
fontSetP.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
final JComboBox fontNameBox = new JComboBox();
fontNameBox.setMaximumRowCount(5);
final String[] localFontNames =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i=0;i < localFontNames.length; ++i){
fontNameBox.addItem(localFontNames[i]);
}
fontNameBox.addItem(" ");
fontNameBox.setSelectedItem("宋体");
fontNameBox.setBackground(Color.white);
fontNameBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(fontNameBox.getSelectedIndex()==localFontNames.length){
fontNameBox.setSelectedItem("宋体");
}
currentFont = new Font(
(String)fontNameBox.getSelectedItem(),
currentFont.getStyle(),
currentFont.getSize());
editorPane.setFont(currentFont);
myFont.fontName = (String)fontNameBox.getSelectedItem();
}
});
final JComboBox fontSizeBox = new JComboBox();
fontSizeBox.addItem("20");
fontSizeBox.addItem("25");
fontSizeBox.addItem("32");
fontSizeBox.addItem("40");
fontSizeBox.addItem("65");
fontSizeBox.addItem(" ");
fontSizeBox.setBackground(Color.white);
fontSizeBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(fontSizeBox.getSelectedIndex()==5){
fontSizeBox.setSelectedItem("20");
}
currentFont = new Font(
currentFont.getName(),
currentFont.getStyle(),
Integer.parseInt((String)fontSizeBox.getSelectedItem()));
myFont.size = 3+ fontSizeBox.getSelectedIndex()% 5;
editorPane.setFont(currentFont);
}
});
JButton boldBtn = new JButton();
boldBtn.setBackground(Color.pink);
boldBtn.setIcon(new ImageIcon("Icon/bold.gif"));
boldBtn.setPreferredSize(new Dimension(20,20));
boldBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int style;
if(currentFont.isPlain()){
style = Font.BOLD;
}
else if(currentFont.isBold()){
if(currentFont.isItalic()){
style = Font.ITALIC;
}
else{
style = Font.PLAIN;
}
}
else{
style = Font.ITALIC|Font.BOLD;
}
currentFont = new Font(currentFont.getName(),
style,
currentFont.getSize());
editorPane.setFont(currentFont);
myFont.style = style;
}
});
JButton italicBtn = new JButton();
italicBtn.setBackground(Color.pink);
italicBtn.setIcon(new ImageIcon("Icon/italic.gif"));
italicBtn.setPreferredSize(new Dimension(20,20));
italicBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int style;
if(currentFont.isPlain()){
style = Font.ITALIC;
}
else if(currentFont.isItalic()){
if(currentFont.isBold()){
style = Font.BOLD;
}
else{
style = Font.PLAIN;
}
}
else{
style = Font.ITALIC|Font.BOLD;
}
currentFont = new Font(currentFont.getName(),
style,
currentFont.getSize());
editorPane.setFont(currentFont);
myFont.style = style;
}
});
JButton underlineBtn = new JButton();
underlineBtn.setBackground(Color.pink);
underlineBtn.setIcon(new ImageIcon("Icon/underline.gif"));
underlineBtn.setPreferredSize(new Dimension(20,20));
underlineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
myFont.isUnderLine = true;
}
});
JButton colorBtn = new JButton();
colorBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editorPane.setForeground(
JColorChooser.showDialog(
TalkFrm.this, "选取颜色", Color.black));
myFont.color = MyUtilities.getColor0XFromRGB(editorPane.getForeground());
}
});
colorBtn.setBackground(Color.pink);
colorBtn.setIcon(new ImageIcon("Icon/color.gif"));
colorBtn.setPreferredSize(new Dimension(20,20));
fontSetP.add(fontNameBox);
fontSetP.add(fontSizeBox);
fontSetP.add(boldBtn);
fontSetP.add(italicBtn);
fontSetP.add(underlineBtn);
fontSetP.add(colorBtn);
fontSetP.setVisible(false);
upP = new JPanel();
upP.setLayout(new BorderLayout());
upP.add(showPane,BorderLayout.CENTER);
upP.add(fontSetP,BorderLayout.SOUTH);
upP.setPreferredSize(new Dimension(500,300));
c.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
c.add(upP);
c.add(downP);
this.setSize(500,535);
this.setIconImage(new ImageIcon("Icon/titleIcon.gif").getImage());
this.setTitle("与猫猫聊天中");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//居中
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private void hookEvent(){
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
Dimension d = TalkFrm.this.getContentPane().getSize();
downP.setPreferredSize(new Dimension(d.width,2*d.height/5));
upP.setPreferredSize(new Dimension(d.width,3*d.height/5));
downP.revalidate();
upP.revalidate();
}
});
this.editorPane.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER&&
e.isControlDown()&& !isEnter){
sendMsg();
MyUtilities.deleteUpLine();
}
else if(e.getKeyCode()==KeyEvent.VK_ENTER&&isEnter){
sendMsg();
MyUtilities.deleteUpLine();
}
}
});
}
private void sendMsg(){
if(!this.editorPane.getText().equals("")){
this.showPane.appendText(editorPane.getText(),this.myFont,true);
//发给对方信息除去分割符$
String s = this.myNickname+"$"+this.talkerNickname+"$"+
myIp+"$"+
editorPane.getText().replace('$', ' ')+
"$"+this.myFont.toString();
this.editorPane.setText("");
try {
packetS = new DatagramPacket(s.getBytes(),0,s.length(),
InetAddress.getByAddress(MyUtilities.getByteFromString(talkerIp)),
9000);
//发送数据报
this.socketS.send(packetS);
} catch (Exception e)
{
e.printStackTrace();
}
return;
}
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null,"不能发送空消息!");
this.editorPane.setCaretPosition(0);
}
private void init(String talkerIp){
if(faceFrm == null){
faceFrm = new FaceFrm();
faceFrm.setLocation(this.getLocation().x,
this.getLocation().y+upP.getPreferredSize().height+50);
}
this.currentFont = new Font("宋体",Font.ROMAN_BASELINE,20);
this.editorPane.setFont(this.currentFont);
this.myFont = new MyFont();
//初始化连接
try {
this.socketS = new DatagramSocket();
this.myIp = InetAddress.getLocalHost().getHostAddress();
} catch (Exception e)
{}
this.talkerIp = talkerIp;
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if(s.equals("发送")){
sendMsg();
}
else if(s.equals("清除")){
editorPane.setText("");
}
else if(s.equals("快捷键")){
// dealKeySet();
}else{
// dealTalkLog();
}
}
private class FaceFrm extends JDialog{
public FaceFrm(){
this.add(new FaceCanvas());
this.setUndecorated(true);
this.pack();
}
private class FaceCanvas extends Canvas{
private ImageIcon img;
private int CELL;
private int currentX,currentY;
public FaceCanvas(){
img = new ImageIcon("GIF/faces.gif");
currentX =0;
currentY =0;
this.setPreferredSize(
new Dimension(img.getIconWidth(),img.getIconHeight()));
CELL = img.getIconWidth()/15;
this.setFocusable(true);
hookEvent();
}
public void paint(Graphics g){
g.drawImage(img.getImage(), 0, 0, null);
g.draw3DRect(currentX*CELL, currentY*CELL, CELL,CELL,true);
}
private void hookEvent(){
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
currentX = e.getPoint().x/CELL;
currentY = e.getPoint().y/CELL;
FaceCanvas.this.repaint();
}
});
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
dealEnter();
}
public void mouseExited(MouseEvent e){
FaceFrm.this.setVisible(false);
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_ESCAPE:
FaceFrm.this.setVisible(false);
break;
case KeyEvent.VK_UP:
currentY = currentY-1<0?0:currentY-1;
FaceCanvas.this.repaint();
break;
case KeyEvent.VK_DOWN:
currentY = (currentY+1) % 6;
FaceCanvas.this.repaint();
break;
case KeyEvent.VK_RIGHT:
currentX = (currentX+1) % 16;
FaceCanvas.this.repaint();
break;
case KeyEvent.VK_LEFT:
currentX = currentX-1<0?0:currentX-1;
FaceCanvas.this.repaint();
break;
case KeyEvent.VK_ENTER:
dealEnter();
}
}
});
}
private void dealEnter(){
//显示自己的一端
String imgName = String.valueOf(currentY)+currentX;
TalkFrm.this.showPane.appendImage(imgName,true);
MyUtilities.playSound("Sound/Ping.wav");
//发送给对方
try {
packetS = new DatagramPacket(imgName.getBytes(),0,imgName.length(),
InetAddress.getByAddress(talkerIp.getBytes()),
9000);
//发送数据报
socketS.send(packetS);
} catch (Exception e)
{}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -