要项目测试需要,使用swing编写个工具,给需要的人,留下代码:)
界面如下:
用java写界面,真费事,使用delphi,分分钟的事情。
特点:
1、给JTextArea增加行数显示
2、给JTextArea增加JScrollPane自适应
贴代码:App.java
package com.etrans.etsv5.app.rdt.benchmark;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.util.EnumSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicLong;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import com.etrans.etsv5.lib.realtime.rdt.IRdtListener;
import com.etrans.etsv5.lib.realtime.rdt.ObjAlarm;
import com.etrans.etsv5.lib.realtime.rdt.ObjImage;
import com.etrans.etsv5.lib.realtime.rdt.ObjOpera;
import com.etrans.etsv5.lib.realtime.rdt.ObjReply;
import com.etrans.etsv5.lib.realtime.rdt.ObjTrack;
import com.etrans.etsv5.lib.realtime.rdt.ObjTrans;
import com.etrans.etsv5.lib.realtime.rdt.RdtConsumer;
import com.etrans.etsv5.lib.realtime.rdt.RdtType;
/**
* 版权所有 (C) 2016 ® E-trans Company <br />
* 单元名称: App.java <br />
* 说 明: <br />
* 作 者: yunnet <br />
* 创建时间: 2016年10月31日 上午11:13:01 <br />
* 最后修改: 2016年10月31日 上午11:13:01 <br />
* 修改历史: <br />
*/
public class App extends JFrame {
private static final long serialVersionUID = -3611164965284482273L;
// 设置label长度
public static final int JLABEL_WIDTH = 100;
// 设置label宽度
public static final int JLABEL_HEIGHT = 21;
// 设置text框长度
public static final int JTEXTFIELD_WIDTH = 121;
// 设置text框高度
public static final int JTEXTFIELD_HEIGHT = 21;
// 设置text框长度
public static final int JBUTTON_WIDTH = 100;
// 设置text框高度
public static final int JBUTTON_HEIGHT = 25;
// 设置上面控件直接的间隔
public static final int SPACING = 10;
private JLabel label_host = null;
private JComboBox<String> comboBox_host = null;
private JLabel label_group = null;
private JTextField text_group = null;
private JLabel label_instance = null;
private JTextField text_instance = null;
private JCheckBox checkBox1 = null;
private JCheckBox checkBox2 = null;
private JCheckBox checkBox3 = null;
private JCheckBox checkBox4 = null;
private JCheckBox checkBox5 = null;
private JCheckBox checkBox6 = null;
private JLabel label_vehicle_id = null;
private JTextField text_vehicle_id = null;
private JButton exec_button = null;
private String[] tabNames = {"log", "track", "alarm", "reply", "image", "trans", "oprea"};
private JTabbedPane jTabbedPane = new JTabbedPane();
private JPanel mainPanel = null;
private JPanel panel2 = null;
private JPanel jPanel_0 = null;
private JPanel jPanel_1 = null;
private JPanel jPanel_2 = null;
private JPanel jPanel_3 = null;
private JPanel jPanel_4 = null;
private JPanel jPanel_5 = null;
private JPanel jPanel_6 = null;
private JTextArea log_memo_0 = null;
private JTextArea log_memo_1 = null;
private JTextArea log_memo_2 = null;
private JTextArea log_memo_3 = null;
private JTextArea log_memo_4 = null;
private JTextArea log_memo_5 = null;
private JTextArea log_memo_6 = null;
//##################################################################
private RdtConsumer consumer;
private String lastData = "";
private AtomicLong[] cnt = new AtomicLong[6];
private volatile boolean isOK = false;
private int width = 0;
private int height = 0;
/**
* 构造函数
*/
public App() {
//设置工具标题
setTitle("RDT RocketMQ client test");
setName("formMain");
//获取屏幕大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = (int) (screenSize.getWidth() * 8 / 10) + 100;
height = (int) (screenSize.getHeight() * 8 / 10);
//设置界面大小
setSize(width, height);
//设置界面居中
setLocationRelativeTo(null);
//设置后,关闭程序时自动释放资源
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//设置界面信息
setContentPane(getMainPanel());
//设置界面可见
setVisible(true);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
panel2.setBounds(0,
comboBox_host.getY() + comboBox_host.getHeight() + 20,
getWidth() -17,
getHeight() - comboBox_host.getY() - comboBox_host.getHeight()-60
);
}
});
addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == JFrame.MAXIMIZED_BOTH) {
panel2.setBounds(0,
comboBox_host.getY() + comboBox_host.getHeight() + 20,
getWidth() -17,
getHeight() - comboBox_host.getY() - comboBox_host.getHeight()-60
);
}else if(e.getNewState() == JFrame.NORMAL){
panel2.setBounds(0,
comboBox_host.getY() + comboBox_host.getHeight() + 20,
getWidth() -17,
getHeight() - comboBox_host.getY() - comboBox_host.getHeight()-60
);
}
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
Object[] options = { "确定", "取消" };
JOptionPane pane2 = new JOptionPane("确定要退出吗?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[1]);
JDialog dialog = pane2.createDialog("警告");
dialog.setVisible(true);
Object selectedValue = pane2.getValue();
if (selectedValue == null || selectedValue == options[1]) {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 这个是关键
} else if (selectedValue == options[0]) {
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
});
this.consumer = new RdtConsumer();
for(int i = 0; i < 6; i++)
cnt[i] = new AtomicLong(0);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
private String hintString = "";
@Override
public void run() {
if(isOK){
sb.delete(0, sb.length());
sb.append("::::::")
.append("track:").append(cnt[0].get()).append(" ")
.append("alarm:").append(cnt[1].get()).append(" ")
.append("reply:").append(cnt[2].get()).append(" ")
.append("image:").append(cnt[3].get()).append(" ")
.append("trans:").append(cnt[4].get()).append(" ")
.append("opera:").append(cnt[5].get()).append(" ")
.append(lastData);
String a = sb.toString();
if(!hintString.equals(a)){
doAddLog_1(a);
hintString = a;
}
}
}
}, 1000, 1000);
}
private void layoutComponents(){
int i = 0;
jPanel_0 = new JPanel();
jPanel_0.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_0);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_0);
log_memo_0 = new JTextArea();
log_memo_0.setLineWrap(true);
JScrollPane scrollPane_0 = new JScrollPane(log_memo_0, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_0.add(scrollPane_0);
LineNumberHeaderView lineNumberHeader_0 = new LineNumberHeaderView();
//设置行号的高度,如果和textarea的行高不匹配,可以修改此值
lineNumberHeader_0.setLineHeight(16);
scrollPane_0.setRowHeaderView(lineNumberHeader_0);
JMenuItem item_0 = new JMenuItem("clear");
item_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log_memo_0.setText("");
}
});
JPopupMenu popupMenu_0 = new JPopupMenu();
popupMenu_0.add(item_0);
log_memo_0.add(popupMenu_0);
log_memo_0.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_0.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_1 = new JPanel();
jPanel_1.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_1);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
log_memo_1 = new JTextArea();
log_memo_1.setLineWrap(true);
JScrollPane scrollPane_1 = new JScrollPane(log_memo_1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_1.add(scrollPane_1);
LineNumberHeaderView lineNumberHeader_1 = new LineNumberHeaderView();
lineNumberHeader_1.setLineHeight(16);
scrollPane_1.setRowHeaderView(lineNumberHeader_1);
JMenuItem item_1 = new JMenuItem("clear");
item_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log_memo_1.setText("");
}
});
JPopupMenu popupMenu_1 = new JPopupMenu();
popupMenu_1.add(item_1);
log_memo_1.add(popupMenu_1);
log_memo_1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_1.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_2 = new JPanel();
jPanel_2.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_2);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_2);
log_memo_2 = new JTextArea();
log_memo_2.setLineWrap(true);
JScrollPane scrollPane_2 = new JScrollPane(log_memo_2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_2.add(scrollPane_2);
LineNumberHeaderView lineNumberHeader_2 = new LineNumberHeaderView();
lineNumberHeader_2.setLineHeight(16);
scrollPane_2.setRowHeaderView(lineNumberHeader_2);
JMenuItem item_2 = new JMenuItem("clear");
item_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log_memo_2.setText("");
}
});
JPopupMenu popupMenu_2 = new JPopupMenu();
popupMenu_2.add(item_2);
log_memo_2.add(popupMenu_2);
log_memo_2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_2.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_3 = new JPanel();
jPanel_3.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_3);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_3);
log_memo_3 = new JTextArea();
log_memo_3.setLineWrap(true);
JScrollPane scrollPane_3 = new JScrollPane(log_memo_3, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_3.add(scrollPane_3);
LineNumberHeaderView lineNumberHeader_3 = new LineNumberHeaderView();
lineNumberHeader_3.setLineHeight(16);
scrollPane_3.setRowHeaderView(lineNumberHeader_3);
JMenuItem item_3 = new JMenuItem("clear");
item_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
item_3.setText("");
}
});
JPopupMenu popupMenu_3 = new JPopupMenu();
popupMenu_3.add(item_3);
log_memo_3.add(popupMenu_3);
log_memo_3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_3.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_4 = new JPanel();
jPanel_4.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_4);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_4);
log_memo_4 = new JTextArea();
log_memo_4.setLineWrap(true);
JScrollPane scrollPane_4 = new JScrollPane(log_memo_4, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_4.add(scrollPane_4);
LineNumberHeaderView lineNumberHeader_4 = new LineNumberHeaderView();
lineNumberHeader_4.setLineHeight(16);
scrollPane_4.setRowHeaderView(lineNumberHeader_4);
JMenuItem item_4 = new JMenuItem("clear");
item_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
item_4.setText("");
}
});
JPopupMenu popupMenu_4 = new JPopupMenu();
popupMenu_4.add(item_4);
log_memo_4.add(popupMenu_4);
log_memo_4.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_4.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_5 = new JPanel();
jPanel_5.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_5);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_5);
log_memo_5 = new JTextArea();
log_memo_5.setLineWrap(true);
JScrollPane scrollPane_5 = new JScrollPane(log_memo_5, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_5.add(scrollPane_5);
LineNumberHeaderView lineNumberHeader_5 = new LineNumberHeaderView();
lineNumberHeader_5.setLineHeight(16);
scrollPane_5.setRowHeaderView(lineNumberHeader_5);
JMenuItem item_5 = new JMenuItem("clear");
item_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
item_5.setText("");
}
});
JPopupMenu popupMenu_5 = new JPopupMenu();
popupMenu_5.add(item_5);
log_memo_5.add(popupMenu_5);
log_memo_5.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_5.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//////////////////////////////////////////////////////////////
jPanel_6 = new JPanel();
jPanel_6.setLayout(new BorderLayout());
jTabbedPane.addTab(tabNames[i++], jPanel_6);
jTabbedPane.setMnemonicAt(0, KeyEvent.VK_6);
log_memo_6 = new JTextArea();
log_memo_6.setLineWrap(true);
JScrollPane scrollPane_6 = new JScrollPane(log_memo_6, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jPanel_6.add(scrollPane_6);
LineNumberHeaderView lineNumberHeader_6 = new LineNumberHeaderView();
lineNumberHeader_6.setLineHeight(16);
scrollPane_6.setRowHeaderView(lineNumberHeader_6);
JMenuItem item_6 = new JMenuItem("clear");
item_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
item_6.setText("");
}
});
JPopupMenu popupMenu_6 = new JPopupMenu();
popupMenu_6.add(item_6);
log_memo_6.add(popupMenu_6);
log_memo_6.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu_6.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
private final StringBuilder sb = new StringBuilder();
private Container getMainPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(null);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 1));
String host_list[] = {"192.168.2.4:9876","10.10.1.140:9876;10.10.1.160:9876"};
label_host = new JLabel("host");
comboBox_host = new JComboBox<>(host_list);
comboBox_host.setEditable(true);
label_group = new JLabel("group");
text_group = new JTextField("RdtBenchmark");
label_instance = new JLabel("instance");
text_instance = new JTextField("RdtBenchmark001");
checkBox1 = new JCheckBox("轨迹数据");
checkBox2 = new JCheckBox("报警数据");
checkBox3 = new JCheckBox("指令回复");
checkBox4 = new JCheckBox("图片数据");
checkBox5 = new JCheckBox("透传数据");
checkBox6 = new JCheckBox("业务数据");
label_vehicle_id = new JLabel("vehicleID");
text_vehicle_id = new JTextField();
exec_button = new JButton("go");
exec_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(text_group.getText().isEmpty()){
doAddLog_0("请输入服务组名");
return;
}
if(text_instance.getText().isEmpty()){
doAddLog_0("请输入实例名");
return;
}
String addrs = comboBox_host.getSelectedItem().toString().trim();
String group = text_group.getText().trim();
String instance = text_instance.getText().trim();
doAddLog_0("::::::::::MQ address: " + addrs);
doAddLog_0("::::::::::MQ group: " + group);
doAddLog_0("::::::::::MQ instance: " + instance);
consumer.setup(addrs, group, instance);
EnumSet<RdtType> data_types = EnumSet.noneOf(RdtType.class);
if(checkBox1.isSelected())
data_types.add(RdtType.RDT_TRACK);
if(checkBox2.isSelected())
data_types.add(RdtType.RDT_ALARM);
if(checkBox3.isSelected())
data_types.add(RdtType.RDT_REPLY);
if(checkBox4.isSelected())
data_types.add(RdtType.RDT_IMAGE);
if(checkBox5.isSelected())
data_types.add(RdtType.RDT_TRANS);
if(checkBox6.isSelected())
data_types.add(RdtType.RDT_OPERA);
if (data_types.isEmpty()){
doAddLog_0("请选择订阅数据类型");
return;
}
try {
consumer.subscribe(data_types);
} catch (Exception e2) {
doAddLog_0(e2.getMessage());
return;
}
consumer.registerListener(listener());
try {
consumer.start();
isOK = true;
doAddLog_0("::::::::::consumer start.");
} catch (Exception e1) {
doAddLog_0(e1.getMessage());
}
}
});
//host
label_host.setBounds(30, 10, JLABEL_WIDTH, JLABEL_HEIGHT);
comboBox_host.setBounds(30, label_host.getY() + label_host.getHeight(), 200, JTEXTFIELD_HEIGHT);
//group
label_group.setBounds(comboBox_host.getX() + comboBox_host.getWidth() + 5, 10, JLABEL_WIDTH, JLABEL_HEIGHT);
text_group.setBounds(comboBox_host.getX() + comboBox_host.getWidth() + 5, label_group.getY() + label_group.getHeight(), JTEXTFIELD_WIDTH, JTEXTFIELD_HEIGHT);
//instance
label_instance.setBounds(text_group.getX() + text_group.getWidth() + 5, 10, JLABEL_WIDTH, JLABEL_HEIGHT);
text_instance.setBounds(text_group.getX() + text_group.getWidth() + 5, label_instance.getY() + label_instance.getHeight(), JTEXTFIELD_WIDTH, JTEXTFIELD_HEIGHT);
checkBox1.setBounds(text_instance.getX() + text_instance.getWidth(), 10, JLABEL_WIDTH, JLABEL_HEIGHT);
checkBox2.setBounds(text_instance.getX() + text_instance.getWidth(), checkBox1.getY() + checkBox1.getHeight(), JLABEL_WIDTH, JLABEL_HEIGHT);
checkBox3.setBounds(checkBox1.getX() + checkBox1.getWidth(), 10, JLABEL_WIDTH, JLABEL_HEIGHT);
checkBox4.setBounds(checkBox1.getX() + checkBox1.getWidth(), checkBox3.getY() + checkBox3.getHeight(), JLABEL_WIDTH, JLABEL_HEIGHT);
checkBox5.setBounds(checkBox3.getX() + checkBox3.getWidth(), 10, JLABEL_WIDTH, JLABEL_HEIGHT);
checkBox6.setBounds(checkBox3.getX() + checkBox3.getWidth(), checkBox5.getY() + checkBox5.getHeight(), JLABEL_WIDTH, JLABEL_HEIGHT);
//vehicle_id
label_vehicle_id.setBounds(checkBox5.getX() + checkBox5.getWidth() + 5, 10, JLABEL_WIDTH, JLABEL_HEIGHT);
text_vehicle_id.setBounds(checkBox5.getX() + checkBox5.getWidth() + 5, label_vehicle_id.getY() + label_vehicle_id.getHeight(), JTEXTFIELD_WIDTH, JTEXTFIELD_HEIGHT);
//go
exec_button.setBounds(text_vehicle_id.getX() + text_vehicle_id.getWidth() + 20, label_vehicle_id.getY() + label_vehicle_id.getHeight() - 2, JBUTTON_WIDTH, JBUTTON_HEIGHT);
//panel2
panel2.setBounds(0,
comboBox_host.getY() + comboBox_host.getHeight() + 20,
this.width -17,
height - comboBox_host.getY() - comboBox_host.getHeight()-60
);
mainPanel.add(label_host);
mainPanel.add(comboBox_host);
mainPanel.add(label_group);
mainPanel.add(text_group);
mainPanel.add(label_instance);
mainPanel.add(text_instance);
mainPanel.add(checkBox1);
mainPanel.add(checkBox2);
mainPanel.add(checkBox3);
mainPanel.add(checkBox4);
mainPanel.add(checkBox5);
mainPanel.add(checkBox6);
mainPanel.add(label_vehicle_id);
mainPanel.add(text_vehicle_id);
mainPanel.add(exec_button);
layoutComponents();
panel2.add(jTabbedPane);
mainPanel.add(panel2);
return mainPanel;
}
/**
* @return
*/
private IRdtListener listener() {
return new IRdtListener() {
public void onTrack(ObjTrack _data) {
cnt[0].incrementAndGet();
doAddLog_1(_data.toString());
}
public void onAlarm(ObjAlarm _data) {
cnt[1].incrementAndGet();
doAddLog_2(_data.toString());
}
public void onReply(ObjReply _data) {
cnt[2].incrementAndGet();
doAddLog_3(_data.toString());
}
public void onImage(ObjImage _data) {
cnt[3].incrementAndGet();
doAddLog_4(_data.toString());
}
public void onTrans(ObjTrans _data) {
cnt[4].incrementAndGet();
doAddLog_5(_data.toString());
}
public void onOpera(ObjOpera _data) {
cnt[5].incrementAndGet();
doAddLog_6(_data.toString());
}
};
}
public void doAddLog_0(final String _data){
if(log_memo_0.getLineCount() > 1000)
log_memo_0.setText("");
log_memo_0.append(_data);
log_memo_0.append("\r\n");
}
public void doAddLog_1(final String _data){
if(log_memo_1.getLineCount() > 1000)
log_memo_1.setText("");
log_memo_1.append(_data);
log_memo_1.append("\r\n");
}
public void doAddLog_2(final String _data){
if(log_memo_2.getLineCount() > 1000)
log_memo_2.setText("");
log_memo_2.append(_data);
log_memo_2.append("\r\n");
}
public void doAddLog_3(final String _data){
if(log_memo_3.getLineCount() > 1000)
log_memo_3.setText("");
log_memo_3.append(_data);
log_memo_3.append("\r\n");
}
public void doAddLog_4(final String _data){
if(log_memo_4.getLineCount() > 1000)
log_memo_4.setText("");
log_memo_4.append(_data);
log_memo_4.append("\r\n");
}
public void doAddLog_5(final String _data){
if(log_memo_5.getLineCount() > 1000)
log_memo_5.setText("");
log_memo_5.append(_data);
log_memo_5.append("\r\n");
}
public void doAddLog_6(final String _data){
if(log_memo_6.getLineCount() > 1000)
log_memo_6.setText("");
log_memo_6.append(_data);
log_memo_6.append("\r\n");
}
public static void main(String[] args) {
new App();
}
}
LineNumberHeaderView.java
package com.etrans.etsv5.app.rdt.benchmark;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
* 版权所有 (C) 2016 ® E-trans Company <br />
* 单元名称: LineNumberHeaderView.java <br />
* 说 明: <br />
* 作 者: yunnet <br />
* 创建时间: 2016年11月2日 下午9:29:18 <br />
* 最后修改: 2016年11月2日 下午9:29:18 <br />
* 修改历史: <br />
*/
public class LineNumberHeaderView extends JComponent {
private static final long serialVersionUID = 9177903177690864500L;
private final Font DEFAULT_FONT = new Font(Font.MONOSPACED, Font.PLAIN, 13);
public final Color DEFAULT_BACKGROUD = new Color(228, 228, 228);
public final Color DEFAULT_FOREGROUD = Color.BLACK;
public final int nHEIGHT = Integer.MAX_VALUE - 1000000;
public final int MARGIN = 5;
private int lineHeight;
private int fontLineHeight;
private int currentRowWidth;
private FontMetrics fontMetrics;
public LineNumberHeaderView() {
setFont(DEFAULT_FONT);
setForeground(DEFAULT_FOREGROUD);
setBackground(DEFAULT_BACKGROUD);
setPreferredSize(9999);
}
public void setPreferredSize(int row) {
int width = fontMetrics.stringWidth(String.valueOf(row));
if (currentRowWidth < width) {
currentRowWidth = width;
setPreferredSize(new Dimension(2 * MARGIN + width + 1, nHEIGHT));
}
}
@Override
public void setFont(Font font) {
super.setFont(font);
fontMetrics = getFontMetrics(getFont());
fontLineHeight = fontMetrics.getHeight();
}
public int getLineHeight() {
if (lineHeight == 0)
return fontLineHeight;
return lineHeight;
}
public void setLineHeight(int lineHeight) {
if (lineHeight > 0)
this.lineHeight = lineHeight;
}
public int getStartOffset() {
return 4;
}
@Override
protected void paintComponent(Graphics g) {
int nlineHeight = getLineHeight();
int startOffset = getStartOffset();
Rectangle drawHere = g.getClipBounds();
g.setColor(getBackground());
g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
g.setColor(getForeground());
int startLineNum = (drawHere.y / nlineHeight) + 1;
int endLineNum = startLineNum + (drawHere.height / nlineHeight);
int start = (drawHere.y / nlineHeight) * nlineHeight + nlineHeight - startOffset;
for (int i = startLineNum; i <= endLineNum; ++i) {
String lineNum = String.valueOf(i);
int width = fontMetrics.stringWidth(lineNum);
g.drawString(lineNum + " ", MARGIN + currentRowWidth - width - 1, start);
start += nlineHeight;
}
setPreferredSize(endLineNum);
}
}