gpt4 book ai didi

java - 计算器编码错误。请更正

转载 作者:行者123 更新时间:2023-12-01 17:35:34 25 4
gpt4 key购买 nike

我曾尝试使用 Java 中的 GUI 编写一个简单的计算器,但被这段代码困住了,并反复收到“输入有效数字”的消息。我愿意接受建议。建议我的代码中可能的更正。我认为我错误地使用了 java 的 try 和 check 异常功能。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.SwingConstants;

public class FIRSTCALC {

private JFrame frame;
private JTextField txtfield1;
private JTextField txtfield2;
private JTextField textfieldans;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FIRSTCALC window = new FIRSTCALC();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public FIRSTCALC() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

txtfield1 = new JTextField();
txtfield1.setHorizontalAlignment(SwingConstants.LEFT);
txtfield1.setText("ENTER NUMBER 1 : ");
txtfield1.setBounds(28, 11, 178, 68);
frame.getContentPane().add(txtfield1);
txtfield1.setColumns(10);

txtfield2 = new JTextField();
txtfield2.setHorizontalAlignment(SwingConstants.LEFT);
txtfield2.setText("ENTER NUMBER 2 : ");
txtfield2.setBounds(228, 11, 175, 68);
frame.getContentPane().add(txtfield2);
txtfield2.setColumns(10);

JButton btnNewButton = new JButton("ADD");
btnNewButton.setToolTipText("TO ADD NUMBERS");
btnNewButton.setFont(new Font("Sitka Text", Font.BOLD, 16));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num1,num2,sum;
try
{
num1=Integer.parseInt(txtfield1.getText());
num2=Integer.parseInt(txtfield2.getText());
sum=num1+num2;
textfieldans.setText(Integer.toString(sum));
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "ENTER VALID NUMBER");
}
}
});
btnNewButton.setBounds(61, 121, 120, 42);
frame.getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton("SUBTRACT");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num1,num2,sum;
try
{
num1=Integer.parseInt(txtfield1.getText());
num2=Integer.parseInt(txtfield2.getText());
sum=num1-num2;
textfieldans.setText(Integer.toString(sum));
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "ENTER VALID NUMBER");
}
}
});
btnNewButton_1.setToolTipText("TO SUBTRACT NUMBERS");
btnNewButton_1.setFont(new Font("Sitka Text", Font.BOLD, 16));
btnNewButton_1.setBounds(251, 121, 128, 42);
frame.getContentPane().add(btnNewButton_1);

JLabel lblNewLabel = new JLabel("THE ANSWER IS :");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Sitka Text", Font.BOLD, 18));
lblNewLabel.setBounds(28, 192, 193, 58);
frame.getContentPane().add(lblNewLabel);

textfieldans = new JTextField();
textfieldans.setBounds(251, 196, 109, 48);
frame.getContentPane().add(textfieldans);
textfieldans.setColumns(10);
}

}

最佳答案

您显然会遇到异常,因为您正在将输入的数字附加或添加到已包含在其中一个 JTextField 中的文本中。当尝试使用 Integer.parseInt() 方法将 JTextField 中包含的字符串解析为整数值时,这会生成 NumberFormatException 。如果提供除数字之外的任何内容,则将引发此异常。您只是让异常显示一个消息框,指示用户“输入有效数字”

如果您想将文本保留在文本字段组件中,请应用焦点监听器来突出显示组件中当前的文本,以便在用户输入数字时它会被覆盖。一般来说,这个前置文本是浅灰色的。这是一个例子:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class FIRSTCALC {

private JFrame frame;
private JTextField txtfield1;
private JTextField txtfield2;
private JTextField textfieldans;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FIRSTCALC window = new FIRSTCALC();
window.frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public FIRSTCALC() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setAlwaysOnTop(true);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

txtfield1 = new JTextField("Enter First Number");
txtfield1.setForeground(Color.lightGray);
// Add a Focus Listener to txtfield1
txtfield1.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
txtfield1.selectAll();
}

@Override
public void focusLost(FocusEvent e) {
txtfield1.setForeground(Color.black);
}
});
txtfield1.setHorizontalAlignment(SwingConstants.CENTER);
txtfield1.setBounds(28, 11, 178, 68);
txtfield1.setColumns(10);
frame.getContentPane().add(txtfield1);

txtfield2 = new JTextField("Enter Second Number");
txtfield2.setForeground(Color.lightGray);
// Add a Focus Listener to txtfield2
txtfield2.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
txtfield2.selectAll();
}

@Override
public void focusLost(FocusEvent e) {
txtfield2.setForeground(Color.black);
}
});
txtfield2.setHorizontalAlignment(SwingConstants.CENTER);
txtfield2.setBounds(228, 11, 175, 68);
txtfield2.setColumns(10);
frame.getContentPane().add(txtfield2);

JButton btnNewButton = new JButton("ADD");
btnNewButton.setToolTipText("TO ADD NUMBERS");
btnNewButton.setFont(new Font("Sitka Text", Font.BOLD, 16));
btnNewButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int num1, num2, sum;
try {
num1 = Integer.parseInt(txtfield1.getText());
num2 = Integer.parseInt(txtfield2.getText());
sum = num1 + num2;
textfieldans.setText(Integer.toString(sum));
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "<html><center>The following formula:<br><br>"
+ "<font size='4' color=red>" + txtfield1.getText() +
"</font> + <font size='4' color=red>"+ txtfield2.getText() +
"</font><br><br>contains invalid digits!</center></html>");
}
}
});
btnNewButton.setBounds(61, 121, 120, 42);
frame.getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton("SUBTRACT");
btnNewButton_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int num1, num2, sum;
try {
num1 = Integer.parseInt(txtfield1.getText());
num2 = Integer.parseInt(txtfield2.getText());
sum = num1 - num2;
textfieldans.setText(Integer.toString(sum));
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "<html><center>The following formula:<br><br>"
+ "<font size='4' color=red>" + txtfield1.getText() +
"</font> - <font size='4' color=red>"+ txtfield2.getText() +
"</font><br><br>contains invalid digits!</center></html>");
}
}
});
btnNewButton_1.setToolTipText("TO SUBTRACT NUMBERS");
btnNewButton_1.setFont(new Font("Sitka Text", Font.BOLD, 16));
btnNewButton_1.setBounds(251, 121, 128, 42);
frame.getContentPane().add(btnNewButton_1);

JLabel lblNewLabel = new JLabel("THE ANSWER IS :");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Sitka Text", Font.BOLD, 18));
lblNewLabel.setBounds(28, 192, 193, 58);
frame.getContentPane().add(lblNewLabel);

textfieldans = new JTextField();
textfieldans.setHorizontalAlignment(SwingConstants.CENTER);
textfieldans.setBounds(251, 196, 109, 48);
textfieldans.setColumns(10);
frame.getContentPane().add(textfieldans);
frame.setLocationRelativeTo(null);

}

}

关于java - 计算器编码错误。请更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61048255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com