swt中用什么做类似qq聊天框解决思路
swt中用什么做类似qq聊天框
swt中用什么做类似qq聊天框(中间可以输入文字和图片,并可以粘贴复制),求给思路或源码。本人邮箱:472852263@qq.com~别说lumaQQ有这个,我看过了,没找到。。最好是自己理解的。。。万分感谢~~
------解决
--------------------------------------------------------
swing实现的三个类:JTextPane StyledDocument ImageIcon
理解下面代码(可直接执行),是告诉如何显示多媒体文本和图片的
Java code
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.BorderFactory; import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class Demo extends JPanel implements ActionListener {
private String newline = \n ;
protected JLabel actionLabel;
public Demo() {
setLayout(new BorderLayout());
//Create a label to put messages during an action event.
actionLabel = new JLabel( Type text in a field and press Enter. );
actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Lay out the text controls and the labels.
JPanel textControlsPane = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
textControlsPane.setLayout(gridbag);
c.gridwidth = GridBagConstraints.REMAINDER; //last
c.anchor = GridBagConstraints.WEST;
c.weightx = 1.0;
textControlsPane.add(actionLabel, c);
textControlsPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder( Text Fields ),
BorderFactory.createEmptyBorder(5,5,5,5)));
//Create a text pane.
JTextPane textPane = createTextPane();
JScrollPane paneScrollPane = new JScrollPane(textPane);
paneScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(250, 155));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
//Put everything together.
add(paneScrollPane, BorderLayout.LINE_END);
}
public void actionPerformed(ActionEvent e) {
}
private JTextPane createTextPane() {
String[] initString =
{ This is an editable JTextPane, , //regular
another , //italic
styled , //bold
text , //small
component, , //large
which supports embedded components... + newline,//regular
+ newline, //button
...and embedded icons... + newline, //regular
, //icon
newline + JTextPane is a subclass of JEditorPane that +
uses a StyledEditorKit and StyledDocument, and provides +
cover methods for interacting with those objects.
};
String[] initStyles =
{ regular , italic , bold , small , large ,
regular , button , regular , icon ,
regular
};
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
addStylesToDocument(doc);
try {
for (int i=0; i initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println( Couldn't insert initial text into text pane. );
}
return textPane;
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle( regular , def);
StyleConstants.setFontFamily(def, SansSerif );
Style s = doc.addStyle( italic , regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle( bold , regular);
StyleConstants.setBold(s, true);
s = doc.addStyle( small , regular);
StyleConstants.setFontSize(s, 10);
s = doc.addStyle( large , regular);
StyleConstants.setFontSize(s, 16);
s = doc.addStyle( icon , regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon pigIcon = createImageIcon( images/Pig.gif ,
a cute pig );
if (pigIcon != null) {
StyleConstants.setIcon(s, pigIcon);
}
s = doc.addStyle( button , regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon soundIcon = createImageIcon( images/sound.gif ,
sound icon );
JButton button = new JButton();
if (soundIcon != null) {
button.setIcon(soundIcon);
} else {
button.setText( BEEP );
}
button.setCursor(Cursor.getDefaultCursor());
button.setMargin(new Insets(0,0,0,0));
button.addActionListener(this);
StyleConstants.setComponent(s, button);
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = Demo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println( Couldn't find file: + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame( TextSamplerDemo );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Demo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put( swing.boldMetal , Boolean.FALSE);
createAndShowGUI();
}
});
}
}