📄 cosewindow.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class CoseWindow implements ActionListener{ //聊天窗口的实现
JTextPane text = new JTextPane(); //信息显示,信息可以以不同的字体和颜色显示.
DefaultStyledDocument document = new DefaultStyledDocument(); //将被设置为JTextPane的关联文档
SimpleAttributeSet attrSet = new SimpleAttributeSet(); //将不同的属性值与此关联,如颜色,字体大小等.
JFrame frame = new JFrame("QQ式窗口");
JTextField textField = new JTextField(60);
JButton send = new JButton("发送");
JComboBox fontSize;
JComboBox fontColor;
public CoseWindow() { }
public void window(){
frame.setVisible(true);
frame.setBounds(10,10,600,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setVisible(true);
text.setStyledDocument(document); //将编辑器与一个文本文档关联。
JScrollPane scroll = new JScrollPane(text);
scroll.setVisible(true);
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
fontSize = new JComboBox(str_Size);
fontSize.setVisible(true);
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
fontColor = new JComboBox(str_Color);
fontColor.setVisible(true);
fontSize.addActionListener(this);
fontColor.addActionListener(this);
send.addActionListener(this);
JPanel panel = new JPanel();
panel.setVisible(true);
panel.setSize(200,60);
panel.add(fontSize);
panel.add(fontColor);
panel.add(textField);
panel.add(send);
frame.add(scroll,BorderLayout.CENTER);
frame.add(panel,BorderLayout.SOUTH);
}
public static void main(String args[]){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {} catch (UnsupportedLookAndFeelException ex) {} catch (InstantiationException ex) {} catch (IllegalAccessException ex) {}
new CoseWindow().window();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == fontSize){
int size = Integer.parseInt((String)fontSize.getItemAt(fontSize.getSelectedIndex()));
StyleConstants.setFontSize(attrSet,size); //使用StyleConstants将字体大小与SimpleAttributeSet关联
}else if(e.getSource() == fontColor){
int color = fontColor.getSelectedIndex();
switch(color){ //使用StyleConstants将字体颜色与SimpleAttributeSet关联
case 0 : fontColor.setBackground(Color.BLACK);StyleConstants.setForeground(attrSet,Color.BLACK); break;
case 1 : fontColor.setBackground(Color.RED);StyleConstants.setForeground(attrSet,Color.RED); break;
case 2 : fontColor.setBackground(Color.BLUE);StyleConstants.setForeground(attrSet,Color.BLUE); break;
case 3 : fontColor.setBackground(Color.YELLOW);StyleConstants.setForeground(attrSet,Color.YELLOW); break;
case 4 : fontColor.setBackground(Color.GREEN);StyleConstants.setForeground(attrSet,Color.GREEN); break;
default : fontColor.setBackground(Color.GRAY);StyleConstants.setForeground(attrSet,Color.GRAY);
}
}else if(e.getSource() == send){
//text.insertComponent(new JButton("我的新按钮")); //在光标处插入按钮组件
text.insertIcon(new ImageIcon("p1.gif")); //在光标处插入图片
try {
int position = text.getCaretPosition(); //取得当前光标的位置
if(position < 0){position = document.getLength();}
document.insertString(position,textField.getText()+"\n",attrSet); //在光标处或文档尾以attrSet属性插入数据,如果文档中有被选中的数据,则此插入将覆盖选中的数据.
textField.setText(""); //清空输入框数据.
} catch (BadLocationException ex) {}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -