JAVA--聊天界面面板
package windows.beautify; import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File; import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument; /**
* JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能
* @author 五斗米 <如转载请保留作者和出处>
* @blog http://blog.csdn.net/mq612
*/ public class ToolTip extends JFrame { private static final long serialVersionUID = -2397593626990759111L; private JScrollPane scrollPane = null; // 滚动 private JTextPane text = null; // 不用说了,如果不认识的话就没必要往后看了 private Box box = null; // 放输入组件的容器 private JButton b_insert = null, b_remove = null, b_icon = null; // 插入按钮;清除按钮;插入图片按钮 private JTextField addText = null; // 文字输入框 private JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,
fontBackColor = null; // 字体名称;字号大小;文字样式;文字颜色;文字背景颜色 private StyledDocument doc = null; // 非常重要插入文字样式就靠它了 public ToolTip() {
super("JTextPane Test");
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
text = new JTextPane();
text.setEditable(false); // 不可录入
doc = text.getStyledDocument(); // 获得JTextPane的Document
scrollPane = new JScrollPane(text);
addText = new JTextField(18);
String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
fontName = new JComboBox(str_name); // 字体名称
fontSize = new JComboBox(str_Size); // 字号
fontStyle = new JComboBox(str_Style); // 样式
fontColor = new JComboBox(str_Color); // 颜色
fontBackColor = new JComboBox(str_BackColor); // 背景颜色
b_insert = new JButton("插入"); // 插入
b_remove = new JButton("清空"); // 清除
b_icon = new JButton("图片"); // 插入图片
b_insert.addActionListener(new ActionListener() { // 插入文字的事件
public void actionPerformed(ActionEvent e) {
insert(getFontAttrib());
addText.setText("");
}
});
b_remove.addActionListener(new ActionListener() { // 清除事件
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});
b_icon.addActionListener(new ActionListener() { // 插入图片事件
public void actionPerformed(ActionEvent arg0) {
JFileChooser f = new JFileChooser(); // 查找文件
f.showOpenDialog(null);
insertIcon(f.getSelectedFile()); // 插入图片
}
});
box = Box.createVerticalBox(); // 竖结构
Box box_1 = Box.createHorizontalBox(); // 横结构
Box box_2 = Box.createHorizontalBox(); // 横结构
box.add(box_1);
box.add(Box.createVerticalStrut(8)); // 两行的间距
box.add(box_2);
box.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); // 8个的边距
// 开始将所需组件加入容器
box_1.add(new JLabel("字体:")); // 加入标签
box_1.add(fontName); // 加入组件
box_1.add(Box.createHorizontalStrut(8)); // 间距
box_1.add(new JLabel("样式:"));
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("字号:"));
box_1.add(fontSize);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("颜色:"));
box_1.add(fontColor);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("背景:"));
box_1.add(fontBackColor);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b_icon);
box_2.add(addText);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b_insert);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b_remove);
this.getRootPane().setDefaultButton(b_insert); // 默认回车按钮
this.getContentPane().add(scrollPane);
this.getContentPane().add(box, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
addText.requestFocus();
} /**
* 插入图片
*
* @param icon
*/
private void insertIcon(File file) {
text.setCaretPosition(doc.getLength()); // 设置插入位置
text.insertIcon(new ImageIcon(file.getPath())); // 插入图片
insert(new FontAttrib()); // 这样做可以换行
} /**
* 将文本插入JTextPane
*
* @param attrib
*/
private void insert(FontAttrib attrib) {
try { // 插入文本
doc.insertString(doc.getLength(), attrib.getText() + "/n", attrib.getAttrSet());
} catch (BadLocationException e) {
e.printStackTrace();
}
} /**
* 获取所需要的文字设置
*
* @return FontAttrib
*/
private FontAttrib getFontAttrib() {
FontAttrib att = new FontAttrib();
att.setText(addText.getText());
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规")) {
att.setStyle(FontAttrib.GENERAL);
} else if (temp_style.equals("粗体")) {
att.setStyle(FontAttrib.BOLD);
} else if (temp_style.equals("斜体")) {
att.setStyle(FontAttrib.ITALIC);
} else if (temp_style.equals("粗斜体")) {
att.setStyle(FontAttrib.BOLD_ITALIC);
}
String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
} else if (temp_color.equals("红色")) {
att.setColor(new Color(255, 0, 0));
} else if (temp_color.equals("蓝色")) {
att.setColor(new Color(0, 0, 255));
} else if (temp_color.equals("黄色")) {
att.setColor(new Color(255, 255, 0));
} else if (temp_color.equals("绿色")) {
att.setColor(new Color(0, 255, 0));
}
String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
} else if (temp_backColor.equals("淡红")) {
att.setBackColor(new Color(255, 200, 200));
} else if (temp_backColor.equals("淡蓝")) {
att.setBackColor(new Color(200, 200, 255));
} else if (temp_backColor.equals("淡黄")) {
att.setBackColor(new Color(255, 255, 200));
} else if (temp_backColor.equals("淡绿")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
} public static void main(String args[]) {
new ToolTip();
} /**
* 字体的属性类
*/
private class FontAttrib {
public static final int GENERAL = 0; // 常规 public static final int BOLD = 1; // 粗体 public static final int ITALIC = 2; // 斜体 public static final int BOLD_ITALIC = 3; // 粗斜体 private SimpleAttributeSet attrSet = null; // 属性集 private String text = null, name = null; // 要输入的文本和字体名称 private int style = 0, size = 0; // 样式和字号 private Color color = null, backColor = null; // 文字颜色和背景颜色 /**
* 一个空的构造(可当做换行使用)
*/
public FontAttrib() {
} /**
* 返回属性集
*
* @return
*/
public SimpleAttributeSet getAttrSet() {
attrSet = new SimpleAttributeSet();
if (name != null)
StyleConstants.setFontFamily(attrSet, name);
if (style == FontAttrib.GENERAL) {
StyleConstants.setBold(attrSet, false);
StyleConstants.setItalic(attrSet, false);
} else if (style == FontAttrib.BOLD) {
StyleConstants.setBold(attrSet, true);
StyleConstants.setItalic(attrSet, false);
} else if (style == FontAttrib.ITALIC) {
StyleConstants.setBold(attrSet, false);
StyleConstants.setItalic(attrSet, true);
} else if (style == FontAttrib.BOLD_ITALIC) {
StyleConstants.setBold(attrSet, true);
StyleConstants.setItalic(attrSet, true);
}
StyleConstants.setFontSize(attrSet, size);
if (color != null)
StyleConstants.setForeground(attrSet, color);
if (backColor != null)
StyleConstants.setBackground(attrSet, backColor);
return attrSet;
} /**
* 设置属性集
*
* @param attrSet
*/
public void setAttrSet(SimpleAttributeSet attrSet) {
this.attrSet = attrSet;
} /* 后面的注释就不写了,一看就明白 */ public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} public Color getColor() {
return color;
} public void setColor(Color color) {
this.color = color;
} public Color getBackColor() {
return backColor;
} public void setBackColor(Color backColor) {
this.backColor = backColor;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getSize() {
return size;
} public void setSize(int size) {
this.size = size;
} public int getStyle() {
return style;
} public void setStyle(int style) {
this.style = style;
}
} }
JAVA--聊天界面面板的更多相关文章
- Android—简单的仿QQ聊天界面
最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):
- Android开发学习之路--UI之简单聊天界面
学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...
- RV 多样式 MultiType 聊天界面 消息类型 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- android 仿微信聊天界面,以及语音录制功能
extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 ...
- 自定义一个ListView实现聊天界面
摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...
- Java 图形界面开发--图文并茂建立学生管理系统
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...
- Java图形界面GUI
Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...
- 重构 JAVA 聊天室 —— CS 模式的简单架构实现
前言 自从开始弄起数据挖掘之后,已经很久没写过技术类的博客了,最近学校 JAVA 课设要求实现一个聊天室,想想去年自己已经写了一个了,但是有些要求到的功能我也没实现,但看着原有的代码想了想加功能好像有 ...
- Android Studio 之 编写精美的聊天界面
•准备工作 首先制作一张 .9 格式的聊天气泡,参见我的这篇博客: 需要注意的是,制作完成后,应该将原始文件删除,否则AS会分不清楚而报错. 新建一个 Empty Activity,Java 和 XM ...
- 剖析:如何用 SwitchUI 5天写一个微信 —— 聊天界面篇
前置资源 GitHub: SwiftUI-WeChatDemo 第零章:用 SwiftUI 五天组装一个微信 - wavky - 博客园 整体结构 UI 部分代码分布如上图所示,App 的主入口类为 ...
随机推荐
- jQuery旋转插件
jQuery旋转插件,支持Internet Explorer 6.0 + .Firefox 2.0.Safari 3.Opera 9.Google Chrome,高级浏览器下使用Transform,低 ...
- poj1182 并查集
题目连接:http://poj.org/problem?id=1182 基础并查集,需要维护与根节点关系,解析见代码: /* poj 1182 并查集 思路分析:让你分析这些话里面多少假的 只需要用 ...
- noip201506 Message 信息传递
试题描述: 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 T_i 的同学.游戏开始时,每人都只 ...
- 转:十条不错的编程观点。(出处:酷 壳 – CoolShell.cn)
在Stack Overflow上有这样的一个贴子<What’s your most controversial programming opinion?>,翻译成中文就是“你认为最有争议的 ...
- Ubuntu 12.04更换显示器后显示“显示不支援”,只能进恢复模式工作
以前用的一台14寸液晶,换成17寸后,进入系统显示器上“显示不支援” .仔细观查,电脑硬盘自检能显示后,后面都是黑屏. 解决过程. 因为grub 启动菜单不能显示.盲按方向键,发现菜单里的其它项目可 ...
- PHPの页面跳转-常见方法
PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header( ...
- Html中input标签的使用
1.取消按钮按下时的虚线框 在input里添加属性值 hideFocus 或者 HideFocus=true 2.只读文本框内容 在input里添加属性值 readonly 3.防止退后清空的TEXT ...
- SpringMVC——从HelloWorld
学习SpringMVC——从HelloWorld开始 前言: 时隔十二年,中国女排最终过关斩将,用3:1的成绩证明了自己的实力,霸气夺冠,为中国赢得了一枚意义非常的金牌.这是一次全民的狂欢,一场视 ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Android隐藏标题栏
打开程序,在onCreate()方法中添加如下代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved ...