2014-06-16 34 views
0

其实我是一个初学者,所以我试图用Swing创建GUI,其中两个输入数字由用户在两个相应字段中填充,所以有一个新的文本字段,其中I想要添加2个输入值。图形用户界面,使用Swing输入文本字段

我想添加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"); 

     } 
}   

在这种编辑过的代码我可以检索在消息对话框。问题的总和值是我能得到在第三JtextField.It所述特定字符串示出了在这一行错误: //错误:不相容类型 字符串sum2 = t3.setText(str); 要求:找到字符串 :无效 1错误:(

回答

2

没有得到运行的代码,你在这里有一个问题...

t3.setText(total); 

因为JTextField没有setText(int)方法,而不是你。需要将值转换为String

您可以使用Integer.toString(total),但它可能给你更好的结果,以使用NumberFormat.getNumberInstance().format(total))代替,例如

t3.setText(NumberFormat.getNumberInstance().format(total)); 

更新

“其他”的问题,你有,这已经把我手忙脚乱,是Button1handler扩展GUI

class Button1handler extends GUI implements ActionListener { 

这意味着,事实上,当它访问字段t1t2它获得空值,因为这些字段与屏幕上的字段不同。

如果您ActionListener是在外部文件/类,你需要将类提供上下文,例如...

class Button1handler implements ActionListener { 

    private JTextField f1; 
    private JTextField f2; 
    private JTextField f3; 

    public Button1handler(JTextField f1, JTextField f2, JTextField f3) { 
     this.f1 = f1; 
     this.f2 = f2; 
     this.f3 = f3; 
    } 

    public void actionPerformed(ActionEvent e) { 
     int firstnumber = Integer.parseInt(f1.getText()); 
     int secondnumber = Integer.parseInt(f2.getText()); 
     int total = firstnumber + secondnumber; 
     f3.setText(NumberFormat.getNumberInstance().format(total)); 
    } 
} 

然后,你需要创建操作处理程序的新实例使用类似...

b1.addActionListener(new Button1handler(t1, t2, t3)); 

这提供了对这些被屏幕上的用户操纵的实际领域的参考操作处理程序...

您可能会对Nested Classes感兴趣,它可以为您提供一些额外的功能来满足此要求(必须通过引用),但在移动到对象之前应该与对象的上下文非常不同;)

+0

您能解释一下吗一段代码: t3.setText(NumberFormat.getNumberInstance()。format(total)); 虽然它会抛出有关“NumberFormat”的错误 – user3522245

+0

“抛出错误”或生成编译器错误?如果这是一个编译器错误,你可能没有导入NumberFormat。你在使用IDE吗? –

+0

nope只是使用简单的文本编辑器“notepad ++” – user3522245