swing开发测试工具

Easter79
• 阅读 718

要项目测试需要,使用swing编写个工具,给需要的人,留下代码:)

界面如下:

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);
    }

}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
3年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k