我的项目绘图部分有问题。谁能告诉我这里做错了什么?该程序解决了,但它没有绘制。 [英] I'm having a problem with the plotting part of my project. Can someone tell me what I did wrong here? the program solves, but it doesn't plot.

查看:39
本文介绍了我的项目绘图部分有问题。谁能告诉我这里做错了什么?该程序解决了,但它没有绘制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package ohmslaw;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
        

public class ohmslaw extends JApplet implements ActionListener {
    
    double  Volt, Amp, Ohm,Volt2,Amp2,Ohm2;
    JLabel Voltage,
           Current,
           Resistance,
           Voltage2,
           Current2,
           Resistance2;
          
    JTextField volt,amp,ohm,volt2,amp2,ohm2;
    JButton compute, plot;

    
    public void init() 
    {
      
      Container c = getContentPane();
      c.setLayout(new FlowLayout());  
          
      Voltage = new JLabel("Voltage");     
      c.add(Voltage);  
      volt = new JTextField(8);
      c.add(volt); 
      
      Current = new JLabel("Current");     
      c.add(Current);  
      amp = new JTextField(8);
      c.add(amp);
      
      Resistance = new JLabel("Resistance");     
      c.add(Resistance);  
     
      ohm = new JTextField (8);
      ohm.setEditable (false);
      c.add(ohm);
      
      
      Voltage2 = new JLabel("Voltage2");     
      c.add(Voltage2);  
      volt2 = new JTextField(8);
      c.add(volt2); 
      
      Current2 = new JLabel("Current2");     
      c.add(Current2);  
      amp2 = new JTextField(8);
      c.add(amp2);
      
      Resistance2 = new JLabel("Resistance2");     
      c.add(Resistance2);  
     
      ohm2 = new JTextField (8);
      ohm2.setEditable (false);
      c.add(ohm2);
      
      compute = new JButton("Compute");
      compute.addActionListener (this);
      c.add(compute);
  
      plot = new JButton("Plot");
      plot.addActionListener (this);
      c.add(plot);
      
    }
    
        public void actionPerformed (ActionEvent e)
        {       
            Volt = Double.parseDouble(volt.getText());
            Amp = Double.parseDouble(amp.getText());
            Ohm = computeOhm(Volt,Amp);
            ohm.setText(Double.toString(Ohm));
            Volt2 = Double.parseDouble(volt2.getText());
            Amp2 = Double.parseDouble(amp2.getText());
            Ohm2 = computeOhm2(Volt2,Amp2);
            ohm2.setText(Double.toString(Ohm2));
            
        }
        public double computeOhm(double Volt, double Amp)
        {
            double dohm = (Volt/Amp);
            return dohm;        
        }         
         
        public double computeOhm2(double Volt2, double Amp2)
        {
            double dohm2 = (Volt2/Amp2);
            return dohm2;        
        }
    
        public class trial extends Application {
 
        @Override public void start(Stage stage) {
            stage.setTitle("Ohm's Law");
            //defining the axes
            final NumberAxis xAxis = new NumberAxis();
            final NumberAxis yAxis = new NumberAxis();
            xAxis.setLabel("Voltage");
            yAxis.setLabel("Current");
            //creating the chart
            final LineChart<number,number> lineChart = 
                    new LineChart<number,number>(xAxis,yAxis);
                
            lineChart.setTitle("Resistance Chart");
            //defining a series
            XYChart.Series series = new XYChart.Series();
            series.setName("Voltage vs. Current Graph");
            //populating the series with data
            series.getData().add(new XYChart.Data(volt, amp));
            series.getData().add(new XYChart.Data(volt2, amp2));
        
            Scene scene  = new Scene(lineChart,800,600);
            lineChart.getData().add(series);
       
            stage.setScene(scene);
            stage.show();
        }
 
        public void plotmain(String[] args) {
            launch(args);
        }
    }    
}

推荐答案

首先,我看到剧情按钮作为计算按钮附加到相同的 ActionListener



其次,最重要的是,从Java applet或应用程序显示JavaFX组件需要一些Java Swing JavaFX魔力。我只是提供一个简单的解决方案让你前进(而不是解释每个嵌入步骤)。



First off, I see that the Plot button is attached to the same ActionListener as the Compute button.

Secondly, and most importantly, displaying of JavaFX components from a Java applet or application requires some Java Swing JavaFX "magic". I simply supply a simple solution to get you going (as opposed to explaining every embedding step).

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class SimpleChart extends JApplet {
    
    double Volt, Amp, Ohm, Volt2, Amp2, Ohm2;
    JLabel Voltage, Current, Resistance, Voltage2, Current2, Resistance2;
    
    JTextField volt, amp, ohm, volt2, amp2, ohm2;
    JButton compute, plot;
    
    private final JFrame frame = new JFrame("Graph");
    private final JFXPanel jfxPanel = new JFXPanel();
    
    private LineChart<Number, Number> lineChart;
    
    public double computeOhm(double Volt, double Amp) {
        double dohm = (Volt / Amp);
        return dohm;
    }
    
    public double computeOhm2(double Volt2, double Amp2) {
        double dohm2 = (Volt2 / Amp2);
        return dohm2;
    }
    
    private void createScene() {
        
        Platform.runLater(new Runnable() {
            
            @Override
            public void run() {
                
                // defining the axes
                final NumberAxis xAxis = new NumberAxis();
                final NumberAxis yAxis = new NumberAxis();
                xAxis.setLabel("Voltage");
                yAxis.setLabel("Current");
                
                // creating the chart
                lineChart = new LineChart<Number, Number>(xAxis, yAxis);
                lineChart.setTitle("Resistance Chart");
                
                // defining a series
                XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
                series.setName("Voltage vs. Current Graph");
                
                // populating the series with data
                lineChart.getData().add(series);
                
                // Java Swing embedding JavaFX
                jfxPanel.setScene(new Scene(lineChart));
            }
        });
    }
    
    public void init() {
        
        createScene();
        
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        
        Voltage = new JLabel("Voltage");
        c.add(Voltage);
        volt = new JTextField(8);
        c.add(volt);
        
        Current = new JLabel("Current");
        c.add(Current);
        amp = new JTextField(8);
        c.add(amp);
        
        Resistance = new JLabel("Resistance");
        c.add(Resistance);
        
        ohm = new JTextField(8);
        ohm.setEditable(false);
        c.add(ohm);
        
        Voltage2 = new JLabel("Voltage2");
        c.add(Voltage2);
        volt2 = new JTextField(8);
        c.add(volt2);
        
        Current2 = new JLabel("Current2");
        c.add(Current2);
        amp2 = new JTextField(8);
        c.add(amp2);
        
        Resistance2 = new JLabel("Resistance2");
        c.add(Resistance2);
        
        ohm2 = new JTextField(8);
        ohm2.setEditable(false);
        c.add(ohm2);
        
        compute = new JButton("Compute");
        compute.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                Volt = Double.parseDouble(volt.getText());
                Amp = Double.parseDouble(amp.getText());
                Ohm = computeOhm(Volt, Amp);
                ohm.setText(Double.toString(Ohm));
                Volt2 = Double.parseDouble(volt2.getText());
                Amp2 = Double.parseDouble(amp2.getText());
                Ohm2 = computeOhm2(Volt2, Amp2);
                ohm2.setText(Double.toString(Ohm2));
            }
        });
        c.add(compute);
        
        this.plot = new JButton("Plot");
        this.plot.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                Platform.runLater(new Runnable() {
                    
                    @Override
                    public void run() {
                        // get and clear the series
                        XYChart.Series<Number, Number> series = lineChart.getData().get(0);
                        series.getData().clear();
                        
                        // populating the series with data
                        series.getData().add(new XYChart.Data<Number, Number>(Volt, Amp));
                        series.getData().add(new XYChart.Data<Number, Number>(Volt2, Amp2));
                        
                        // display the frame
                        frame.setVisible(true);
                    }
                });
            }
        });
        
        c.add(plot);
        
        frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        frame.getContentPane().add(jfxPanel, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(400, 400));
        frame.pack();
        
        setPreferredSize(new Dimension(1024, 600));
    }
}





我希望这会有所帮助。



I hope this helps.


这篇关于我的项目绘图部分有问题。谁能告诉我这里做错了什么?该程序解决了,但它没有绘制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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