JLabel.setText() 方法
所以我正在编写一个程序来求解二次方程,除了让 2 个 JLabels(以前为空)显示答案之外,一切都正常,(当用户单击 JButton 时会发生这种情况)
这是整个程序,因为我不知道错误出在哪里。
import java.awt.event.*;
import javax.swing.*;
public class Third implements ActionListener {
//--------------
//Data Members
//--------------
/**
* Top level window
*/
JFrame top;
/**
* Changed into a string by ConvertToDouble(string str);
*/
double a, b, c;
double answer1,answer2;
JTextField inputA, inputB, inputC;
JLabel describeA, describeB, describeC, print1, print2;
JButton submit;
String aa, bb, cc;
String result1, result2;
String strA, strB, strC;
public Third(){
top = new JFrame("Ned's quadratic equation solver");
top.setVisible(true);
top.setLayout(null);
top.setBounds(50,50,250,250);
inputA = new JTextField(12);
inputA.setBounds(100,30,200,25);
inputB = new JTextField(12);
inputB.setBounds(100,105,200,25);
inputC = new JTextField(12);
inputC.setBounds(100,185,200,25);
describeA = new JLabel("Enter A here:");
describeA.setBounds(10,30,200,25);
describeB = new JLabel("Enter B here:");
describeB.setBounds(10,105,200,25);
describeC = new JLabel("Enter C here:");
describeC.setBounds(10,185,200,25);
print1 = new JLabel();
print1.setBounds(15,290,1000,10);
print2 = new JLabel();
print2.setBounds(15,310,1000,10);
submit = new JButton ("WHAT DOES X = ???");
submit.setBounds(50,230,150,25);
submit.addActionListener(this);
top.add(inputA);
top.add(inputB);
top.add(inputC);
top.add(describeA);
top.add(describeB);
top.add(describeC);
top.add(submit);
top.doLayout();
}
public void actionPerformed(ActionEvent event) {
aa = inputA.getText();
bb = inputB.getText();
cc = inputC.getText();
a = convertToDouble(aa);
b = convertToDouble(bb);
c = convertToDouble(cc);
makeAns(a,b,c);
/*
* DEBUG CODE
*
* System.out.println(a);
* System.out.println(b);
* System.out.println(c);
* System.out.println(answer1);
* System.out.println(answer2);
*/
result1 = "x = " + answer1;
result2 = "x = " + answer2;
print1.setText(result1);
print2.setText(result2);
//System.out.println(result1);
top.doLayout();
}
private void makeAns(double x,double y,double z){
answer1 =(-y + Math.sqrt (y*y-4*x*z))/(2*x);
answer2 =(-y - Math.sqrt (y*y-4*x*z))/(2*x);
}
private double convertToDouble (String str) {
Double dubb = new Double(str);
return dubb.doubleValue();
}
}
So i was writing a program to solve a quadratic equation, and everything works apart from when it comes to making 2 JLabels (previously empty) show the answers, (this happens when the user clicks a JButton)
Here is the whole program, because i have no idea where the error is.
import java.awt.event.*;
import javax.swing.*;
public class Third implements ActionListener {
//--------------
//Data Members
//--------------
/**
* Top level window
*/
JFrame top;
/**
* Changed into a string by ConvertToDouble(string str);
*/
double a, b, c;
double answer1,answer2;
JTextField inputA, inputB, inputC;
JLabel describeA, describeB, describeC, print1, print2;
JButton submit;
String aa, bb, cc;
String result1, result2;
String strA, strB, strC;
public Third(){
top = new JFrame("Ned's quadratic equation solver");
top.setVisible(true);
top.setLayout(null);
top.setBounds(50,50,250,250);
inputA = new JTextField(12);
inputA.setBounds(100,30,200,25);
inputB = new JTextField(12);
inputB.setBounds(100,105,200,25);
inputC = new JTextField(12);
inputC.setBounds(100,185,200,25);
describeA = new JLabel("Enter A here:");
describeA.setBounds(10,30,200,25);
describeB = new JLabel("Enter B here:");
describeB.setBounds(10,105,200,25);
describeC = new JLabel("Enter C here:");
describeC.setBounds(10,185,200,25);
print1 = new JLabel();
print1.setBounds(15,290,1000,10);
print2 = new JLabel();
print2.setBounds(15,310,1000,10);
submit = new JButton ("WHAT DOES X = ???");
submit.setBounds(50,230,150,25);
submit.addActionListener(this);
top.add(inputA);
top.add(inputB);
top.add(inputC);
top.add(describeA);
top.add(describeB);
top.add(describeC);
top.add(submit);
top.doLayout();
}
public void actionPerformed(ActionEvent event) {
aa = inputA.getText();
bb = inputB.getText();
cc = inputC.getText();
a = convertToDouble(aa);
b = convertToDouble(bb);
c = convertToDouble(cc);
makeAns(a,b,c);
/*
* DEBUG CODE
*
* System.out.println(a);
* System.out.println(b);
* System.out.println(c);
* System.out.println(answer1);
* System.out.println(answer2);
*/
result1 = "x = " + answer1;
result2 = "x = " + answer2;
print1.setText(result1);
print2.setText(result2);
//System.out.println(result1);
top.doLayout();
}
private void makeAns(double x,double y,double z){
answer1 =(-y + Math.sqrt (y*y-4*x*z))/(2*x);
answer2 =(-y - Math.sqrt (y*y-4*x*z))/(2*x);
}
private double convertToDouble (String str) {
Double dubb = new Double(str);
return dubb.doubleValue();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须先向 GUI 添加一个组件,然后它才能显示任何内容。您在何处将 print1 和 print2 JLabel 添加到 GUI 或任何容器中?
此外,您还需要使用布局管理器而不是空布局和绝对定位,以使 GUI 的编码更加容易。
此外,您还需要在添加所有组件之后在 JFrame 上调用
setVisible(true)
。You've got to add a component to the GUI first before it can display anything. Where do you add your print1 and print2 JLabels to the GUI or to any container for that matter?
Also, you'll want to use layout managers rather than null layout and absolute positioning to make coding your GUI's much easier.
Also, you'll want to call
setVisible(true)
on the JFrame after adding all components.