简体   繁体   English

我的Java代码有什么问题?

[英]What's wrong with my Java code?

I am showing my code; 我正在显示我的代码; I am having problem with to display an output on submit button click. 我在提交按钮单击时显示输出有问题。 At first, I was not able to use local varible in my inner class, but when I search some guy said use final with it. 起初,我无法在内部类中使用局部变量,但是当我搜索某个人时说可以将final与它一起使用。 I did, but still not getting any output this is simple formula behind this button. 我做到了,但仍然没有得到任何输出,这是此按钮后面的简单公式。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;

public class FtoC {

    public static void main(String[] args) {
        Frame frm = new Frame();
        Label lb = new Label("Calculater");
        frm.setSize(500, 300);
        frm.setVisible(true);
        frm.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Panel obj = new Panel();
        Panel obj2 = new Panel();
        Label F = new Label("F");
        final TextField Ft = new TextField(10);
        Label C = new Label("C");
        TextField Ftc = new TextField(10);
        obj.setLayout(new GridLayout(1, 1));
        obj.add(F);
        obj.add(Ft);
        obj.add(C);
        obj.add(Ftc);
        final String sFt = Ft.getText();
        Button submit = new Button("Calculate");
        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                double Ftn = Double.parseDouble(sFt);
                double result = (Ftn - 32) * 5 / 9;
                //System.out.println(Ft);
                JOptionPane.showMessageDialog(null, result);
            }
        });

        obj.add(submit);
        obj2.add(obj);
        frm.add(obj2, BorderLayout.NORTH);
    }
}
final String sFt=Ft.getText();

The problem here is your assigning the value from the field BEFORE the user has ever entered any text. 这里的问题是您在用户输入任何文本之前从字段中分配值。

Instead of making the String final, get the text from the field when the action event is fired. 触发动作事件时,不要从字符串中获取最终文本,而是从字段中获取文本。

While I hope this is a test program, I'd suggest that you should create your self a custom panel (a class that extends from JPanel), make the form elements private members. 虽然我希望这是一个测试程序,但我建议您创建一个自定义面板(自JPanel扩展的类),并将表单元素设为私有成员。 Form there you will greatly simply your design and reduce your problems 在那里形成表格,您将可以极大地简化设计并减少问题

You don't want the value of the Ft widget right after construction, don't you? 您不希望在构造后立即使用Ft小部件的值,不是吗? You want, whatever is there when the button is clicked, right? 您想要什么,单击按钮时有什么,对吧? So, move the 因此,移动

String sFt=Ft.getText()

line into the action listener. 进入动作监听器。

In addition, pack() your frame and call setVisible() last : 另外,将框架pack()最后调用setVisible()

public static void main(String[] args) {
    ...
    frm.add(obj2, BorderLayout.NORTH);
    frm.pack();
    // frm.setSize(500, 300); // optional
    frm.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM