실제로 저는 초보자입니다. 그래서 두 개의 입력 필드가 두 개의 입력란에 사용자가 채우는 Swing을 사용하여 GUI를 만들려고 했으므로 새 텍스트 필드가 있습니다. 입력 된 2 개의 값을 더하고 싶습니다.Swing을 사용하여 입력 용 텍스트 필드가있는 GUI
2 개의 텍스트 필드의 정수를 추가하고 두 개의 값을 모두 추가하고 다른 텍스트 필드에 표시하려고합니다. JTextField
정수 값을 다른 JTextField
에 어떻게 추출합니까? // 오류 : 내가 메시지 대화 상자 국지적 인 문제에 합계 값을 검색 할 수있는이 편집 된 코드에서
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI
{
public JFrame f;
public JPanel p;
public JLabel l1,l2;
public JButton b1;
public JButton b2;
public JTextField t1;
public JTextField t2;
public JTextField t3;
public GUI()
{
f=new JFrame("hello world application");
p=new JPanel(true);
l1=new JLabel("enter first number");
t1=new JTextField(30);
l2=new JLabel("\n enter the second number");
t2=new JTextField(30);
t3=new JTextField(30);
b1=new JButton("check here");
b2=new JButton("clear all");
//b1.addActionCommand1("checking....");
//b2.addActionCommand2("clearing all input's ....");
}
public void launch()
{
f.setSize(400,500);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(t3);
p.add(b1);
p.add(b2);
f.pack();
f.add(p);
//l.setFont(Times Roman);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setSize(400,500);
p.setForeground(Color.red);
p.setBackground(Color.green);
f.setLayout(new BorderLayout());
b1.addActionListener(new Button1handler(t1,t2,t3));
b2.addActionListener(new Button2handler());
f.setVisible(true);
}
public static void main(String a[])
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
GUI br=new GUI();
br.launch();
}
});
}
}
class Button1handler implements ActionListener
{
private JTextField t1;
private JTextField t2;
private JTextField t3;
public Button1handler(JTextField t1, JTextField t2, JTextField t3)
{
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
public void actionPerformed(ActionEvent e)
{
System.out.println("action occurred for checking");
String firstnumber=t1.getText();
String secondnumber=t2.getText();
int num1=Integer.parseInt(firstnumber);
int num2=Integer.parseInt(secondnumber);
int sum=num1+num2;
String str=Integer.toString(sum);
String sum2=t3.setText(str);
JOptionPane.showMessageDialog(null, "The sum is " + sum,"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE);
}
}
class Button2handler implements ActionListener
{
public void actionPerformed(ActionEvent r)
{
System.out.println("clearing all");
}
}
내가 3 JtextField.It에서 특정 문자열이 줄에 오류가 표시 얻을 수 있다는 것입니다 호환되지 않는 유형 문자열 sum2 = t3.setText (str); 이 필요합니다 : 찾을 문자열 : 무효 1 오류 :(
나에게 설명 할 수 있습니까? 코드 조각 : t3.setText (NumberFormat.getNumberInstance(). format (total)); "NumberFormat"에 대한 오류가 발생합니다. – user3522245
"오류가 발생했습니다"또는 컴파일러 오류가 발생합니까? 컴파일러 오류 일 경우 NumberFormat을 가져 오지 않았을 수 있습니다. IDE를 사용하고 있습니까? –
단순한 텍스트 편집기를 사용하지 않고 "notepad ++" – user3522245