2014-02-17 1 views
0

방금 ​​프로그래밍을 시작했으며 선생님이 나에게이 과제를 부여하여 클래스 파일을 사용하여 그래프 표시를 만들고이 파일에서 호출 할 메서드 목록을 작성했습니다.JTextField 문자열을 Int로 변환

나는 GUI를 사용하고 있으며 그래프를 변경하기 위해 숫자를 입력 할 수있는 세 개의 JTextFields이 필요합니다. 저는 현재 첫 번째 텍스트 필드에 어려움을 겪고 있습니다.이 텍스트 필드는 제가 마침표라고 부릅니다 (그래프에서 마침표를 변경 함). http://i.imgur.com/tC6ZY2C.png

그것이 내가이 프로그램을 실행할 때 (많은 코드를 건너 뜀), 이런 일이 중요한 코드입니다

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Name extends Jpanel implements ActionListener 
{ 
private SinCosGraph scg = new SinCosGraph //SinCosGraph is the name of the class file that im using. 

//delclaring all the components, dont think it is neccecary to write them all(JButtons, JPanels etc.) 

public Name() 
{ 
setLayout(new BorderLayout()); 
add("Center", scg); 

scg.setPeriod(45); 
System.out.println("The period is = " + scg.getPeriod()); 
System.out.println("Intperiod is = " + scg.getPeriod()); 

initComponent(); 
} 
public void initComponent() 
{ 
e = new JPanel(); 
e.setLayout(new GridLayout(5,1, 40, 40)); 

p1 = new JPanel(); 
p1.setPreferredSize(new Dimension(200, 50)); 
p1.setBorder(BorderFactory.createTitledBorder("Period")); 
period = new JTextField(5); 
period.setText("" + scg.getPeriod()); 
int intperiod = Integer.praseInt(period.getText()); 

p1.add(period); 
e.add(p1); 

add("East",e); 

. 
. 
. 
. 

public void actionPerformed(ActionEvent e) 
{ 
//Redrawing the graph 
if(e.getSource() == reDraw) 
{ 

scg.setPeriod(intperiod); 
repaint(); 
    } 

public static void main(String[]args) 
{ 
JFrame jf = new JFrame("Name"); 
jf.setSize(1000, 700); 
jf.setContentPane(new Name()); 
jf.setVisible(true); 

} 
} 

: 나는 코드 (중요한 부분)에 다음과 같이 썼다 나는 45로 설정했기 때문에 기간은 45라고 말합니다. 그러나 textfield은 기본 숫자 인 360을 보여줍니다. int 기간은 0입니다.

어떤 도움이 될지 모르겠지만 무엇이 잘못 됐는지 알 수 없습니다.

답변

0

System.out.println(Integer.parseInt(scg.getPeriod())); 
1

당신은 정수로 문자열을 변환 할 정수 클래스를 사용해야합니다보십시오. 당신은 정수 원하는 어느

:

Integer.parseInt(scg.getPeriod()); 
0

사용하는 정수 래퍼 클래스의 방법을 .. 데이터 캐스팅에 대한 추가 정보를 원하시면

Integer.parseInt("your string"); 

구글 "래퍼 클래스"과 방법