📄 userinterface.java.bak
字号:
/*
*this class is mainly for create the user chatting GUI
*it can get the user input
*then 1) copy it to Display pane
* 2) send the user input out
*
*
* last edit by Kong Chong Shun, John
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //for layout managers and more
import java.awt.event.*; //for action events
import java.net.URL;
import java.io.IOException;
public class UserInterface extends JPanel implements ActionListener, Runnable
{
private String newline = "\n";
private JButton sendButton = new JButton("Send");
private static JTextPane inputPane, displayPane;
private String defaultFont = "SansSerif";
private String defaultIconString = ":$:";
public static StyledDocument inputdoc, displaydoc;
public static ObjectReceiver r = null;
public static ObjectSender sender = null;
public static JFrame frame = null;
//contrustor
public UserInterface()
{
setLayout(new BorderLayout());
//Create a text pane (for display)
displayPane = new JTextPane();
JScrollPane displayScrollPane = new JScrollPane(displayPane);
//enable the scroll bar
displayScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Set Prefer size
displayScrollPane.setPreferredSize(new Dimension(400, 300));
//set minimun size
displayScrollPane.setMinimumSize(new Dimension(100, 100));
//Create a text pane (for User input)
inputPane = new JTextPane();
JScrollPane inputScrollPane = new JScrollPane(inputPane);
//enable the scroll bar
inputScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Set Prefer size
inputScrollPane.setPreferredSize(new Dimension(400, 300));
//set minimun size
inputScrollPane.setMinimumSize(new Dimension(100, 100));
//create a panel for user input
JPanel mainInputPane = new JPanel();
mainInputPane.setLayout(new BorderLayout());
//add the send button to the listener
sendButton.addActionListener(this);
mainInputPane.add(sendButton, BorderLayout.EAST);
mainInputPane.add(inputScrollPane);
//Put two textpane together
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, displayScrollPane, mainInputPane);
splitPane.setOneTouchExpandable(true);
splitPane.setResizeWeight(0.5);
add(splitPane);
//add style to Pane
inputdoc = inputPane.getStyledDocument();
addStylesToDocument(inputdoc);
displaydoc = displayPane.getStyledDocument();
addStylesToDocument(displaydoc);
//displayPane.setText("NONONO");
}
//send out fuuction not finish
public void actionPerformed(ActionEvent event)
{
//copy the message from UserInput Pane to display Pane
//send the message out
//clear inputPane
if ((JButton)event.getSource() == sendButton)
{
String input = this.getUserInput();
if (input != null)
{ insertTextToDisplay(input);
System.out.println("insert");
//send it out
System.out.println("Message from UI :" + input);
sender.sendObject(input);
//clear inputPane
clearinputPane();
}
}
else
System.out.println("Listener is noted");
}
public void clearinputPane()
{
inputPane.setText("");
}
//return the String that user typed in the UserInput Pane
public String getUserInput()
{
String temp = null;
try
{
temp = inputPane.getText(0,inputdoc.getLength() );
System.out.println("Text got :");
System.out.println(temp);
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert initial text into text pane.");
}
return temp;
}
//insert the Text to the Display Pane
public void insertTextToDisplay(String userInput)
{
try
{
displaydoc.insertString(displaydoc.getLength(),"GAGAGA" + newline + newline, displaydoc.getStyle("regular"));
}
catch(Exception e)
{}
//create style doc
//add style to doc
//check the string if it contain graphic
//if yes, divide it into different part
//& insert style & string array to the doc
System.out.println("UI start to insert text : " + userInput);
int i = 0;
int firstIndex = -1;
while (userInput.indexOf(":$:", i) != -1)
{
System.out.println("Loop");
firstIndex = userInput.indexOf(":$:", i);
i = firstIndex + 3; //becuase the string is ":$"
if (userInput.indexOf(":$:", i) != -1)
{
System.out.println(userInput.substring(0, firstIndex ));
System.out.println(userInput.substring(i, userInput.indexOf(":$:", i)));
try
{
//inesrt text
displaydoc.insertString(displaydoc.getLength(),userInput.substring(0, firstIndex ), displaydoc.getStyle("regular"));
//insert Icon
displaydoc.insertString(displaydoc.getLength()," ", displaydoc.getStyle(userInput.substring(i, userInput.indexOf(":$:", i))));
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert initial text into text pane.");
}
userInput = userInput.substring(userInput.indexOf(":$:", i) + 3);
System.out.println(userInput);
}
}
System.out.println("UI insert text no icon string or remaining no icon string \n" + userInput);
//no icon string or remaining no icon string
if (userInput != null)
{
try
{
//inesrt text
System.out.println("displaydoc.getLength() = " + displaydoc.getLength());
displaydoc.insertString(displaydoc.getLength(),userInput + newline + newline, displaydoc.getStyle("regular"));
System.out.println("displaydoc.getLength() = " + displaydoc.getLength());
System.out.println("afafafafa");
//displayPane.append(userInput);
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert initial text into text pane.");
}
}
//displayPane.requestFocus();
//displayPane.revalidate();
//displayPane.setText(userInput);
//displayPane.setText("NONONO");
//displayPane.repaint();
//displayPane.validate();
//validate();
//updateUI() ;
//repaint();
}
//add default Style to Document
protected void addStylesToDocument(StyledDocument doc)
{
//add default style to the doc
//Initialize styles
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, defaultFont);
Style s = doc.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle("bold", regular);
StyleConstants.setBold(s, true);
/*
:happy:
:sad:
:winky:
:silly:
:laughing:
:angry:
:censored:
:bitter:
:shocked:
:surprised:
:cool:
:blush:
:shy:
:kiss:
:smooch:
:heart:
*/
//:happy:
s = doc.addStyle("happy", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon happyIcon = createIamgeIcon("face/happy.gif", defaultIconString + "happy" + defaultIconString);
if (happyIcon != null)
{
StyleConstants.setIcon(s, happyIcon);
}
//:sad:
s = doc.addStyle("sad", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon sadIcon = createIamgeIcon("face/sad.gif", defaultIconString + "sad" + defaultIconString);
if (sadIcon != null)
{
StyleConstants.setIcon(s, sadIcon);
}
//:winky:
s = doc.addStyle("winky", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon winkyIcon = createIamgeIcon("face/sad.gif", defaultIconString + "winky" + defaultIconString);
if (winkyIcon != null)
{
StyleConstants.setIcon(s, winkyIcon);
}
//:silly:
s = doc.addStyle("silly", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon sillyIcon = createIamgeIcon("face/silly.gif", defaultIconString + "silly" + defaultIconString);
if (sillyIcon != null)
{
StyleConstants.setIcon(s, sillyIcon);
}
//:laughing:
s = doc.addStyle("laughing", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon laughingIcon = createIamgeIcon("face/laughing.gif", defaultIconString + "laughing" + defaultIconString);
if (laughingIcon != null)
{
StyleConstants.setIcon(s, laughingIcon);
}
//:angry:
s = doc.addStyle("angry", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon angryIcon = createIamgeIcon("face/angry.gif", defaultIconString + "angry" + defaultIconString);
if (angryIcon != null)
{
StyleConstants.setIcon(s, angryIcon);
}
//:censored:
s = doc.addStyle("censored", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon censoredIcon = createIamgeIcon("face/censored.gif", defaultIconString + "censored" + defaultIconString);
if (censoredIcon != null)
{
StyleConstants.setIcon(s, censoredIcon);
}
//:bitter:
s = doc.addStyle("bitter", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon bitterIcon = createIamgeIcon("face/bitter.gif", defaultIconString + "bitter" + defaultIconString);
if (bitterIcon != null)
{
StyleConstants.setIcon(s, bitterIcon);
}
//:shocked:
s = doc.addStyle("shocked", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon shockedIcon = createIamgeIcon("face/shocked.gif", defaultIconString + "shocked" + defaultIconString);
if (shockedIcon != null)
{
StyleConstants.setIcon(s, shockedIcon);
}
//:surprised:
s = doc.addStyle("surprised", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon surprisedIcon = createIamgeIcon("face/surprised.gif", defaultIconString + "surprised" + defaultIconString);
if (surprisedIcon != null)
{
StyleConstants.setIcon(s, surprisedIcon);
}
//:cool:
s = doc.addStyle("cool", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon coolIcon = createIamgeIcon("face/cool.gif", defaultIconString + "cool" + defaultIconString);
if (coolIcon != null)
{
StyleConstants.setIcon(s, coolIcon);
}
//:blush:
s = doc.addStyle("blush", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon blushIcon = createIamgeIcon("face/blush.gif", defaultIconString + "blush" + defaultIconString);
if (blushIcon != null)
{
StyleConstants.setIcon(s, blushIcon);
}
//:shy:
s = doc.addStyle("shy", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon shyIcon = createIamgeIcon("face/shy.gif", defaultIconString + "shy" + defaultIconString);
if (shyIcon != null)
{
StyleConstants.setIcon(s, shyIcon);
}
//:kiss:
s = doc.addStyle("kiss", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon kissIcon = createIamgeIcon("face/kiss.gif", defaultIconString + "kiss" + defaultIconString);
if (kissIcon != null)
{
StyleConstants.setIcon(s, kissIcon);
}
//:smooch:
s = doc.addStyle("smooch", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon smoochIcon = createIamgeIcon("face/smooch.gif", defaultIconString + "smooch" + defaultIconString);
if (smoochIcon != null)
{
StyleConstants.setIcon(s, smoochIcon);
}
//:heart:
s = doc.addStyle("heart", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon heartIcon = createIamgeIcon("face/heart.gif", defaultIconString + "heart" + defaultIconString);
if (heartIcon != null)
{
StyleConstants.setIcon(s, heartIcon);
}
}
//return IamgeIcon by calling with path
protected static ImageIcon createIamgeIcon(String path, String description)
{
//can the full path of from the class file
java.net.URL imgURL = UserInterface.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
//create & show the GUI
public static void createAndShowGUI()
{
//nice window decorations
JFrame.setDefaultLookAndFeelDecorated(true);
//create and set exit window
frame = new JFrame("Test TextPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and setting the content pane
JComponent newContentPane = new UserInterface();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window
frame.pack();
frame.setVisible(true);
}
public void run()
{
createAndShowGUI();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -