gpt4 book ai didi

java - 单击 Java 中的按钮时文本区域不打印输入

转载 作者:行者123 更新时间:2023-12-02 05:52:25 25 4
gpt4 key购买 nike

这是我的代码,伙计们。我已经对此进行了审查,但不确定为什么单击 JButton 后它不会在 JTextArea 中输出摄氏温度,而 JButton 应该从 JTextField 接收其输入。

这是代码...

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);
}
}

谢谢大家!

最佳答案

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

关于java - 单击 Java 中的按钮时文本区域不打印输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com