这有什么错我的Java code? [英] What's wrong with my Java code?

查看:233
本文介绍了这有什么错我的Java code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示我的code;我有显示在提交按钮点击输出问题。起初,我没能在我的内部类使用本地varible,但是当我搜索一些家伙说使用最后它。我做了,但还是没有得到任何输出,这是该按钮后面简​​单的公式。

 进口java.awt中的*。
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.WindowAdapter中;
进口java.awt.event.WindowEvent中;
进口javax.swing.JOptionPane中;公共类FTOC {    公共静态无效的主要(字串[] args){
        帧FRM =新帧();
        标签LB =新标签(Calculater);
        frm.setSize(500,300);
        frm.setVisible(真);
        frm.addWindowListener(新WindowAdapter的(){            公共无效的windowClosing(WindowEvent五){
                System.exit(0);
            }
        });        面板的obj =新面板();
        面板OBJ2 =新面板();
        标签F =新标签(F);
        最终文本字段FT =新的TextField(10);
        标签C =新标签(C);
        文本字段FTC =新的TextField(10);
        obj.setLayout(新的GridLayout(1,1));
        obj.add(F);
        obj.add(英尺);
        obj.add(℃);
        obj.add(FTC);
        最后弦乐SFT = Ft.getText();
        按钮提交=新按钮(计算);
        submit.addActionListener(新的ActionListener(){            公共无效的actionPerformed(ActionEvent的五){                双FTN = Double.parseDouble(SFT);
                double结果=(FTN - 32)* 5/9;
                //System.out.println(Ft);
                JOptionPane.showMessageDialog(NULL,结果);
            }
        });        obj.add(提交);
        obj2.add(OBJ);
        frm.add(OBJ2,BorderLayout.NORTH);
    }
}


解决方案

 最后弦乐SFT = Ft.getText();

这里的问题是你从分配领域中的前值的用户曾经输入的任何文本。

代替把字符串决赛,获得时的动作事件被触发的字段中的文本。

虽然我希望这是一个测试程序,我建议你应该创建你自己的自定义面板(从JPanel的扩展一个类),使窗体元素私有成员。形成在那里你会大大只需将设计并降低你的问题。

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. 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. Form there you will greatly simply your design and reduce your problems

这篇关于这有什么错我的Java code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆