package hao;
我們提供的服務有:做網(wǎng)站、網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、拜城ssl等。為上千多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的拜城網(wǎng)站制作公司
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;
JButton music;
public ChatPanel() {
setLayout(new BorderLayout());
text = new JTextPane();
text.setEditable(false);
doc = text.getStyledDocument();//跟蹤文本和圖片寫到該區(qū)域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面對JComboBox的巧用***********************************************************************
String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常規(guī)", "斜體", "粗體", "粗斜體" };
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);
fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//創(chuàng)建一個可以容納多個Box組件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字體~~"), b2 = new JLabel("樣式~~"),b3 = new JLabel("字號~~"),b4 = new JLabel("顏色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);
textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));
inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //設置文本區(qū)的換行策略。88888*********************************
send = new JButton("發(fā)送");
record=new JButton("顯示記錄");
saveRecord=new JButton("儲存記錄");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設置按鈕間距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置Box的邊框線********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);
inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置輸入窗口邊框線*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設置輸入窗口邊框線*******************
JPanel audioPanel = new JPanel();//最上面的邊框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));
music = new JButton("想聽就聽");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//設置輸入窗口邊框線*******************
audioPanel.add(music);
add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}
void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}
public void insert(MessageStyle attrib) {
try {
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//寫完后接著換行************
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public MessageStyle getMessageStyle(String line) {
MessageStyle att = new MessageStyle();
att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常規(guī)")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗體")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜體")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜體")) {
att.setStyle(MessageStyle.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;
}
}
視圖里面改一下就好了,反正就那么幾個選項
你都點一遍順便還可以熟悉一下其他的功能,點了看看效果,不要怕麻煩,初學習階段最好都嘗試一遍,這樣以后遇到問題的時候就能快速解決,特別是工作當中,裨益頗大
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.IOException;
import java.io.*;
public class chat {
public static void main(String[] args) {
new UDPMessage();
}
}
class UDPMessage extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea recordText = new JTextArea();
private JTextArea sendText = new JTextArea();
private DatagramSocket ds;
private JButton btnSend = new JButton("發(fā)送");
private JButton btnExit = new JButton("關閉");
private JLabel otherTips = new JLabel("對方IP");
private JTextField otherIp = new JTextField();
private JLabel myTips = new JLabel("我的IP");
private JTextArea myIp = new JTextArea();
InputStreamReader in;
//創(chuàng)建一個文件輸入流
OutputStreamWriter out;
//創(chuàng)建一個文件輸出流
Container p;
JScrollPane jspRecord;
JMenuBar mb = new JMenuBar();
JMenu[] m = {new JMenu("文件"),
new JMenu("好友"),
new JMenu("背景")};
JMenuItem[][] mi = {{new JMenuItem("傳送文件")},
{new JMenuItem("Regina"),new JMenuItem("Taotao"),new JMenuItem("LiShuo")},
{new JMenuItem("默
認"),new JMenuItem("淺灰色"),new JMenuItem("淡紫色"),new JMenuItem("黃綠色")}};
public UDPMessage() {
super("HiChat");
p = getContentPane();
setSize(550, 475);
centerOnScreen();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0; im.length; i++) {
mb.add(m[i]);
for(int j=0; jmi[i].length; j++) {
m[i].add(mi[i][j]);
mi[i][j].addActionListener(this);
}
}
setJMenuBar(mb);
p.setBackground(new Color(218, 112, 214));
p.setLayout(null);
recordText.setEditable(false);
recordText.setLineWrap(true);
jspRecord = new JScrollPane(recordText);
jspRecord.setBounds(2, 5, 350, 270);
p.add(jspRecord);
sendText.setLineWrap(true);
JScrollPane jspSend = new JScrollPane(sendText);
jspSend.setBounds(2, 285, 350,100);
p.add(jspSend);
btnExit.setBounds(184,390,80,25);
btnExit.setMargin(new Insets(0, 0, 0, 0));
btnExit.addActionListener(this);
p.add(btnExit);
btnSend.setBounds(271,390,80,25);
btnSend.setMargin(new Insets(0, 0, 0, 0));
btnSend.addActionListener(this);
p.add(btnSend);
GridLayout gl = new GridLayout(2, 0);
gl.setVgap(15);
JPanel infoPane = new JPanel(gl);
infoPane.setOpa
setTextColor(0xFF0000FF);
//0xFF0000FF是int類型的數(shù)據(jù),分組一下0x|FF|0000FF,0x是代表顏色整 數(shù)的標記,ff是表示透明度,0000FF表示顏色,注意:這里0xFF0000FF必須是8個的顏色表示,不接受0000FF這種6個的顏色表示。
setTextColor(Color.rgb(255, 255, 255));
setTextColor(Color.parseColor("#FFFFFF"));
//還有就是使用資源文件進行設置
setTextColor(this.getResources().getColor(R.color.blue));
//通過獲得資源文件進行設置。根據(jù)不同的情況R.color.blue也可以是R.string.blue或者
//另外還可以使用系統(tǒng)自帶的顏色類
setTextColor(android.graphics.Color.BLUE);
名稱欄目:淺灰色java代碼 淺灰色的代碼是什么
轉載來于:http://m.kartarina.com/article10/dodeodo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供、服務器托管、面包屑導航、云服務器、網(wǎng)站導航、外貿建站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)