简体   繁体   English

在Java中单击按钮时,文本区域不打印输入

[英]Text Area not printing input when clicked button in Java

Heres my code guys. 这是我的代码人员。 I have reviewed this and not sure why it is not outputting the Celcius temperature in the JTextArea once the JButton is clicked which should receive its input from the JTextField. 我已经回顾过这个并且不确定为什么在点击JButton后它不会在JTextArea中输出Celcius温度,它应该从JTextField接收它的输入。

Heres the code... 继承人代码......

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class C_to_F extends JFrame {

    JPanel layout = new JPanel();
    JLabel title = new JLabel("<html>Welcome to Farenheit to Celcius!</html>");
    JLabel inputtxt = new JLabel("<html>Input Farenheit:</html>");
    static JTextField input = new JTextField (null);
    static JTextArea answer = new JTextArea();
    static JButton submit = new JButton("Submit");

    public static void main(String[] args) {
        JFrame frame = new C_to_F();
        frame.show();   
    }       

    public static void submit() {
        submit.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                input();
            } 
        });
    }

    public static void input() {
        String TextBox = input.getText();
        float farenheit = Float.parseFloat(TextBox);
        for(int i=0;i<=0;i++) {
            double celcius = (farenheit - 32) * 5/9;
            String celciustxt = ("Celcius = " + celcius);
            answer.setText(String.valueOf(celciustxt));             
        }
    }

    public C_to_F() {
        setTitle("Farenheit to Celcius");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(true);
        layout.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 
        layout.setLayout(new GridLayout(8,1));          

        inputtxt.setHorizontalTextPosition(SwingConstants.CENTER);
        title.setHorizontalTextPosition(SwingConstants.CENTER);

        add(layout);

        layout.add(title);
        layout.add(inputtxt);
        layout.add(input);
        layout.add(submit);
        layout.add(answer);

        setVisible(true);
    }    
}

Thanks guys! 多谢你们!

您尚未调用submit()将侦听器添加到按钮

您永远不会调用使用submit按钮注册ActionListener的方法submit

You need to register the Listener on the button, eg: 您需要在按钮上注册监听器,例如:

submit.addActionListener(...);
layout.add(submit);

I have modified your code a little. 我稍微修改了你的代码。 See if this works 看看这是否有效

public class C_to_F extends JFrame{
JPanel layout = new JPanel();
JLabel title = new JLabel("<html>Welcome to Farenheit to Celcius!</html>");
JLabel inputtxt = new JLabel("<html>Input Farenheit:</html>");
static JTextField input = new JTextField (null);
static JTextArea answer = new JTextArea();
static JButton submit = new JButton("Submit");



public static void main(String[] args) {
    JFrame frame = new C_to_F();
    frame.pack();
    frame.setVisible(true);
}


public static void input() {
    String TextBox = input.getText();
    float farenheit = Float.parseFloat(TextBox);
    for(int i=0;i<=0;i++) {
        double celcius = (farenheit - 32) * 5/9;
        String celciustxt = ("Celcius = " + celcius);
        answer.setText(String.valueOf(celciustxt));


    }
}

public C_to_F() {
    setTitle("Farenheit to Celcius");
    setSize(300,300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(true);
    layout.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 
    layout.setLayout(new GridLayout(8,1));


    inputtxt.setHorizontalTextPosition(SwingConstants.CENTER);
    title.setHorizontalTextPosition(SwingConstants.CENTER);

    submit.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
            input();
            SwingUtilities.updateComponentTreeUI(layout);
        } 
    });

    add(layout);

    layout.add(title);
    layout.add(inputtxt);
    layout.add(input);
    layout.add(submit);
    layout.add(answer);

    setVisible(true);


}
}

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

相关问题 单击该按钮时,滚动到文本区域中的某个位置。 - Scroll to a position in a text area when the button clicked. 单击按钮时在文本区域中显示类输出 - Display class output in a text area when button clicked Java-单击按钮时更改标签文本 - Java - Change label text when button is clicked 单击复选框时打印文本 - Printing the text when checkbox is clicked 单击按钮时,如何在文本区域的文本字段中显示文本? - How can I display the text in the text field in the text area when I clicked the button? 将另一个程序的输出打印到java文本区域 - Printing output of another program to a java text area 单击适当的按钮时,Java Activity TextEdit输入无法打印 - Java Activity TextEdit input not printing when I click the appropriate button 使用小程序单击按钮时是否打印图像? - Printing images when a Button is clicked using Applets? 单击时是否有办法使工具提示文本显示在按钮区域的随机位置? - Is there a way to make a tooltip text show in random location in the button area when clicked? 单击按钮时如何使用编辑文本连续获取输入,删除编辑文本中的上一个值 - How to get the input using the edit text continuously when the button is clicked removing the previous value in edit text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM