本文介绍了我在窗口中添加数字有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的问题是我把两个数字放在TextField中,点击结果按钮后它不会给我两个数字的总和! 我不确定我添加到活动中的扫描仪,这只是我对JFrame中读取数字的看法。 这里是我的代码: 包calculator.addition; import javax.swing。*; import java.awt。*; import java.awt.event。*; import java.util.Scanner; 公共类CalculatorAddition { public static int a; public static int b; // globale var public static int s; public CalculatorAddition(){ JFrame f = new JFrame(); JTextField t1 = new JTextField( ); JTextField t2 = new JTextField(); JTextField t3 = new JTextField(); JButton b3 =新的JButton(结果); f.setSize(150,150); t1.addActionListener((ActionEvent arg0) - > {Scanner in = new Scanner(System.in); //我添加Scanner方法来读取用户的数字 a = in.nextInt(); t3.setText(Integer.toString(a));}); t2.addActionListener((ActionEvent arg0) - > // event {Scanner in = new Scanner(System.in); b = in.nextInt() ; }); b3.addActionListener((ActionEvent) - > {s = a + b; t3.setText(Integer.toString(s)); }); JPanel p = new JPanel(); p。 add(t1); p.add(t2); p.add(t3); p.add(b3); f.getContentPane()。add(p,BorderLayout.CENTER); f.show(); } public static void main(String ... arg) {CalculatorAddition p = new CalculatorAddition() ; }} 我的尝试: i将事件更改为新类但它保持不起作用!My problem is whene i put the two number in the TextField , after clicking on the "Result" button it won't give me the total of two numbers !and i m not sure about the Scanner that i add into the event, it s just my opinion about reading numbers in the JFrame .here is my Code : package calculator.addition; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Scanner; public class CalculatorAddition { public static int a ; public static int b ;//globale var public static int s ; public CalculatorAddition(){ JFrame f = new JFrame(); JTextField t1 = new JTextField(" "); JTextField t2 = new JTextField(" "); JTextField t3 = new JTextField(" "); JButton b3 = new JButton("result"); f.setSize(150,150); t1.addActionListener((ActionEvent arg0) -> {Scanner in = new Scanner(System.in);//i add Scanner method to read the numbers from user a = in.nextInt(); t3.setText(Integer.toString(a));}); t2.addActionListener((ActionEvent arg0) -> //event {Scanner in = new Scanner(System.in); b = in.nextInt(); }); b3.addActionListener((ActionEvent) -> {s = a+b ; t3.setText(Integer.toString(s)); }); JPanel p = new JPanel(); p.add(t1); p.add(t2); p.add(t3); p.add(b3); f.getContentPane().add(p,BorderLayout.CENTER); f.show(); } public static void main(String...arg) { CalculatorAddition p = new CalculatorAddition(); }}What I have tried:i was change the event to new class but it keep doesn t work !推荐答案 Scanner sc = new Scanner(System.in);此行用于扫描程序从控制台读取输入。您希望它如何从 JTextField 中读取? 要从 JTextField 中检索值,它应该类似于 This line is used for scanner to read an input from the console. How would you expect it to read from the JTextField ? To retrieve value from JTextField, it should be something likeint a = t1.getText(); 如果你想增加 JTextField 的宽度,请使用 JTextField(5)而不是使用 JTextField()。If you want to increase the width of the JTextField, use JTextField(5) instead, don't use JTextField(" "). 这篇关于我在窗口中添加数字有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 10:09