简体   繁体   English

带有文本字段的GUI,可使用Swing输入

[英]GUI with text fields for input using Swing

Actually I'm a beginner, so I tried myself to create GUI using Swing where two input numbers are filled by the user in two respective fields, so there is a new text field where I want to add the 2 inputted value. 实际上我是一个初学者,所以我尝试自己使用Swing创建GUI,其中用户在两个相应的字段中填写了两个输入数字,因此有一个新的文本字段要在其中添加2个输入值。

I want to add the integers of 2 text fields and add both the values and display in another text field. 我想添加2个文本字段的整数,并添加两个值并显示在另一个文本字段中。 How to extract the JTextField integer values to another JTextField ? 如何将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");

        }
}           

In this edited code i can retrieving the sum value in message dialog box .The problem is that i can get the particular string in the 3rd JtextField.It shows error in this line: //error: incompatible types String sum2=t3.setText(str); 在这段经过编辑的代码中,我可以在消息对话框中检索总和值。问题是我可以在第3个JtextField中获取特定的字符串。它在此行中显示错误://错误:不兼容的类型String sum2 = t3.setText( str); required: String found:void 1 error :( 必需:找到的字符串:无效1错误:(

Without getting to run the code, you have a problem here... 在不运行代码的情况下,您在这里遇到了问题...

t3.setText(total);

Because JTextField does not have a setText(int) method. 因为JTextField没有setText(int)方法。 Instead you need to convert the value to a String . 相反,您需要将值转换为String

You can use Integer.toString(total) , but it might give you better results to use NumberFormat.getNumberInstance().format(total)) instead, for example 您可以使用Integer.toString(total) ,但是例如使用NumberFormat.getNumberInstance().format(total))可能会给您带来更好的结果NumberFormat.getNumberInstance().format(total))

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

Updated 更新

The "other" problem you have, which has had me running around in circles, is the fact that Button1handler extends GUI 我遇到的另一个问题是Button1handler扩展了GUI

class Button1handler extends GUI implements ActionListener {

This means, that when it access the fields t1 and t2 it's getting empty values, because these fields are not the same as the fields that are on the screen. 这意味着,当它访问字段t1t2它会得到空值,因为这些字段与屏幕上的字段不同。

If your ActionListener is in a external file/class, you will need provide context to the class, for example... 如果您的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));
    }
}

Then you'll need to create a new instance of the action handler using something like... 然后,您需要使用类似...的方法来创建动作处理程序的新实例。

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

This provides the action handler with a reference to the actual fields which are been manipulated by the user on the screen... 这为动作处理程序提供了对由用户在屏幕上操纵的实际字段的引用。

You might be interested in Nested Classes which provides you with some additional features to egt over this requirement (of having to pass references), but you should VERY varimular with context of objects before moving onto it ;) 您可能对嵌套类感兴趣, 该类为您提供了一些其他功能来满足这一要求(必须传递引用),但是在移动到对象之前,您应该对对象的上下文非常了解;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM