Java JFrame使框架获得两个数字的总和。不工作! [英] Java JFrame Making a Frame to get sum of two numbers. Not Working!
问题描述
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'显示在两个文本字段中输入的两个数字的总和?
< blockquote>您没有在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. Thenew 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 implementation
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); F1.setText("5"); // set 5 in F1 F2.setText("5"); // set 5 in F2 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); } }
这篇关于Java JFrame使框架获得两个数字的总和。不工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!