GUI 读取 JTextField 时遇到问题
我不知道我哪里出了问题,我尝试改变一些东西,但我就是无法让“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();
}
}
I can't figure out where I am going wrong with this, I have tried to changing a few things but I just can't get the "CalculateButtonHandler to work correctly. Sorry put out all this code but last time I wasn't specific enough :S If someone could point me in the right direction that would be great. Thanks.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在解析 sideX 文本,它们是 JLabels,而不是 JTextField。将 CalculateButtonHandler 中的 sideX 替换为 inputX ,一切都会好起来的
You are parsing the sideX text which are the JLabels and not your JTextFields. Replace the sideX by inputX in your
CalculateButtonHandler
and all will be fine1)使用 JFormattedTextField (witt
数字实例) 而不是普通的 JTextField,那么你无法解决从
String
解析Number
实例的问题,2)看起来像此代码解析来自 JLabels 而不是
JTextFields
1) use JFormattedTextField (witt
Number instance
) rather than plain JTextField, then you couldn't ever to solving parsingNumber
instance fromString
,2) looks like this code parse text from JLabels instead of
JTextFields
在您使用 JLabel 获取输入的 CalculateButtonHandler 类中,似乎您需要 JTextField 来获取输入,将该类更改为:
并且从下次开始始终安排作业对于事件调度程序线程,在使用 Swing(如您的情况)时,您的主要方法必须如下所示:
要了解更多关于我正在谈论的内容,请阅读 Swing 中的并发。关于 Swing 非常重要的事情在本章中。
In your
CalculateButtonHandler
class you usingJLabel
to get input, seems like you need JTextField to get input for that, change that class to this :And From next time always Schedule a JOB for the Event Dispatcher Thread, while using Swing like in your case your main method must look like this :
To know more about what I am talking about read Concurrency in Swing. Very Important thing about Swing is on this Chapter.
您还直接比较双打。您不太可能认为这种比较是正确的。运行这个:
You're also directly comparing doubles. It's very unlikely that you will get this comparison as true. Run this: