首页 > 解决方案 > 我正在尝试让 JButton Age 计算,但我有错误

问题描述

package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class agecal {
    
    JButton MyButton = new JButton("This is my button.");
    JTextField textField = new JTextField();
    MyButton.addActionListener (new ActionListener () {
    public void buttonFunction (Action Event e){
    int age;
    age = currentYear - birthYear;
    textField.setText(age);
    }}

当有人键入出生年份时,如何使这个 JButton 显示在下面,它在JButton图像下方显示年龄图像

标签: javaeclipse

解决方案


这是因为你试图在setText()方法上打印一个 int,这个方法接受一个字符串作为参数使用

textField.setText(String.valueOf(age));

推荐阅读