继承我的代码人员。我已经回顾过这个并且不确定为什么在点击JButton后它不会在JTextArea中输出Celcius温度,它应该从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);
}
}
谢谢你们!
答案 0 :(得分:1)
您尚未调用submit()
将听众添加到按钮
答案 1 :(得分:1)
您永远不会调用submit
方法ActionListener
和submit
按钮
答案 2 :(得分:0)
您需要在按钮上注册监听器,例如:
submit.addActionListener(...);
layout.add(submit);
答案 3 :(得分:0)
我稍微修改了你的代码。看看是否有效
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);
}
}