
想插入图片需要使用JTextPane。
1
2
3
4
5
6
7
JTextPane textPane = new JTextPane()
StyledDocument doc = (StyledDocument) textPane.getDocument()
Style style = doc.addStyle("StyleName", null)
StyleConstants.setIcon(style, new ImageIcon("imagefile"))
doc.insertString(doc.getLength(), "ignored text", style)
调用append方法可在原内容后添加,不会覆盖原内容import javax.swing.JFrame
import javax.swing.JTextArea
public class Example{
JFrame jf=new JFrame("小例子")
JTextArea t1=new JTextArea(5,14)
public Example(){
jf.add(t1)
jf.setVisible(true)
jf.pack()
t1.append("字符串一")
t1.append("字符串二")
}
public static void main(String args[]){
new Example()
}
}
我刚做了一个 你试试把 不行就改改server端
package socket
import javax.swing.*
import java.awt.*
import java.io.IOException
import java.net.*
import java.util.Vector
import java.awt.Dimension
import java.awt.Rectangle
import java.awt.GridBagConstraints
import java.awt.Point
import javax.swing.JButton
import javax.swing.JLabel
import java.awt.Color
import javax.swing.ImageIcon
public class Server extends JFrame implements Runnable {
private static final long serialVersionUID = 1L
private JPanel jContentPane = null
private JPanel diaPanel = null
private JPanel outPanel = null
private JPanel btnPanel = null
private JButton closeButton = null
private JButton sendButton = null
private JTextArea outTextArea = null
private List dialist = null
public Server() {
super()
initialize()
Listen()
}
private void initialize() {
this.setSize(355, 267)
this.setContentPane(getJContentPane())
this.setTitle("服务器")
this.setVisible(true)
this.setLocationRelativeTo(null)
this.setResizable(false)
}
private JPanel getJContentPane() {
if (jContentPane == null) {
img3Label = new JLabel()
img3Label.setBounds(new Rectangle(269, 94, 85, 95))
img3Label.setIcon(new ImageIcon(getClass().getResource("/socket/7.jpg")))
img3Label.setText("")
img2Label = new JLabel()
img2Label.setBounds(new Rectangle(267, 1, 86, 94))
img2Label.setIcon(new ImageIcon(getClass().getResource("/socket/6.jpg")))
img2Label.setText("")
imgLabel = new JLabel()
imgLabel.setBounds(new Rectangle(1, 95, 269, 23))
imgLabel.setBackground(new Color(255, 153, 153))
imgLabel.setIcon(new ImageIcon(getClass().getResource("/socket/24.jpg")))
imgLabel.setText("")
jContentPane = new JPanel()
jContentPane.setLayout(null)
jContentPane.add(getDiaPanel(), null)
jContentPane.add(getOutPanel(), null)
jContentPane.add(getBtnPanel(), null)
jContentPane.add(imgLabel, null)
jContentPane.add(img2Label, null)
jContentPane.add(img3Label, null)
}
return jContentPane
}
private JPanel getDiaPanel() {
if (diaPanel == null) {
GridBagConstraints gridBagConstraints11 = new GridBagConstraints()
gridBagConstraints11.fill = GridBagConstraints.BOTH
gridBagConstraints11.gridy = 0
gridBagConstraints11.weightx = 1.0
gridBagConstraints11.weighty = 1.0
gridBagConstraints11.gridheight = 1
gridBagConstraints11.gridx = 0
diaPanel = new JPanel()
diaPanel.setLayout(new GridBagLayout())
diaPanel.setLocation(new Point(0, 0))
diaPanel.setSize(new Dimension(271, 94))
diaPanel.add(getDialist(), gridBagConstraints11)
}
return diaPanel
}
private JPanel getOutPanel() {
if (outPanel == null) {
GridBagConstraints gridBagConstraints1 = new GridBagConstraints()
gridBagConstraints1.fill = GridBagConstraints.BOTH
gridBagConstraints1.weighty = 1.0
gridBagConstraints1.weightx = 1.0
outPanel = new JPanel()
outPanel.setLayout(new GridBagLayout())
outPanel.setLocation(new Point(1, 120))
outPanel.setSize(new Dimension(269, 68))
outPanel.add(getOutTextArea(), gridBagConstraints1)
}
return outPanel
}
private JPanel getBtnPanel() {
if (btnPanel == null) {
btnPanel = new JPanel()
btnPanel.setLayout(new GridBagLayout())
btnPanel.setBounds(new Rectangle(-2, 188, 356, 50))
btnPanel.add(getSendButton(), new GridBagConstraints())
btnPanel.add(getCloseButton(), new GridBagConstraints())
}
return btnPanel
}
private JTextArea getOutTextArea() {
if (outTextArea == null) {
outTextArea = new JTextArea()
}
return outTextArea
}
private List getDialist() {
if (dialist == null) {
dialist = new List()
}
return dialist
}
private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton()
closeButton.setText("关闭")
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(EXIT_ON_CLOSE)
}
})
}
return closeButton
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton()
sendButton.setText("发送")
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String s = outTextArea.getText()
processmsg(s)
outTextArea.setText(null)
for (int i = 0i <clients.size()i++) {
Connection c = (Connection) clients.get(i)
try {
c.sendmsg(s)
} catch (IOException e1) {}
}
}
})
}
return sendButton
}
public void processmsg(String str) {
dialist.add(str)
}
public static void main(String[] args) {
Server sv = new Server()
}
public final static int DEFAULT_PORT = 8808
ServerSocket listen_socket
Socket client_socket
Vector clients
private JLabel imgLabel = null
private JLabel img2Label = null
private JLabel img3Label = null
public void Listen() {
try {
listen_socket = new ServerSocket(DEFAULT_PORT)
} catch (IOException e) {
e.printStackTrace()
}
processmsg("server:listening on port:" + DEFAULT_PORT)
clients = new Vector()
Thread thread = new Thread()
thread.start()
Socket client_socket
try {
while(true){
client_socket = listen_socket.accept()
Connection c = new Connection(client_socket, this)
clients.add(c)
processmsg("有客户端请求连接,客户端ip地址:"
+ client_socket.getInetAddress().getHostAddress())
processmsg("one client connection!")
}
} catch (IOException e) {
e.printStackTrace()
}
}
public void run() {
//Socket client_socket
// try {
// while(true){
//client_socket = listen_socket.accept()
//processmsg("one client connection!")
//Connection c = new Connection(client_socket, this)
//clients.add(c)
// System.out.println("有客户端请求连接,客户端ip地址:"
// + client_socket.getInetAddress().getHostAddress()
// + ",远程端口:" + client_socket.getPort() + ",本地端口:"
// + client_socket.getLocalPort())
// processmsg("有客户端请求连接,客户端ip地址:"+client_socket.getInetAddress().getHostAddress())
// processmsg("one client connection!")
// }
// } catch (IOException e) {
// e.printStackTrace()
// }
}
} // @jve:decl-index=0:visual-constraint="10,10"
client 端
package socket
import java.awt.*
import java.awt.event.*
import java.io.*
import java.net.Socket
import javax.swing.*
import java.awt.Rectangle
import javax.swing.JButton
import javax.swing.ImageIcon
import java.awt.Dimension
import java.awt.Point
import java.awt.Color
import javax.swing.JLabel
public class Client extends JFrame implements Runnable {
private static final long serialVersionUID = 1L
private JPanel jContentPane = null
private JPanel btnPanel = null
private JButton sendButton = null
private JButton closeButton = null
private JButton connButton = null
private JTextArea outTextArea = null
private List dialist = null
Socket sock
Thread thread
BufferedReader in
PrintWriter out
public final static int DEFAULT_PORT = 8808
boolean bConnected
private JLabel imgLabel = null
private JLabel img2Label = null
private JLabel img3Label = null
public Client() {
super()
initialize()
}
private void initialize() {
this.setSize(355, 267)
this.setContentPane(getJContentPane())
this.setTitle("客户端")
this.setVisible(true)
this.setLocationRelativeTo(null)
this.setResizable(false)
}
private JPanel getBtnPanel() {
if (btnPanel == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints()
gridBagConstraints3.gridx = 3
gridBagConstraints3.gridy = 1
GridBagConstraints gridBagConstraints2 = new GridBagConstraints()
gridBagConstraints2.gridx = 1
gridBagConstraints2.gridy = 1
GridBagConstraints gridBagConstraints1 = new GridBagConstraints()
gridBagConstraints1.gridx = 0
gridBagConstraints1.gridy = 1
btnPanel = new JPanel()
btnPanel.setLayout(new GridBagLayout())
btnPanel.setBounds(new Rectangle(1, 187, 347, 47))
btnPanel.add(getSendButton(), gridBagConstraints1)
btnPanel.add(getCloseButton(), gridBagConstraints2)
btnPanel.add(getConnButton(), gridBagConstraints3)
}
return btnPanel
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton()
sendButton.setText("发送")
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String msg = outTextArea.getText()
if (msg!=null) { //此句无效???
processmsg(msg)
outTextArea.setText(null)
try {
sendmsg(msg)
} catch (IOException e2) {
processmsg(e2.toString())
}
}
}
})
}
return sendButton
}
private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton()
closeButton.setText("关闭")
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(EXIT_ON_CLOSE)
}
})
closeButton.setText("关闭")
}
return closeButton
}
private JButton getConnButton() {
if (connButton == null) {
connButton = new JButton()
connButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startConnect()
}
})
connButton.setText("链接")
}
return connButton
}
private JTextArea getOutTextArea() {
outTextArea = new JTextArea()
outTextArea.setLocation(new Point(0, 119))
outTextArea.setSize(new Dimension(269, 68))
return outTextArea
}
public void processmsg(String str) {
this.dialist.add(str)
}
private List getDialist() {
dialist = new List()
dialist.setLocation(new Point(-1, 1))
dialist.setSize(new Dimension(271, 94))
return dialist
}
public void run() {
while (true) {
try {
String msg = receivemsg()
Thread.sleep(100L)
if (msg != null) {
processmsg(msg)
}
} catch (IOException e) {
e.printStackTrace()
}catch (InterruptedException ei) {}
}
}
public void sendmsg(String msg)throws IOException {
out.println(msg)
out.flush()
}
public String receivemsg() throws IOException{
String msg = new String()
try {
msg = in.readLine()
} catch (IOException e) {
e.printStackTrace()
}
return msg
}
public void startConnect() {
bConnected = false
try {
sock = new Socket("127.0.0.1", DEFAULT_PORT)
bConnected = true
processmsg("connect ok!")
in =new BufferedReader(new InputStreamReader(sock.getInputStream()))
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())))
} catch (IOException e) {
e.printStackTrace()
processmsg("connection failed")
}
if(thread==null){
thread=new Thread(this)
thread.start()
}
}
private JPanel getJContentPane() {
if (jContentPane == null) {
img3Label = new JLabel()
img3Label.setText("")
img3Label.setSize(new Dimension(269, 23))
img3Label.setLocation(new Point(1, 94))
img2Label = new JLabel()
img2Label.setBounds(new Rectangle(270, 95, 81, 89))
img2Label.setIcon(new ImageIcon(getClass().getResource("/socket/10.jpg")))
img2Label.setText("")
imgLabel = new JLabel()
imgLabel.setBounds(new Rectangle(270, 0, 78, 94))
imgLabel.setIcon(new ImageIcon(getClass().getResource("/socket/3.jpg")))
imgLabel.setText("")
jContentPane = new JPanel()
jContentPane.setLayout(null)
jContentPane.add(getBtnPanel(), null)
jContentPane.add(getOutTextArea(), null)
jContentPane.add(getDialist(), null)
jContentPane.add(imgLabel, null)
jContentPane.add(img2Label, null)
jContentPane.add(img3Label, null)
}
return jContentPane
}
public static void main(String[] args) {
Client cl = new Client()
}
}
connection类
package socket
import java.io.*
import java.net.Socket
class Connection extends Thread{
protected Socket client
protected BufferedReader in
Server server
protected PrintWriter out
public Connection(Socket client_socket,Server server_frame){
client=client_socket
server=server_frame
try {
in=new BufferedReader(new InputStreamReader(client.getInputStream()))
out=new PrintWriter(client.getOutputStream())
} catch (IOException e) {
try {
client.close()
} catch (IOException e1) {
e1.printStackTrace()
return
}
}
this.start()
}
public void run(){
String line
// StringBuffer revline
// int len
try {
for(){
// read in a line
line=receivemsg()
server.processmsg(line)
if(line==null)break
}
} catch (IOException e){}
finally {try {client.close()}
catch (IOException e2) {}}
}
public void sendmsg(String msg) throws IOException{
out.println(msg)
out.flush()
}
public String receivemsg() throws IOException{
String msg=new String()
try {
msg=in.readLine()
} catch (IOException e) {
e.printStackTrace()
}
return msg
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)