Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有文本字段的GUI,用于使用Swing进行输入_Java_Swing_Jtextfield - Fatal编程技术网

Java 带有文本字段的GUI,用于使用Swing进行输入

Java 带有文本字段的GUI,用于使用Swing进行输入,java,swing,jtextfield,Java,Swing,Jtextfield,实际上我是一个初学者,所以我尝试自己使用Swing创建GUI,其中用户在两个各自的字段中填写两个输入数字,因此有一个新的文本字段,我想在其中添加两个输入值 我想添加两个文本字段的整数,并在另一个文本字段中添加值和显示。如何将JTextField整数值提取到另一个JTextField import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI { pub

实际上我是一个初学者,所以我尝试自己使用Swing创建GUI,其中用户在两个各自的字段中填写两个输入数字,因此有一个新的文本字段,我想在其中添加两个输入值

我想添加两个文本字段的整数,并在另一个文本字段中添加值和显示。如何将
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中获取特定字符串。这一行显示错误: //错误:不兼容的类型 字符串sum2=t3.setText(str); 必需:字符串 发现:无效
1错误:(

没有运行代码,您在这里遇到问题

t3.setText(total);
因为
JTextField
没有
setText(int)
方法。您需要将值转换为
字符串

例如,您可以使用
Integer.toString(total)
,但使用
NumberFormat.getNumberInstance().format(total))
可能会得到更好的结果

t3.setText(NumberFormat.getNumberInstance().format(total));
已更新

您遇到的“另一个”问题让我绕了一圈,就是
Button1handler
extensions
GUI

class Button1handler extends GUI implements ActionListener {
这意味着,当它访问字段
t1
t2
时,会得到空值,因为这些字段与屏幕上的字段不同

如果
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));
这为操作处理程序提供了对用户在屏幕上操作的实际字段的引用


您可能会感兴趣的是,在这个需求(必须传递引用)上,哪个为您提供了egt的一些附加特性,但在进入它之前,您应该非常熟悉对象的上下文;)

你能给我解释一下这段代码吗:t3.setText(NumberFormat.getNumberInstance().format(total));虽然它抛出了一个关于“NumberFormat”的错误,“抛出了一个错误”或生成了一个编译器错误?如果是编译器错误,您可能没有导入NumberFormat。你在使用IDE吗?@AndrewG它会(最终)在
Integer.parseInt(f1.getText())
处抛出一个
NumberFormatException
,因为该字段由于引用问题而为空。我也试过了,仍然引发错误:找不到符号符号:变量NumberFormat位置:button1handler将
导入java.text.NumberFormat
添加到导入部分