我无法弄清楚我的错误在哪里,我试图改变一些事情,但我无法让“CalculateButtonHandler正常工作。抱歉把所有这些代码都拿出去但是上次我没有足够具体:S如果有人能指出我的方向很好,那就太棒了。谢谢。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// some kind of problem with Calculate button handler (LINE 78
public class Program5 extends JFrame
{
// Setting up for the program
private JLabel side1, side2, side3, str, result;
private JButton calculate, endProgram;
private JTextField input1, input2, input3;
private CalculateButtonHandler calcHandler;
private EndProgramButtonHandler endHandler;
private Container pane;
private static final int WIDTH = 500;
private static final int HEIGHT = 350;
// constructor
public Program5()
{
// create labels
side1 = new JLabel ("Triangle's Longest Side: ", SwingConstants.CENTER);
side2 = new JLabel ("Triangle's Next Side: ", SwingConstants.CENTER);
side3 = new JLabel ("Triangle's Last Side: ", SwingConstants.CENTER);
result = new JLabel ("", SwingConstants.CENTER);
str = new JLabel ("Is the Triangle a right Triangle?",SwingConstants.CENTER);
// create text fields
input1 = new JTextField ("", 60);
input1.setHorizontalAlignment(JTextField.CENTER);
input2 = new JTextField ("", 60);
input2.setHorizontalAlignment(JTextField.CENTER);
input3 = new JTextField ("", 60);
input3.setHorizontalAlignment(JTextField.CENTER);
// create buttons
calculate = new JButton ("Calculate");
calcHandler = new CalculateButtonHandler ();
calculate.addActionListener(calcHandler);
endProgram = new JButton ("Exit");
endHandler = new EndProgramButtonHandler();
endProgram.addActionListener(endHandler);
// Set title of Window
setTitle ("Right Triangle Tester");
//Get Container
pane = getContentPane();
// set Layout
pane.setLayout (new GridLayout (5, 2));
// place the components in the pane
pane.add(side1);
pane.add(input1);
pane.add(side2);
pane.add(input2);
pane.add(side3);
pane.add(input3);
pane.add(str);
pane.add(result);
pane.add(calculate);
pane.add(endProgram);
// set size of the window and display it
setSize (WIDTH, HEIGHT);
setVisible (true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double num1, num2, num3;
num1 = Double.parseDouble(side1.getText());
num2 = Double.parseDouble(side2.getText());
num3 = Double.parseDouble(side3.getText());
if ((num1*num1) == ((num2*num2)+(num3*num3)))
{
result.setText("YES");
}
else
{
result.setText("NO");
}
}
}
private class EndProgramButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Program5 myObject = new Program5();
}
}
答案 0 :(得分:2)
1)使用JFormattedTextField(witt Number instance
)而非普通JTextField,然后您无法解决Number
中解析String
实例的问题,
2)看起来这段代码解析了JLabels而不是JTextFields
答案 1 :(得分:2)
您正在解析作为JLabel而不是JTextField的sideX文本。用CalculateButtonHandler
中的inputX替换sideX,一切都会好的
答案 2 :(得分:2)
在使用CalculateButtonHandler
获取输入的JLabel
课程中,您似乎需要JTextField来获取输入,将该类更改为:
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double num1, num2, num3;
num1 = Double.parseDouble(input1.getText());
num2 = Double.parseDouble(input2.getText());
num3 = Double.parseDouble(input3.getText());
if ((num1*num1) == ((num2*num2)+(num3*num3)))
{
result.setText("YES");
}
else
{
result.setText("NO");
}
}
}
从下次开始,总是为Event Dispatcher Thread安排一个JOB,而在你的情况下使用Swing,你的main方法必须如下所示:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// some kind of problem with Calculate button handler (LINE 78
public class Program5 extends JFrame
{
// Setting up for the program
private JLabel side1, side2, side3, str, result;
private JButton calculate, endProgram;
private JTextField input1, input2, input3;
private CalculateButtonHandler calcHandler;
private EndProgramButtonHandler endHandler;
private Container pane;
private static final int WIDTH = 500;
private static final int HEIGHT = 350;
// constructor
public Program5()
{
// create labels
side1 = new JLabel ("Triangle's Longest Side: ", SwingConstants.CENTER);
side2 = new JLabel ("Triangle's Next Side: ", SwingConstants.CENTER);
side3 = new JLabel ("Triangle's Last Side: ", SwingConstants.CENTER);
result = new JLabel ("", SwingConstants.CENTER);
str = new JLabel ("Is the Triangle a right Triangle?",SwingConstants.CENTER);
// create text fields
input1 = new JTextField ("",60);
input1.setHorizontalAlignment(JTextField.CENTER);
input2 = new JTextField ("",60);
input2.setHorizontalAlignment(JTextField.CENTER);
input3 = new JTextField ("",60);
input3.setHorizontalAlignment(JTextField.CENTER);
// create buttons
calculate = new JButton ("Calculate");
calcHandler = new CalculateButtonHandler ();
calculate.addActionListener(calcHandler);
endProgram = new JButton ("Exit");
endHandler = new EndProgramButtonHandler();
endProgram.addActionListener(endHandler);
// Set title of Window
setTitle ("Right Triangle Tester");
//Get Container
pane = getContentPane();
// set Layout
pane.setLayout (new GridLayout (5,2));
// place the components in the pane
pane.add(side1);
pane.add(input1);
pane.add(side2);
pane.add(input2);
pane.add(side3);
pane.add(input3);
pane.add(str);
pane.add(result);
pane.add(calculate);
pane.add(endProgram);
// set size of the window and display it
setSize (WIDTH, HEIGHT);
se1tVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double num1, num2, num3;
num1 = Double.parseDouble(input1.getText());
num2 = Double.parseDouble(input2.getText());
num3 = Double.parseDouble(input3.getText());
if ((num1*num1) == ((num2*num2)+(num3*num3)))
{
result.setText("YES");
}
else
{
result.setText("NO");
}
}
}
private class EndProgramButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
// Here this is used to attain Concurrency in Swing.
// So that if there is a need to update the GUI,
// that can be done without any difficulty or freezing the GUI.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Program5 myObject = new Program5();
}
});
}
}
要了解更多关于我所说的内容,请阅读Concurrency in Swing。关于Swing非常重要的是本章。
答案 3 :(得分:1)
你也直接比较双打。您不太可能将此比较视为真实。运行这个:
double num1,num2,num3;
num1=2.8284271247461903; //== Math.sqrt(8)
num2=2.0; num3=2.0;
if ((num1*num1) == ((num2*num2)+(num3*num3)))
System.out.println ("yes");
else System.out.println ("no " + num1*num1 + " " + (num2*num2 + num3*num3));
}