1017代码比较

来源:互联网 发布:mac 制作启动u盘 编辑:程序博客网 时间:2024/06/03 17:32



package org.hjm.component;




import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import java.net.URL;


import javax.swing.ImageIcon;


import com.android.ninepatch.NinePatch;


public class NinePatchImageIcon extends ImageIcon {

private static final long serialVersionUID = -5004430700627593660L;

private NinePatch mNinePatch;

public NinePatchImageIcon(URL urlRes) {
try {
mNinePatch = NinePatch.load(urlRes, true);
} catch (IOException e) {
e.printStackTrace();
return;
}
}

@Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {

int iCompWidth = c.getWidth();
int iCompHeigth = c.getHeight();
mNinePatch.draw((Graphics2D) g, 0, 0,iCompWidth, iCompHeigth);
}


}








package org.hjm.component;
import java.awt.Graphics;
import java.awt.Insets;


import javax.swing.Icon;
import javax.swing.JLabel;




/**
 * <code>RLabel</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class RLabel extends JLabel{

private static final long serialVersionUID = 1L;

private Icon mBgIcon;

public void setBackgroundIcon(Icon icon) {
mBgIcon = icon;
}

@Override
protected void paintComponent(Graphics g) {
if (mBgIcon != null) {
mBgIcon.paintIcon(this, g, 0, 0);
}
super.paintComponent(g);
}

@Override
public Insets getInsets() {
return new Insets(10,10,10,10);
}

}




package org.hjm.component;
import java.awt.Graphics;
import java.awt.Insets;


import javax.swing.Icon;
import javax.swing.JTextArea;




/**
 * <code>RTextPane</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class RTextPane extends JTextArea{

private static final long serialVersionUID = 1L;

private Icon mBgIcon;

public RTextPane() {

setOpaque(false);
setLineWrap(true);
setWrapStyleWord(true);

}

public void setBackgroundIcon(Icon icon) {
mBgIcon = icon;
}

@Override
protected void paintComponent(Graphics g) {
if (mBgIcon != null) {
mBgIcon.paintIcon(this, g, 0, 0);
}
super.paintComponent(g);
}

@Override
public Insets getInsets() {
return new Insets(10,10,10,10);
}

}




package org.hjm.entity;
/**
 * <code>IMMessage</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class IMMessage {
private String sender;
private String time;
private String msg;

public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}

}



package org.hjm.main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;


import net.miginfocom.swing.MigLayout;


import org.hjm.entity.IMMessage;
import org.hjm.model.BubbleModel;
import org.hjm.renderer.BubbleRenderer;


/**
 * <code>BubbleDemo</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class BubbleDemo extends JFrame {

private static final long serialVersionUID = 1L;

BubbleModel mModel = new BubbleModel();

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BubbleDemo frame = new BubbleDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public BubbleDemo() {
initGUI();
}

public void initGUI() {
setTitle("泡泡聊天");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 550);
setLocationRelativeTo(null);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);

JTable table = new JTable();
table.setTableHeader(null);
table.setModel(mModel);
table.getColumnModel().getColumn(0).setPreferredWidth(260);
table.getColumnModel().getColumn(0).setCellRenderer(new BubbleRenderer());
scrollPane.setViewportView(table);
table.setBackground(Color.white);
table.setOpaque(true);
table.setShowHorizontalLines(false);
scrollPane.getViewport().setBackground(Color.WHITE);

JButton btnSend = new JButton("发送");
final JComboBox cmb = new JComboBox(new String[]{"闷骚男", "萌妹子"});
final JTextPane txtPnl = new JTextPane();
JPanel pnlSend = new JPanel(new MigLayout("ins 4"));
pnlSend.add(cmb, "hmax pref");
pnlSend.add(new JScrollPane(txtPnl), "hmin 50px,growx,pushx");
pnlSend.add(btnSend, "growy,pushy");
contentPane.add(pnlSend, BorderLayout.SOUTH);

btnSend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sMsg = txtPnl.getText().trim();
if ("".equals(sMsg)) {
System.err.println("Require not blank.");
return ;
}
String sSend = (String)cmb.getSelectedItem();
String sTime =  getTime();
IMMessage imMsg = new IMMessage();
imMsg.setSender(sSend);
imMsg.setTime(sTime);
imMsg.setMsg(sMsg);
mModel.addRow(imMsg);
//clear
txtPnl.setText("");
}
});

}

static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return sdf.format(new Date());
}

}



package org.hjm.model;


import java.util.ArrayList;
import java.util.List;


import javax.swing.table.AbstractTableModel;


import org.hjm.entity.IMMessage;
/**
 * <code>BubbleModel</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class BubbleModel extends AbstractTableModel{

private static final long serialVersionUID = 1L;

private List<IMMessage> mLstData;

public BubbleModel() {
mLstData = new ArrayList<IMMessage>();
}

public void addRow(IMMessage imMsg) {
mLstData.add(imMsg);
int iLen = mLstData.size();
this.fireTableRowsInserted(iLen - 1, iLen - 1);
}

@Override
public int getRowCount() {
return mLstData.size();
}


@Override
public int getColumnCount() {
return 1;
}


@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return mLstData.get(rowIndex);
}


}





package org.hjm.renderer;


import java.awt.Color;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


import net.miginfocom.swing.MigLayout;


import org.hjm.component.NinePatchImageIcon;
import org.hjm.component.RLabel;
import org.hjm.component.RTextPane;
import org.hjm.entity.IMMessage;
/**
 * <code>DemoRenderer</code> 
 * 
 * @author Jimmy
 * @since v1.0.0 (Oct 15, 2013)
 */
public class BubbleRenderer implements TableCellRenderer {

LeftViewHolder mLeftHolder = new LeftViewHolder();
RightViewHolder mRightHolder = new RightViewHolder();
Map<String, Integer> mCacheCount = new HashMap<String, Integer>();
int iCount = 0;

public BubbleRenderer() {
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Holder holder = null;
IMMessage imMsg = (IMMessage)value;
String sSender = imMsg.getSender();
String sMsg = imMsg.getMsg();
String sTime = imMsg.getTime();

Integer iDisplaySide = mCacheCount.get(sSender);
if (iDisplaySide == null) {
iDisplaySide = iCount++;
mCacheCount.put(sSender, iDisplaySide);
}

if (iDisplaySide % 2 == 0) {
holder = mLeftHolder;
} else {
holder = mRightHolder;
}
holder.mLblUser.setText(sSender);
holder.mLblTime.setText(sTime);
holder.mTxtMsg.setText(sMsg);
int iHeight = holder.mTxtMsg.getPreferredSize().height + 20;
iHeight = iHeight < 85 ? 85 : iHeight;

System.err.println(iHeight);
int iH = table.getRowHeight(row);
if (iH != iHeight) {
table.setRowHeight(row, iHeight);
}
return holder;
}

private class Holder extends JPanel{
private static final long serialVersionUID = -2850253575244483981L;
JLabel mLblUser = new JLabel();
JLabel mLblTime = new JLabel();
RLabel mLblHead = new RLabel();
RTextPane mTxtMsg = new RTextPane();
public Holder() {
setBackground(Color.white);
}
}

private class LeftViewHolder extends Holder{

private static final long serialVersionUID = 1L;

private final Icon mBgIconLeft= new NinePatchImageIcon(this.getClass().getResource(
"/resource/msg_bg2.9.png"));
private final Icon mHeadLeft= new ImageIcon(this.getClass().getResource(
"/resource/head1.png"));
public LeftViewHolder() {
this.setLayout(new MigLayout("ins 0"));
mTxtMsg.setBackgroundIcon(mBgIconLeft);
mLblHead.setIcon(mHeadLeft);
this.add(mLblTime, "wmax pref");
this.add(mLblUser, "pushx,growx, wrap");
this.add(mLblHead, "spanx 2,split 2,wmax pref");
this.add(mTxtMsg, "growy, pushy, wmin 300px");
}
}


private class RightViewHolder extends Holder{
private static final long serialVersionUID = 1L;
private final Icon mBgIconRgh = new NinePatchImageIcon(this.getClass().getResource(
"/resource/msg_bg1.9.png"));
private final Icon mHeadRight = new ImageIcon(this.getClass().getResource(
"/resource/head2.png"));
public RightViewHolder() {
this.setLayout(new MigLayout("ins 0, rtl"));
mLblHead.setIcon(mHeadRight);
mTxtMsg.setBackgroundIcon(mBgIconRgh);
mLblUser.setHorizontalAlignment(JLabel.RIGHT);
this.add(mLblTime, "wmax pref");
this.add(mLblUser, "pushx,growx, wrap");
this.add(mLblHead, "spanx 2,split 2,wmax pref");
this.add(mTxtMsg, "growy, pushy, wmin 300px");
}
}


}





原创粉丝点击