我需要項目中最後一件事的幫助。我需要將4個變量(命名爲a,b,c和d)從JDialog傳遞到另一個類中的JFreeChart。我已經添加了檢查站,看看它實際停止要處理,代碼如下:Java Swing將變量傳遞給JFreeChart類
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);
}
}
}
類我試圖傳遞變量是這個:
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;
}
}
}
什麼我得到印在控制檯是這樣的:
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
所以它不是實際上那使事情變壞的解析,並且它要麼是非常簡單的,我只是缺少一個點或者是不可能的...
+1我ganaste德馬諾:P – dic19
@nachokk 應用於您講述了變化,現在的結果在控制檯是這樣的: '第一個檢查點:一個1.0 B 2.0Ç3.0 d 4.0 第三檢查點:A 0.0 B 0.0 C 0.0 D 0.0 第二個檢查點:A 1.0 B 2.0 C 3.0 D 4.0' 按此順序。如果圖形是正確編碼的,那麼它沒有得到a,b,c和d變量(顯示f(x)= 0)。 – Asunez
@Anenez查看編輯 – nachokk