如何使我的代码能够创建多个实例

时间:2014-07-23 03:24:19

标签: java swing inheritance jmeter instance

我正在更改JMeter Aggregate Report模块。我创建了一个新类StatVisualizerAddOn,其中包含要添加到JPanel类中StatVisualizer的组件,但是,目前只能创建它的一个实例。如果创建了多个实例,则数据不准确。

这是我写的StatVisualizerAddOn课程的主要部分

public class StatVisualizerAddOn {

    public static JTextField percentLine = null;

    private static String DEFAULT_PERCENT = "90.0";

    private static float percentage;

    private static JFrame errorWindow;

    private static JButton enterPercent;

    public JPanel initCustomization()
    {
        percentage = new Float(DEFAULT_PERCENT)/100;
        //new panel to contain all the elements
        JPanel percentLinePanel = new JPanel();
        //create text field for user input     
        percentLine = new JTextField(5);
        percentLine.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                percentLine.setText(percentLine.getText());
            }
        });
        percentLine.setEditable(true);
        percentLine.setText(DEFAULT_PERCENT);
        percentLine.setPreferredSize(new Dimension(100,25));
        //create label beside text field
        JLabel percentLineLabel = new JLabel();
        percentLineLabel.setText("Enter percent line to display (1-100)%:");
        //create the confirmation button
        enterPercent = new JButton();
        enterPercent.setText("Confirm");
        enterPercent.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                if(checkPercent(percentLine.getText())) //check for valid input
                {  
                    setPercent(percentLine.getText());
                    JOptionPane.showMessageDialog(errorWindow,
                        (percentLine.getText()+"% Line has been set."),"Confirmation",
                        JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        enterPercent.setPreferredSize(new Dimension(80,25));

        percentLinePanel.add(percentLineLabel, BorderLayout.WEST);
        percentLinePanel.add(percentLine, BorderLayout.CENTER);
        percentLinePanel.add(enterPercent, BorderLayout.EAST);

        return percentLinePanel;
    }

总之,有一些方法可以获取和设置percentline textfield,我没有在这里复制。

在我的StatVisualizer课程中,我只是添加了这个:

public StatVisualizer() {

    super();

    model = new ObjectTableModel(COLUMNS,
            SamplingStatCalculator.class,
            new Functor[] {
                new Functor("getLabel"),   //$NON-NLS-1$
                new Functor("getCount"),  //$NON-NLS-1$
                new Functor("getMeanAsNumber"),   //$NON-NLS-1$
                new Functor("getMedian"),  //$NON-NLS-1$
                new Functor("getPercentPoint",  //$NON-NLS-1$
                        new Object[] { StatVisualizerAddOn.getPercent() }),         //90% line
                new Functor("getMin"),  //$NON-NLS-1$
                new Functor("getMax"),   //$NON-NLS-1$
                new Functor("getErrorPercentage"),   //$NON-NLS-1$
                new Functor("getRate"),  //$NON-NLS-1$
                new Functor("getKBPerSecond")   //$NON-NLS-1$
            },
            new Functor[] { null, null, null, null, null, null, null, null, null, null },
            new Class[] { String.class, Long.class, Long.class, Long.class, Long.class,
                          Long.class, Long.class, String.class, String.class, String.class });
    clearData();
    init();
}

在对象模型中,我调用StatVisualizerAddOn.getPercent()来获取输入值,在init()中,我添加了以下行:

    StatVisualizerAddOn addon = new StatVisualizerAddOn();
    JPanel percentile = addon.initCustomization();
    mainPanel.add(percentile);

问题:

(例如1 StatVisualizer在我的程序中创建,我在percentline中输入90%,它将计算出第90个百分点。

如果我的程序中创建了2 StatVisualizer,则percentline中的1%输入90%的其他输入80%,StatVisualizer只会计算80%

需要一些帮助才能更改代码,以便在没有数据差异的情况下创建多个Statvisualizer

1 个答案:

答案 0 :(得分:1)

static不是你的朋友......

public static JTextField percentLine = null;
private static String DEFAULT_PERCENT = "90.0";
private static float percentage;
private static JFrame errorWindow;
private static JButton enterPercent;

这意味着只会在StatVisualizerAddOn个对象的所有实例中共享每个字段的单个实例。删除static引用,例如

public JTextField percentLine = null;
private String DEFAULT_PERCENT = "90.0";
private float percentage;
private JFrame errorWindow;
private JButton enterPercent;