我正在更改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
。
答案 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;