JTextField 在单击 JButton 时获取用户的整数输入以添加
JTextField to get integer input from user to add when clicked on JButton
我要构建一个简单的 swing UI 应用程序以从单独的 JTextField 获取用户的输入,然后最后单击 Button 执行添加以显示在 JButton 下方的 JTextField 上。
输入字段 (JTextField) 用于接收整数值。
package com.APPOne_G5;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppOne_G5 {
private JPanel calculatorFrame;
private JTextField inputField1;
private JTextField inputField2;
private JButton calculateButton;
private JTextField outputField;
private JLabel inputFieldLabel1;
private JLabel inputFieldLabel2;
private JLabel outputFieldLabel;
private int number1, number2;
private int calc = number1 + number2;
public AppOne_G5() {
inputField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
number1 = Integer.parseInt(inputField1.getText());
}
});
inputField2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
number2 = Integer.parseInt(inputField2.getText());
}
});
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calc = number1 + number2;
outputField.setText(String.valueOf(number1+number2));
}
});
outputField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputField.setEditable(false);
outputField.setText(String.valueOf(calc));
}
});
}
public static void main(String[] args) {
JFrame app = new JFrame("Group 5 AppOne");
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
app.setContentPane(new AppOne_G5().calculatorFrame);
app.setLocationRelativeTo(null);
app.setVisible(true);
app.pack();
}
}
输入要添加的值后我得到零。
This is the GUI form of the application
根据我的评论,您只需要一个在您单击按钮时激活的动作侦听器。这是一个完整的工作示例,说明它如何工作:
public class AppOne_G5 {
private JFrame app = new JFrame("Group 5 AppOne");
private JPanel calculatorFrame = new JPanel();
private JTextField inputField1 = new JTextField("2");
private JTextField inputField2 = new JTextField("5");
private JButton calculateButton = new JButton("Calculate");
private JTextField outputField = new JTextField(" ");
private JLabel inputFieldLabel1 = new JLabel("Enter Integer");
private JLabel inputFieldLabel2 = new JLabel("Enter Integer");;
private JLabel outputFieldLabel = new JLabel("Result");
private int number1, number2;
private int calc;
public AppOne_G5() {
//Add components in order
app.add(calculatorFrame);
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
calculatorFrame.add(inputFieldLabel1);
calculatorFrame.add(inputField1);
calculatorFrame.add(inputFieldLabel2);
calculatorFrame.add(inputField2);
calculatorFrame.add(calculateButton);
calculatorFrame.add(outputFieldLabel);
calculatorFrame.add(outputField);
//Create action listener
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Get the inputs
number1 = Integer.parseInt(inputField1.getText());
number2 = Integer.parseInt(inputField2.getText());
System.out.println("number 1: " + number1 + ", number2: " + number2);
//Calculate the output
calc = number1 + number2;
System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);
//Show the output
outputField.setText(String.valueOf(calc));
}
});
//Show JFrame
app.setVisible(true);
app.pack();
}
public static void main(String[] args) {
new AppOne_G5();
}
}
要调试未获得预期输出的原因,您可以使用 System.out.println("...");
。例如,如果您要使用以下内容,那么您可以查看问题是否是因为计算错误,或者 numebr1 或 number2 是否已经为零:
calc = number1 + number2;
System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);
一旦您知道问题出在哪里,您就可以进一步返回代码并找出为什么值为零。
正如 Scorfield 提到的,您只需要 calculateButton
上的一个 ActionListener。那是你应该得到用户输入的文本的地方。
我要构建一个简单的 swing UI 应用程序以从单独的 JTextField 获取用户的输入,然后最后单击 Button 执行添加以显示在 JButton 下方的 JTextField 上。
输入字段 (JTextField) 用于接收整数值。
package com.APPOne_G5;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppOne_G5 {
private JPanel calculatorFrame;
private JTextField inputField1;
private JTextField inputField2;
private JButton calculateButton;
private JTextField outputField;
private JLabel inputFieldLabel1;
private JLabel inputFieldLabel2;
private JLabel outputFieldLabel;
private int number1, number2;
private int calc = number1 + number2;
public AppOne_G5() {
inputField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
number1 = Integer.parseInt(inputField1.getText());
}
});
inputField2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
number2 = Integer.parseInt(inputField2.getText());
}
});
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calc = number1 + number2;
outputField.setText(String.valueOf(number1+number2));
}
});
outputField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputField.setEditable(false);
outputField.setText(String.valueOf(calc));
}
});
}
public static void main(String[] args) {
JFrame app = new JFrame("Group 5 AppOne");
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
app.setContentPane(new AppOne_G5().calculatorFrame);
app.setLocationRelativeTo(null);
app.setVisible(true);
app.pack();
}
}
输入要添加的值后我得到零。
This is the GUI form of the application
根据我的评论,您只需要一个在您单击按钮时激活的动作侦听器。这是一个完整的工作示例,说明它如何工作:
public class AppOne_G5 {
private JFrame app = new JFrame("Group 5 AppOne");
private JPanel calculatorFrame = new JPanel();
private JTextField inputField1 = new JTextField("2");
private JTextField inputField2 = new JTextField("5");
private JButton calculateButton = new JButton("Calculate");
private JTextField outputField = new JTextField(" ");
private JLabel inputFieldLabel1 = new JLabel("Enter Integer");
private JLabel inputFieldLabel2 = new JLabel("Enter Integer");;
private JLabel outputFieldLabel = new JLabel("Result");
private int number1, number2;
private int calc;
public AppOne_G5() {
//Add components in order
app.add(calculatorFrame);
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
calculatorFrame.add(inputFieldLabel1);
calculatorFrame.add(inputField1);
calculatorFrame.add(inputFieldLabel2);
calculatorFrame.add(inputField2);
calculatorFrame.add(calculateButton);
calculatorFrame.add(outputFieldLabel);
calculatorFrame.add(outputField);
//Create action listener
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Get the inputs
number1 = Integer.parseInt(inputField1.getText());
number2 = Integer.parseInt(inputField2.getText());
System.out.println("number 1: " + number1 + ", number2: " + number2);
//Calculate the output
calc = number1 + number2;
System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);
//Show the output
outputField.setText(String.valueOf(calc));
}
});
//Show JFrame
app.setVisible(true);
app.pack();
}
public static void main(String[] args) {
new AppOne_G5();
}
}
要调试未获得预期输出的原因,您可以使用 System.out.println("...");
。例如,如果您要使用以下内容,那么您可以查看问题是否是因为计算错误,或者 numebr1 或 number2 是否已经为零:
calc = number1 + number2;
System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);
一旦您知道问题出在哪里,您就可以进一步返回代码并找出为什么值为零。
正如 Scorfield 提到的,您只需要 calculateButton
上的一个 ActionListener。那是你应该得到用户输入的文本的地方。