本文介绍了Java JFrame使框架获得两个数字的总和。不工作!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! package com.testing.Test; import java.awt。*; import javax.swing。*; public class Test扩展JFrame { JLabel no1 = new JLabel(Number 1); JLabel no2 = new JLabel(Number 2 ); JLabel sum = new JLabel(Sum:,JLabel.CENTER); JTextField F1 = new JTextField(5); JTextField F2 =新的JTextField(5); JLabel no3 =新的JLabel(); public Test(){ super(Test); Container container = getContentPane(); container.setLayout(new FlowLayout( )); container.add(no1); container.add(F1); container.add(no2); container.add(F2); container.add(sum); container.add(no3); int n1 = Integer.parseInt((F1.getText())); int n2 = Integer.parseInt((F2.getText())); int no4 = n1 + n2; String s1 = String.valueOf(no4); no3.setText(s1); } public static void main(String []算法测试=新测试(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setSize(500, 400); test.setVisible(true); } } 我应该做些什么更改,以便标签'no3'显示在两个文本字段中输入的两个数字的总和?解决方案 您没有在JTextField中设置任何值。所以我添加了两行让你看到结果。 新的JTextField(5); 不用于设置字段值5. public JTextField(int columns) 构造具有指定列数的新空TextField。创建默认模型,并将初始字符串设置为null。 参数: columns - 用于计算首选宽度的列数;如果列设置为零,则首选宽度将是组件实现自然产生的任何结果 import java .awt *。 import javax.swing。*; 公共类测试扩展JFrame { JLabel no1 = new JLabel(Number 1); JLabel no2 =新JLabel(2号); JLabel sum = new JLabel(Sum:,JLabel.CENTER); JTextField F1 = new JTextField(5); JTextField F2 = new JTextField(5); JLabel no3 = new JLabel(); public Test(){ super(Test); Container container = getContentPane(); container.setLayout(new FlowLayout()); container.add(no1); container.add(F1); container.add(no2); container.add(F2); container.add(sum); container.add(no3); F1.setText(5); //在F1 F2.setText(5)中设置5; //在F2 中设置5 int n1 = Integer.parseInt((F1.getText())); // 5 int n2 = Integer.parseInt((F2.getText())); // 5 int no4 = n1 + n2; // 10 String s1 = String.valueOf(no4); no3.setText(s1); } public static void main(String [] args){ Test test = new Test(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setSize(500,400); test.setVisible(true); } } package com.testing.Test;import java.awt.*;import javax.swing.*;public class Test extends JFrame { JLabel no1 = new JLabel("Number 1"); JLabel no2 = new JLabel("Number 2"); JLabel sum = new JLabel("Sum:",JLabel.CENTER); JTextField F1 = new JTextField(5); JTextField F2 = new JTextField(5); JLabel no3 = new JLabel(); public Test() { super("Test"); Container container = getContentPane(); container.setLayout(new FlowLayout()); container.add(no1); container.add(F1); container.add(no2); container.add(F2); container.add(sum); container.add(no3); int n1 = Integer.parseInt((F1.getText())); int n2 = Integer.parseInt((F2.getText())); int no4 = n1 + n2; String s1 = String.valueOf(no4); no3.setText(s1); }public static void main(String[] args) {Test test = new Test();test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);test.setSize(500, 400);test.setVisible(true);}}What changes should I make to so that the label 'no3' shows sum of the two numbers entered in the two textfields? 解决方案 you didn't set any value in JTextField. so I added two lines to let you see the results. The new JTextField(5); is not used to set the field value 5.public JTextField(int columns)Constructs a new empty TextField with the specified number of columns. A default model is created and the initial string is set to null.Parameters:columns - the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementationimport java.awt.*;import javax.swing.*;public class Test extends JFrame {JLabel no1 = new JLabel("Number 1");JLabel no2 = new JLabel("Number 2");JLabel sum = new JLabel("Sum:", JLabel.CENTER);JTextField F1 = new JTextField(5);JTextField F2 = new JTextField(5);JLabel no3 = new JLabel();public Test() {super("Test");Container container = getContentPane();container.setLayout(new FlowLayout());container.add(no1);container.add(F1);container.add(no2);container.add(F2);container.add(sum);container.add(no3);F1.setText("5"); // set 5 in F1F2.setText("5"); // set 5 in F2int n1 = Integer.parseInt((F1.getText())); // 5int n2 = Integer.parseInt((F2.getText())); // 5int no4 = n1 + n2; // 10String s1 = String.valueOf(no4);no3.setText(s1);}public static void main(String[] args) {Test test = new Test();test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);test.setSize(500, 400);test.setVisible(true);}} 这篇关于Java JFrame使框架获得两个数字的总和。不工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 16:10
查看更多