更新数据时更新PieChart

时间:2017-10-23 20:22:22

标签: java javafx event-handling pie-chart

我在我的程序中制作了一个PieChart,我想让PieChart正常运行,所以当我更新任何字段时,我想与PieChart进行沟通。我尝试通过获取数据的值或通过创建事件处理程序来使其工作。真的卡住了。 pie1 得到的输出为0,它永远不会更新。

(现在我只是想让 pie1 工作,其余的工作将在稍后进行。

面板的代码是:

class CenterPaneel extends JPanel{
private static final long serialVersionUID = -835526987955952967L;
private final ObservableList<PieChart.Data> details = FXCollections.observableArrayList();
private PieChart pieChart;

public CenterPaneel(){
setLayout( new GridLayout(2,3,1,1));
setBackground(Color.DARK_GRAY);

double pie1 = SamenvattingPaneel.cst1;
double pie2 = 25;
double pie3 = 25;
double pie4 = 25;

JFXPanel jfxPanel = new JFXPanel();
final JFXPanel dataPaneel = jfxPanel;

ScrollPane sp = new ScrollPane();

details.addAll(new PieChart.Data("Catagorie 1", pie1),
        new PieChart.Data("Catagorie 2", pie2),
        new PieChart.Data("Catagorie 3", pie3),
        new PieChart.Data("Catagorie 4", pie4)
    );
pieChart = new PieChart();
pieChart.setData(details);
pieChart.setTitle("Kosten samenvatting");
pieChart.setLegendSide(Side.BOTTOM);
pieChart.setLabelsVisible(true);
sp.setContent(pieChart);

Scene scene = new Scene(sp, 600, 500);
dataPaneel.setScene(scene);

add( new Catagorie1Paneel() );                add( new Catagorie3Paneel() );           add( new SamenvattingPaneel() );
add( new Catagorie2Paneel() );                add( new Catagorie4Paneel() );           add( dataPaneel);
}

SamenvattingPaneel =

类的代码
public class SamenvattingPaneel extends JPanel{
private static final long serialVersionUID = 3366301906602088093L;
private JLabel verwachting, huidigeMaand, bespaard;
private JTextField verwachtingText, huidigeMaandText, bespaardText;
private JButton collectAll,updatePieChart;
public static double cst1;

public SamenvattingPaneel (){
    setLayout(new GridLayout(7,4,10,10));
    Border border = BorderFactory.createEmptyBorder(5,5,5,5);
    setBorder(border);
    setBackground(Color.LIGHT_GRAY);

    verwachting = new JLabel("verwacht inkomen ; ");
    verwachting.setHorizontalAlignment(JLabel.RIGHT);
    huidigeMaand = new JLabel("werkelijke uitgave ; ");
    huidigeMaand.setHorizontalAlignment(JLabel.RIGHT);
    bespaard = new JLabel("geld tekort/over : ");
    bespaard.setHorizontalAlignment(JLabel.RIGHT);

    verwachtingText = new JTextField(10);
    verwachtingText.addActionListener(new VerschilHandler());
    verwachtingText.setHorizontalAlignment(JTextField.CENTER);
    huidigeMaandText = new JTextField(10);
    huidigeMaandText.setEditable(false);
    huidigeMaandText.setBackground(Color.GRAY);
    huidigeMaandText.setHorizontalAlignment(JTextField.CENTER);
    bespaardText = new JTextField(10);
    bespaardText.setHorizontalAlignment(JTextField.CENTER);


    collectAll = new JButton("haal alle gegevens op");
    collectAll.addActionListener(new CollectAllHandler());

    updatePieChart = new JButton("update grafiek");
    updatePieChart.addActionListener(new UpdateHandler());


    add(collectAll);                    add(new JLabel(" "));                       add(new JLabel(" "));   
    add(new JLabel(" "));               add(verwachting);                           add(verwachtingText);   
    add(new JLabel(" "));               add(huidigeMaand);                          add(huidigeMaandText);  
    add(new JLabel(" "));               add(new JLabel(" "));                       add(new JLabel(" "));   
    add(new JLabel(" "));               add(bespaard);                              add(bespaardText);  
    add(new JLabel(" "));               add(new JLabel(" "));                       add(new JLabel(" "));   
    add(updatePieChart);                add(new JLabel(" "));                       add(new JLabel(" "));           

}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawLine(0, 60, 5000, 60);
}
class CollectAllHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        double totaal = 0;
        String temps1 = Catagorie1Paneel.subTotText1.getText();
        double tempd1 = Double.parseDouble(temps1); 
        String temps2 = Catagorie2Paneel.subTotText2.getText();
        double tempd2 = Double.parseDouble(temps2);
        String temps3 = Catagorie3Paneel.subTotText3.getText();
        double tempd3 = Double.parseDouble(temps3);
        String temps4 = Catagorie4Paneel.subTotText4.getText();
        double tempd4 = Double.parseDouble(temps4);

        totaal = tempd1+tempd2+tempd3+tempd4;
        huidigeMaandText.setText(String.format(Locale.US, "%.2f", totaal));
    }   
}
class VerschilHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        double verschil = 0;
        String temps5 = verwachtingText.getText();
        double tempd5 = Double.parseDouble(temps5);
        String temps6 = huidigeMaandText.getText();
        double tempd6 = Double.parseDouble(temps6);
        verschil = tempd5 - tempd6;         
        bespaardText.setText(String.format(Locale.US,"%.2f",verschil));

        if(verschil >= 0){
            bespaardText.setBackground(Color.GREEN);
        }
        else{
            bespaardText.setBackground(Color.RED);
        }
    }
}
class UpdateHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String temp1 = Catagorie1Paneel.s v ubTotText1.getText();
        cst1 = Double.parseDouble(temp1);
    }
}

我尝试过几件事,但没有成功。我想也许是一个事件处理程序,所以当我按下按钮时它会更新(仍在尝试,因此处理程序没有完成但是我被卡住了)。

提前致谢。

0 个答案:

没有答案