gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:43:08 25 4
gpt4 key购买 nike

我正在对 JMeter 聚合报告模块进行更改。我创建了一个新类 StatVisualizerAddOn,其中包含要添加到 StatVisualizer 类中的 JPanel 的组件,但是只能使用它的一个实例当前创建。如果创建多个实例,数据不准确。

这是我编写的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,其中 1 个输入 90%,另一个输入 80% 在 percentline 中,则仅计算 80% 的 StatVisualizer )

需要一些帮助来了解如何更改代码,以便可以创建多个 Statvisibleizer 而不会出现数据差异。

最佳答案

static 在这里不是你的 friend ...

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;

关于java - 如何使我的代码能够创建多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com