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

时间:2015-10-08 12:08:47

标签: java netbeans

我想将我的calculator.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的代码,我将它们从points.add中的calculator.java中放入x1,y1,x2和y2的值

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的新手并提前感谢你!

1 个答案:

答案 0 :(得分:0)

如果你通过另一个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 中,您需要再使用一个参数扩展constuctor。

    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 ); 
   }

在类XYLineChart_AWT中的 this.array = array 之后,你可以使用array [0],array [1],...