java 设置字体的颜色,java设置字体颜色

本文档展示了如何使用Java创建一个简单的GUI应用程序,包括建立主框架、编辑功能(发送和接收文本)、颜色选择以及使用Socket进行实时数据传输。主要关注编辑器组件的交互和网络通信的实现。

package com.yidu.demo;

import java.awt.Color;

import java.awt.Font;

import java.awt.Image;

import java.awt.MenuItem;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Enumeration;

import javax.swing.ImageIcon;

import javax.swing.JColorChooser;

import javax.swing.JEditorPane;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JTextPane;

import javax.swing.KeyStroke;

import javax.swing.text.AttributeSet;

import javax.swing.text.BadLocationException;

import javax.swing.text.Document;

import javax.swing.text.Element;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;

import javax.swing.text.StyleConstants.ColorConstants;

import javax.swing.text.StyleConstants.FontConstants;

import sun.font.FontFamily;

public class MainFrame extends JFrame {

JTextPane editorPane=new JTextPane();

Socket socket;

public MainFrame(){

JMenuBar bar=new JMenuBar();

this.setJMenuBar(bar);

JMenu menuFile=new JMenu("编辑");

bar.add(menuFile);

JMenuItem itemSend=new JMenuItem("发送");

menuFile.add(itemSend);

try {

socket=new Socket("127.0.0.1",1000);

} catch (UnknownHostException e2) {

// TODO Auto-generated catch block

e2.printStackTrace();

} catch (IOException e2) {

// TODO Auto-generated catch block

e2.printStackTrace();

}

itemSend.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

try {

OutputStream os= socket.getOutputStream();

ObjectOutputStream objectOutputStream=new ObjectOutputStream(os);

objectOutputStream.writeObject(editorPane);

objectOutputStream.flush();

} catch (UnknownHostException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

www.2cto.com

});

JMenuItem itemGet=new JMenuItem("得到内容");

menuFile.add(itemGet);

itemGet.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

Document document= editorPane.getDocument();

editorPane.insertIcon(new ImageIcon("MessageReading.gif"));

}

});

JMenuItem itemColor=new JMenuItem("颜色");

itemColor.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

JColorChooser colorChooser=new JColorChooser();

Color color= colorChooser.showDialog(null, "字体颜色", Color.BLACK);

Document document= editorPane.getDocument();

try {

StyleContext   sc   =   StyleContext.getDefaultStyleContext();

AttributeSet   aset   =   sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, color);

Font font=new Font("隶书",Font.BOLD,30);

aset=sc.addAttribute(aset, StyleConstants.Family, font.getFamily());

aset=sc.addAttribute(aset, StyleConstants.FontSize, 30);

int start= editorPane.getSelectionStart();

int end=editorPane.getSelectionEnd();

String str= document.getText(start,end-start);

document.remove(start, end-start);

document.insertString(start, str, aset);

} catch (BadLocationException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

menuFile.add(itemColor);

itemColor.setMnemonic('C');

//设置快捷键

itemColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.ALT_MASK));

this.add(editorPane);

editorPane.setText("测试");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

/**

* @param args

*/

public static void main(String[] args) {

MainFrame mainFrame=new MainFrame();

mainFrame.setSize(400, 300);

mainFrame.setVisible(true);

}

}

摘自 zyqyz520的专栏

http://www.dengb.com/Javabc/544136.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/544136.htmlTechArticlepackage com.yidu.demo; import java.awt.Color; import java.awt.Font; import java.awt.Image; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.Action...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值