머리를쥐어짜서 겨우해보았는데 메소드오류가납니다 도와주세요
조회수 1008회
package glephic; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; 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.JTextField;
public class sums extends JFrame{ private static JTextField TF = new JTextField(20); private static JTextField TA = new JTextField(20); private JButton BT = new JButton("꾸욱");
public sums(){
setTitle("입력된 값의 1까지의 합");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("입력후 버튼을누르세요"));
c.add(TF);
c.add(BT);
c.add(TA);
c.setBackground(Color.green);
BT.addActionListener(new ActionListener(){
public static int actionPerformed(ActionEvent e){ //<-여기에서 오류가납니다
String from = TF.getText();
int to = Integer.parseInt(from);
if(to >=1){
return (to -1) * to;}
else{
return to;
}
from = Integer.toString(to);
TA.setText(from);
TF.setText("");
}
});
setSize(250,250);
setVisible(true);
}
public static void main(String[] args) {
new sums();
}
}
오류내용은 This static method cannot hide the instance method from ActionListener 이것 구글링해도 안나오는데 어찌하면 좋을까요.......ㅗㅜㅑ
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
to를 전역변수로 선언한뒤 return을 다 지워줍니다. 그럼 else없이 if(to>=) to=to-1*to 잘돌아갑니다. 드리고 저걸 응용 펙토리얼문으로 만들어 보았습니다. public class sums extends JFrame{ private static JTextField TF = new JTextField(20); private static JTextField TA = new JTextField(50); private JButton BT = new JButton("꾸욱"); private static int to; public sums(){ setTitle("입력된 값의 1까지의 합"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JLabel("입력후 버튼을누르세요")); c.add(TF); c.add(BT); c.add(TA); c.setBackground(Color.green);
BT.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ TA.setText(""); String from = TF.getText(); to = Integer.parseInt(from); for(int i = to-1; i >=1; i--){ to= i * to; } from = Integer.toString(to); TA.setText(from); TF.setText(""); } }); setSize(600,150); setVisible(true); } public static void main(String[] args) { new sums(); } }
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력