Java Swing将变量传递给JFreeChart类 [英] Java Swing passing variables to JFreeChart class

查看:102
本文介绍了Java Swing将变量传递给JFreeChart类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目的最后一件事上,我需要帮助.我需要将JDialog的4个变量(分别命名为a,b,c和d)传递给另一个类中的JFreeChart.我添加了检查点以查看它实际上在哪里停止处理,代码如下:

I need help with last one thing in my project. I need to pass 4 variables (named a, b, c and d) from JDialog to JFreeChart in another class. I have added checkpoints to see where it actually stops to be processed and the code is as follows:

package calc.kalkulator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class PanelFunkcji extends JDialog implements ActionListener {
    private JLabel lA, lB, lC, lD;
    private JTextField tA, tB, tC, tD;
    private JButton bOK, bCancel;
    private OknoFunkcji oknoFunkcji;
    public double dA, dB, dC, dD;






public PanelFunkcji(JFrame owner) {
    super(owner, "Wprowadzanie funkcji", true);
    setSize(300, 300);
    setLayout(null);

    bOK = new JButton("OK");
    bOK.setBounds(10, 40, 130, 20);
    add(bOK);
    bOK.addActionListener(this);

    bCancel = new JButton("Anuluj");
    bCancel.setBounds(150, 40, 130, 20);
    add(bCancel);
    bCancel.addActionListener(this);

    lA = new JLabel("A = ");
    lA.setBounds(10, 70, 40, 20);
    add(lA);

    tA = new JTextField("0");
    tA.setBounds(50, 70, 60, 20);
    add(tA);

    lB = new JLabel("B = ");
    lB.setBounds(10, 100, 40, 20);
    add(lB);

    tB = new JTextField("0");
    tB.setBounds(50, 100, 60, 20);
    add(tB);

    lC = new JLabel("C = ");
    lC.setBounds(10, 130, 40, 20);
    add(lC);

    tC = new JTextField("0");
    tC.setBounds(50, 130, 60, 20);
    add(tC);

    lD = new JLabel("D = ");
    lD.setBounds(10, 160, 40, 20);
    add(lD);

    tD = new JTextField("0");
    tD.setBounds(50, 160, 60, 20);
    add(tD);

}

public double getA() {
    return Double.parseDouble(tA.getText());
}

public double getB() {
    return Double.parseDouble(tB.getText());
}

public double getC() {
    return Double.parseDouble(tC.getText());
}

public double getD() {
    return Double.parseDouble(tD.getText());
}

@Override
public void actionPerformed(ActionEvent e) {
    Object z = e.getSource(); 
    if(z == bOK) {
        dA = getA();
        dB = getB();
        dC = getC();
        dD = getD();
        System.out.println("1st checkpoint: A " + dA + " B " + dB + " C " + dC + " D " + dD);
        if (oknoFunkcji == null) {
            oknoFunkcji = new OknoFunkcji("Wykres");
        }
        oknoFunkcji.setVisible(true);
        //setVisible(false);
    }
    else if (z == bCancel) {
        setVisible(false);          
    }   
}   
}

我正试图将变量传递给的类是这样的:

The class I am trying to pass the variables to is this one:

package calc.kalkulator;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.*;

class OknoFunkcji extends ApplicationFrame {

private static PanelFunkcji panelFunkcji;
private static double a;
private static double b;
private static double c;
private static double d;

public void getDane () {
    a = panelFunkcji.dA;
    b = panelFunkcji.dB;
    c = panelFunkcji.dC;
    d = panelFunkcji.dD;
    System.out.println("2nd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
}


public OknoFunkcji(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();

    panelFunkcji = new PanelFunkcji(new JFrame()); 
    chartPanel.setSize(500, 270);
    setContentPane(chartPanel);
    getDane();
    System.out.println("3rd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);


}

/**
 * Tworzy chart.
 * 
 * @param dataset - zbiór danych.
 * 
 * @return Zwraca instancję charta.
 */
private static JFreeChart createChart(XYDataset dataset) {
    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(
        " ",       // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,  
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.getDomainAxis().setLowerMargin(0.0);
    plot.getDomainAxis().setUpperMargin(0.0);
    return chart;
}

/**
 * Creates a sample dataset.
 * 
 * @return A sample dataset.
 */
public static XYDataset createDataset() {
    XYDataset result = DatasetUtilities.sampleFunction2D(new X2(), 
            -10.0, 10.0, 40, "f(x)");
    return result;
}

public static JPanel createDemoPanel() {
    JFreeChart chart = createChart(createDataset());
    return new ChartPanel(chart);
}

static class X2 implements Function2D {

    public double getValue(double x) {
        return a*x*x*x + b*x*x + c*x + d;
    }

}



}

我在控制台上打印的是:

And what I get printed in console is this:

 1st checkpoint: A 1.0 B 2.0 C 3.0 D 4.0
 2nd checkpoint: A 0.0 B 0.0 C 0.0 D 0.0
 3rd checkpoint: A 0.0 B 0.0 C 0.0 D 0.0

实际上,解析并不是使情况变糟的原因,或者它真的很简单,我只是遗漏了一点,还是不可能...

So it is not actually the parsing that's making things go bad, and either it is REALLY simple and I am just missing a point or it is impossible...

推荐答案

您错过了重点.问题是您在 OknoFunkcji 中有一个对 PanelFunkcji 的引用.

You are missing the point. The problem is that you have a reference in OknoFunkcji to PanelFunkcji.

这里您正在创建一个新实例,并且您不想拥有此JPanel的新实例.此外,您没有将其添加到任何容器中.

Here you are creating a new instance, and you don't want to have a new instance of this JPanel. Besides you are not adding it to any container.

panelFunkcji = new PanelFunkcji(new JFrame()); 

此类 PanelFunkcji 使用 OknoFunkcji ,而不是反之.因此,当您创建 OknoFunkcji 的新实例时,必须通过构造函数注入或setter注入传递该值.例如,您可以做这样的事情.

This class PanelFunkcji use OknoFunkcji and not viceversa. So when you create a new instance of OknoFunkcji you have to pass that values via constructor injection or setter injection. For example you could do something like this.

@Override
public void actionPerformed(ActionEvent e) {
    Object z = e.getSource(); 
    if(z == bOK) {
        dA = getA();
        dB = getB();
        dC = getC();
        dD = getD();
        System.out.println("1st checkpoint: A " + dA + " B " + dB + " C " + dC + " D " + dD);
        if (oknoFunkcji == null) {
            oknoFunkcji = new OknoFunkcji("Wykres");
        }
        oknoFunkcji.createChart(dA,dB,dC,dD); // added this method 
        oknoFunkcji.setVisible(true);
        //setVisible(false);
    }
    else if (z == bCancel) {
        setVisible(false);          
    }   
}  

在另一堂课中

  public OknoFunkcji(String title) {
         super(title);
         System.out.println("3rd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
  }

  public void createChart(double dA, double dB, double dC, double dD) {
        setData(dA,dB,dC,dD);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setSize(500, 270);
        setContentPane(chartPanel);
        revalidate();
        repaint();        
    }

    public void setData(double dA, double dB, double dC, double dD) {
        a = dA;
        b = dB;
        c = dC;
        d = dD;
        System.out.println("2nd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
    }

请注意,同样重要的是,您不应该使变量和所有方法都变为静态的,因此,没有实际的理由来创建该类的实例.不要使用空布局了解有关使用布局的信息经理.

As a side note and not less important you should not make your variables and all your methods static cause then there is no practical reason to create instances of that class. Don't use null layout learn about using Layout Manager.

这篇关于Java Swing将变量传递给JFreeChart类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆