如何将变量传递给另一个java文件



我想把值从我的计算器。java的输入到我的XYLineChart_AWT.java我怎么能做到这一点?

这是我的代码calculator.java

public class calculator extends javax.swing.JFrame {                     
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int x1, y1, x2, y2,
        inverted_x1, inverted_y1, inverted_x2, inverted_y2,
        x1Times2, x2Times2, y1Times2, y2Times2,
        x1raised, y1raised, x2raised, y2raised,
        computeX, computeY, computeS,
        x2inverted, y2inverted, invertedx2times2, invertedy2times2;
    try{
        x1 = Integer.parseInt(jTextField1.getText());
        y1 = Integer.parseInt(jTextField2.getText());
        x2 = Integer.parseInt(jTextField3.getText());
        y2 = Integer.parseInt(jTextField4.getText());
        Graphics g = jPanel1.getGraphics();
        g.drawLine(x1 + 200,y1 - 115 ,x2 + 200,y2 - 115);        
        inverted_x1 = x1 *= -1;
        inverted_y1 = y1 *= -1;
        inverted_x2 = x2 *= -1;      
        inverted_y2 = y2 *= -1;
        jTextField5.setText("(x" + Integer.toString(inverted_x1) + ")² + (y" + Integer.toString(inverted_y1) + ")²" 
                + " = (x" + Integer.toString(inverted_x2) + ")² + (y" + Integer.toString(inverted_y2) + ")²" );
        x1Times2 = inverted_x1*2;
        y1Times2 = inverted_y1*2;           
        x2Times2 = inverted_x2*2;
        y2Times2 = inverted_y2*2;                                   
        x1raised = inverted_x1*inverted_x1;
        y1raised = inverted_y1*inverted_y1;         
        x2raised = inverted_x2*inverted_x2;
        y2raised = inverted_y2*inverted_y2;
        jTextField9.setText("x" + Integer.toString(x1Times2) + "x" + Integer.toString(x1raised) + "+y²" + Integer.toString(y1Times2)
                + "y" + Integer.toString(y1raised) + "= x²" + Integer.toString(x2Times2) + "x" + Integer.toString(x2raised) + "+y²"
                + Integer.toString(y2Times2) + "y" + Integer.toString(y2raised));            
        x2inverted = x2raised *= -1;
        y2inverted = y2raised *= -1;
        invertedx2times2 = x2Times2 *= -1;
        invertedy2times2 = y2Times2 *= -1;
        computeX = x1Times2 + invertedx2times2;
        computeY = y1Times2 + invertedy2times2;
        computeS = x2inverted + y2inverted + x1raised + y1raised;
        jTextField17.setText(Integer.toString(computeX) + "x" + Integer.toString(computeY) + "y" + Integer.toString(computeS) + "=0");                                                                                 
        XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "");
        chart.pack( );          
        RefineryUtilities.centerFrameOnScreen( chart );          
        chart.setVisible( true );             
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "Please fill necessary inputs");
    }
}                        

这是我的XYLineChart_AWT.java的代码我将计算器。java中的x1,y1,x2和y2的值放入points。add

public class XYLineChart_AWT extends javax.swing.JFrame  
{
   public XYLineChart_AWT( String applicationTitle, String chartTitle )
   {
      super(applicationTitle);
      JFreeChart xylineChart = ChartFactory.createXYLineChart(
         chartTitle ,
         "X- axis" ,
         "Y - axis" ,
         createDataset() ,
         PlotOrientation.VERTICAL ,
         true , true , false);
      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 500 , 500 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
      XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
      renderer.setSeriesPaint( 0 , Color.RED );
      renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
      plot.setRenderer( renderer ); 
      setContentPane( chartPanel ); 
   }
   private XYDataset createDataset( )
   {
      final XYSeries points = new XYSeries( "bla bla" );          
      points.add( 2.0, 6.0);          
      points.add( 5.0, 3.0);                                  
      final XYSeriesCollection dataset = new XYSeriesCollection( );          
      dataset.addSeries( points );                   
      return dataset;
   }
   public static void main( String[ ] args ) 
   {
      XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "");
      chart.pack( );          
      RefineryUtilities.centerFrameOnScreen( chart );          
      chart.setVisible( true ); 
   }
}

对不起,我是java的新手,提前感谢你!

如果您通过另一个Java类的构造函数传递x1,x2,y1,y2的值,就可以实现这一点:

int[] array = new int[4];
array[0] = x1;
array[1] = x2;
array[2] = y1;
array[3] = x2;
XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "", array);

在其他Java类XYLineChart_AWT中,您需要用一个多参数展开构造函数。

    public class XYLineChart_AWT extends javax.swing.JFrame  
{
   int[] array;
   public XYLineChart_AWT( String applicationTitle, String chartTitle, int[] array )
   {
      super(applicationTitle);

      this.array = array;

JFreeChart xylineChart = ChartFactory.createXYLineChart(
         chartTitle ,
         "X- axis" ,
         "Y - axis" ,
         createDataset() ,
         PlotOrientation.VERTICAL ,
         true , true , false);
      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 500 , 500 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
      XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
      renderer.setSeriesPaint( 0 , Color.RED );
      renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
      plot.setRenderer( renderer ); 
      setContentPane( chartPanel ); 
   }

。array = array在XYLineChart_AWT类中可以使用array[0], array[1],…

相关内容

  • 没有找到相关文章

最新更新