将字符串转换为int

时间:2015-02-20 04:38:53

标签: java 2d jtextfield

我正在尝试从jtextfield获取一些值然后将它们转换为int以将它们用作绘制多边形的参数,但是我很难将字符串转换为int,请帮助我,什么是最好的这样做是我的代码:

这是我从textfield获取数据的地方

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Transformacion extends JFrame {


    Transformacion_R2 grafico= new Transformacion_R2();


    public Transformacion()
       {  
           // Use helper methods
        createpuntosx();
        createpuntosy();
        createButton();

          xa=puntoFieldx1.getText();
          xb=puntoFieldx2.getText();
          xc=puntoFieldx3.getText(); 
          xd=puntoFieldx4.getText();


          createPanel();

          setSize(FRAME_WIDTH, FRAME_HEIGHT);
       }

    public void createButton()
       {  
          graficar = new JButton("Grafica");

          class PoligonoListener implements ActionListener
          {
             public void actionPerformed(ActionEvent event)
             {
                  JFrame frame = new JFrame(Transformacion_R2.TITLE);
                  frame.setContentPane(new Transformacion_R2());
                  frame.pack();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationRelativeTo(null);
                  frame.setVisible(true);
             }            
          }

          ActionListener listener = new PoligonoListener();
          graficar.addActionListener(listener);
       }


    public void createpuntosx()
       {
          puntosx = new JLabel("Puntos x: ");
          final int FIELD_WIDTH = 4;
          puntoFieldx1 = new JTextField(FIELD_WIDTH);
          puntoFieldx2 = new JTextField(FIELD_WIDTH);
          puntoFieldx3 = new JTextField(FIELD_WIDTH);
          puntoFieldx4 = new JTextField(FIELD_WIDTH);
       }

       public void createpuntosy()
       {
          puntosy = new JLabel("Puntos y: ");
          final int FIELD_WIDTH = 4;
          puntoFieldy1 = new JTextField(FIELD_WIDTH);
          puntoFieldy2 = new JTextField(FIELD_WIDTH);
          puntoFieldy3 = new JTextField(FIELD_WIDTH);
          puntoFieldy4 = new JTextField(FIELD_WIDTH);
       }


       public void createPanel()
       {
          JPanel northPanel = new JPanel();
          northPanel.add(puntosx);
          northPanel.add(puntoFieldx1);       
          northPanel.add(puntoFieldx2);
          northPanel.add(puntoFieldx3);
          northPanel.add(puntoFieldx4);
          northPanel.add(puntosy);
          northPanel.add(puntoFieldy1);
          northPanel.add(puntoFieldy2);
          northPanel.add(puntoFieldy3);
          northPanel.add(puntoFieldy4);

          add(northPanel,BorderLayout.NORTH);

          JPanel centerPanel = new JPanel();
          add(centerPanel,BorderLayout.CENTER);


          JPanel southPanel = new JPanel();
          southPanel.add(graficar);
          add(southPanel,BorderLayout.SOUTH);




       }


       private JLabel puntosx;
       private JTextField puntoFieldx1;
       private JTextField puntoFieldx2;
       private JTextField puntoFieldx3;
       private JTextField puntoFieldx4;
       private JLabel puntosy;
       private JTextField puntoFieldy1;
       private JTextField puntoFieldy2;
       private JTextField puntoFieldy3;
       private JTextField puntoFieldy4;



       private JButton graficar;
       public static String xa;
       public static String xb;
       public static String xc;
       public static String xd;
       public static String ya;
       public static String yb;
       public static String yc;
       public static String yd;
       private static final int FRAME_WIDTH = 800;
       private static final int FRAME_HEIGHT = 200;

这就是我将它们转换成字符串

的地方
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

/** Test applying affine transform on vector graphics */
@SuppressWarnings("serial")



public class Transformacion_R2 extends JPanel{

   public static final int CANVAS_WIDTH = 1000;
   public static final int CANVAS_HEIGHT = 1200;
   public static final String TITLE = "Tranformacion Lineal R2";

//DECLARO PUNTOS DEL POLIGONO
   int a=Integer.parseInt (Transformacion.xa);
   int b=Integer.parseInt (Transformacion.xb);
   int c=Integer.parseInt (Transformacion.xc);
   int d=Integer.parseInt (Transformacion.xd);
   int[] polygonXs = { a, b, c, d};
   int[] polygonYs = { 80, 70, 80, 40};
   Shape shape = new Polygon(polygonXs, polygonYs, polygonXs.length);


   /** Constructor to set up the GUI components */

   public Transformacion_R2() {
      setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
   }

   /** Custom painting codes on this JPanel */
   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);    
      setBackground(Color.BLACK);


      Graphics2D g2d = (Graphics2D)g;

      AffineTransform saveTransform = g2d.getTransform();

      AffineTransform identity = new AffineTransform();
      g2d.setTransform(identity);

      g2d.setColor(Color.GREEN);
      g2d.fill(shape);

      //TRANSFORMACIONES
      double x = 200.0, y = -500.0;  
      g2d.translate(x, y);
      g2d.rotate(Math.toRadians(30.0));
      g2d.scale(10, 9);



      g2d.setColor(Color.RED);
      g2d.fill(shape);

      g2d.setTransform(saveTransform);
   }

   /** The Entry main method */
   public static void main(String[] args) {

      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {


            JFrame frame = new JFrame(TITLE);
            frame.setContentPane(new Transformacion_R2());
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

         }
      });
   }
}

谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

int number = Integer.parseInt(jTextField.getText());

可以在以下位置找到执行此操作的典型方法 How to convert a String to an int in Java?

你似乎做得很好。您遇到了哪些问题,更具体地说?

如果未设置jtextField,则需要处理错误的可能性。

int number;
try {
    number = Integer.parseInt(jTextField.getText());
} catch(NumberFormatException e) {
    number = 0; // Handle the error or give a default value
}

答案 1 :(得分:0)

除非Transformacion的构造函数运行,否则您的xa始终为null。 在此代码构造函数中,Transformacion_R2的初始化在Transformacion的构造函数之前运行。因此,请确保在Transformacion_R2之前调用Transformacion的构造函数。要删除循环初始化,请删除Transformacion_R2 grafico = new Transformacion_R2();来自Transformacion Eg。代码可能是

 public static void main(String[] args) {

  SwingUtilities.invokeLater(new Runnable() {
     @Override
     public void run() {


        JFrame frame = new JFrame(TITLE);
        Transformacion trns = new Transformacion();
        frame.setContentPane(new Transformacion_R2());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

     }
  });

}