java - 通过 JLabel 显示总和

标签 java swing actionlistener jlabel jtextfield

我是java新手,我只想制作一个简单的JFrame,它通过JtextFields接受两个数字作为输入,对它们求和并通过JLabel返回答案......但它根本没有按照它的假设去做要做的,这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.*;
public class gUi extends JFrame {
    private JLabel lbl;
    private JLabel lbl2;
    private JLabel lbl3;
    private JTextField tb;
    private JTextField tb1;
    private int num1;
    private int num2;
        public gUi(){
        super("Sum Dialog");
        setLayout(new FlowLayout());
        lbl = new JLabel("1st Number:");
        add(lbl);
        tb = new JTextField(15);
        add(tb);
        tb.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event){
         num1 = Integer.parseInt(tb.getText());
        }
        });
        lbl2 = new JLabel("2nd Number");
        add(lbl2);
        tb1= new JTextField(15);
        tb1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event ){
                num2= Integer.parseInt(tb1.getText());
            }
        });

        add(tb1);
        int sum= num1+num2;
        String ssum = Integer.toString(sum);
        lbl3 = new JLabel();
        lbl3.setText(ssum);
        add(lbl3);      
    }
    }

import java.awt.*;
import javax.swing.*;
public class Main {

    public static void main(String[] args) {
        gUi g =new gUi();
        g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        g.setVisible(true);
        g.setSize(180, 160);


    }

}

它编译得很好,但当我输入一些整数时,没有通过 JLabel 显示总和 通过文本字段... 我遇到这个问题已经一周了,并尝试了迄今为止我所知道的不同方法,但问题仍然存在......

最佳答案

Have a look at the code below(includes comments):

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;

/**
 * Shows a Stage that has two text Fields
 *
 */
@SuppressWarnings("serial")
public class UserInterface extends JFrame {

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JTextField textField2;

    /**
     * Constructor
     */
    public UserInterface() {
        super("Sum Dialog");

        // Layout
        setLayout(new FlowLayout());

        // 1st JLabel
        label1 = new JLabel("1st Number:");
        add(label1);
        textField1 = new JTextField(15);
        add(textField1);

        // 2 second JLabel
        label2 = new JLabel("2nd Number");
        add(label2);
        textField2 = new JTextField(15);
        add(textField2);

        // 3 third label
        label3 = new JLabel();
        label3.setSize(200, 30);
        add(label3);

        JButton button = new JButton("Calculate");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // calculate the sum when the Button is pressed
                int sum = Integer.parseInt(textField2.getText()) + Integer.parseInt(textField2.getText());
                label3.setText(Integer.toString(sum));
            }
        });
        add(button);

    }

    /**
     * Main method of the application
     * 
     * @param args
     */
    public static void main(String[] args) {
        UserInterface main = new UserInterface();
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);
        main.setSize(180, 160);

    }

}

关于java - 通过 JLabel 显示总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550152/

相关文章:

java - EJB @Asynchronous 在同一个类中不起作用

java - JFreeChart:缩放和平移后数据消失

java - 按钮上带有图像的 JOptionPane?

java - 将 int 分配给 JButtons 以提高方法效率?

java - JButton Action 监听器

java - 在 Java 中,什么时候 "do while"循环是唯一的选择?

java - 在RelativeLayout中居中GridView

java - 如何让 Spring MVC 在 JUnit 测试中调用验证?

java - JFrame鼠标点击停止键盘按钮

java - token 错误 ";",{预期 GUI