java简单编程
发布网友
我来回答
共4个回答
热心网友
代码如下;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class App extends JFrame {
private JTextField txtNum1;
private JTextField txtNum2;
private JTextField txtResult;
public App() {
this.setSize(500, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(new JLabel("计算"));
txtNum1 = new JTextField();
txtNum1.setPreferredSize(new Dimension(100, 25));
this.add(txtNum1);
this.add(new JLabel("+"));
txtNum2 = new JTextField();
txtNum2.setPreferredSize(new Dimension(100, 25));
this.add(txtNum2);
JButton btnCalc = new JButton("=");
btnCalc.addActionListener(e -> {
if (txtNum1.getText() == "" ||
txtNum2.getText() == "") {
JOptionPane.showMessageDialog(this,"请输入正确的数字。");
}
try {
int num1 = Integer.parseInt(txtNum1.getText());
int num2 = Integer.parseInt(txtNum2.getText());
txtResult.setText(Integer.toString(num1 + num2));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,"请输入正确的数字。");
}
});
this.add(btnCalc);
txtResult = new JTextField();
txtResult.setPreferredSize(new Dimension(100, 25));
txtResult.setEditable(false);
this.add(txtResult);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
热心网友
php的话我会 A.A
热心网友
你邮箱是多少,我发给你我做的源码
热心网友
Java_007_Java_编程规范(命名规则)_上