我试图让代码来计算风寒,但它不工作。我希望计算完成后,能在一个单独的窗口中显示。
另外我还需要程序在输入无效数据或风寒值无效时,出现一个单独的错误对话框,GUI中的文字应该反映出这个问题。而选择文件输入按钮时,应该显示一个文件选择窗口。文件打开错误将用异常和对话框处理。如果文件有效且打开成功,程序将读入数据,计算结果,并将数据和结果以列的形式显示在通过单独的类和Java文件创建的单独窗口中,并绘制温度和风寒值。
代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Scanner;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WindChillCalculations extends JFrame {
private JPanel contentPane;
private static JTextField Fahrenheit_textField;
private static JTextField Mph_textField;
private static JTextField DewPoint_textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WindChillCalculations frame = new WindChillCalculations();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String fahrenheit = Fahrenheit_textField.getText();
String speed = Mph_textField.getText();
String dewpoint = DewPoint_textField.getText();
double t = Double.parseDouble(fahrenheit);
double v = Double.parseDouble(speed);
double d = Double.parseDouble(dewpoint);
//Calculate windchill
double w = 35.74 + (0.6215 * t) - (35.75 * (Math.pow(v,0.16))) + (0.4275 * (t * (Math.pow(v,0.16))));
//Format to keep the windchill to 2 digits after decimal
w = (int)(w*100)/100.0;
}
/**
* Create the frame.
*/
public WindChillCalculations() {
setTitle("Weather Data Program - Wind Chill Calculations");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 491, 383);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setToolTipText("");
panel.setBorder(new LineBorder(Color.WHITE));
panel.setForeground(Color.BLACK);
panel.setBackground(Color.BLACK);
panel.setBounds(2, 4, 470, 338);
contentPane.add(panel);
panel.setLayout(null);
JLabel WindChillCalculationsLabel = new JLabel("Wind Chill Calculations");
WindChillCalculationsLabel.setBounds(146, 6, 137, 15);
WindChillCalculationsLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
panel.add(WindChillCalculationsLabel);
WindChillCalculationsLabel.setForeground(Color.WHITE);
JLabel Fahrenheit_Label = new JLabel("Enter the temperature in degrees Fahrenheit:");
Fahrenheit_Label.setForeground(new Color(255, 140, 0));
Fahrenheit_Label.setBounds(10, 67, 305, 14);
panel.add(Fahrenheit_Label);
Fahrenheit_textField = new JTextField();
Fahrenheit_textField.setBounds(338, 64, 86, 20);
panel.add(Fahrenheit_textField);
Fahrenheit_textField.setHorizontalAlignment(JTextField.RIGHT);
Fahrenheit_textField.setColumns(10);
JLabel Mph_Label = new JLabel("Enter the wind speed in mph:");
Mph_Label.setForeground(new Color(255, 140, 0));
Mph_Label.setBounds(10, 123, 305, 14);
panel.add(Mph_Label);
Mph_textField = new JTextField();
Mph_textField.setColumns(10);
Mph_textField.setBounds(338, 120, 86, 20);
Mph_textField.setHorizontalAlignment(JTextField.RIGHT);
panel.add(Mph_textField);
JLabel DewPoint_Label = new JLabel("Enter the dew point in degrees Fahrenheit:");
DewPoint_Label.setForeground(new Color(255, 140, 0));
DewPoint_Label.setBounds(10, 177, 305, 14);
panel.add(DewPoint_Label);
DewPoint_textField = new JTextField();
DewPoint_textField.setColumns(10);
DewPoint_textField.setBounds(338, 174, 86, 20);
DewPoint_textField.setHorizontalAlignment(JTextField.RIGHT);
panel.add(DewPoint_textField);
JButton FileEntry_btn = new JButton("File Entry");
FileEntry_btn.setBackground(Color.BLACK);
FileEntry_btn.setForeground(new Color(255, 140, 0));
FileEntry_btn.setBounds(39, 283, 89, 23);
panel.add(FileEntry_btn);
JButton Compute_btn = new JButton("Compute");
Compute_btn.setForeground(new Color(255, 140, 0));
Compute_btn.setBackground(Color.BLACK);
Compute_btn.setBounds(301, 283, 89, 23);
panel.add(Compute_btn);
Compute_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
WeatherOutput output = new WeatherOutput();
output.setVisible(true);
}
});
}}
我想这个答案不能满足上位者的要求。 我希望其他人能从中学习。
下面是我对代码的修改。
我使用了一个 JFrame
. 我没有延长 JFrame
. 只有当你想覆盖一个类的方法时,你才会扩展一个Swing组件,或者任何Java类。
我创建了GUI,并在计算风速之前给用户一个机会输入数值。
我取消了所有的绝对定位,使用了 周转式布局管理器. 我创建了一个主 JPanel
和两个下属 JPanel
一个用于放置标签和文本字段,另一个用于放置按钮。 主面板使用了一个 BorderLayout
在标签和文本字段面板上使用了一个 GridBagLayout
和按钮面板使用的是 GridBagLayout
所以按钮会水平展开以填充区域。
我在代码中使用了camelCase变量名。
这是GUI的结果。
我对代码进行了格式化,以便更容易看到方法和类。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class WindChillCalculations {
private JPanel contentPane;
private JTextField fahrenheit_textField;
private JTextField mph_textField;
private JTextField dewPoint_textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new WindChillCalculations();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WindChillCalculations() {
// top, left, bottom, right
Insets topInsets = new Insets(10, 10, 10, 10);
Insets topRightInsets = new Insets(10, 0, 10, 10);
Insets insets = new Insets(0, 10, 10, 10);
Insets rightInsets = new Insets(0, 0, 10, 10);
Color textColor = new Color(255, 140, 0);
JFrame frame = new JFrame();
frame.setTitle("Weather Data Program - "
+ "Wind Chill Calculations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout());
JLabel windChillCalculationsLabel = new JLabel(
"Wind Chill Calculations");
windChillCalculationsLabel.setFont(new Font(
"Tahoma", Font.BOLD, 12));
windChillCalculationsLabel.setForeground(Color.WHITE);
windChillCalculationsLabel.setHorizontalAlignment(
JLabel.CENTER);
contentPane.add(windChillCalculationsLabel,
BorderLayout.BEFORE_FIRST_LINE);
JPanel panel = new JPanel();
panel.setToolTipText("");
panel.setBorder(new LineBorder(Color.WHITE));
panel.setForeground(Color.BLACK);
panel.setBackground(Color.BLACK);
panel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel fahrenheit_Label = new JLabel("Enter the "
+ "temperature in degrees Fahrenheit:");
fahrenheit_Label.setForeground(textColor);
addComponent(panel, fahrenheit_Label, 0, gridy, 1, 1,
topInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
fahrenheit_textField = new JTextField(15);
fahrenheit_textField.setHorizontalAlignment(
JTextField.RIGHT);
addComponent(panel, fahrenheit_textField, 1, gridy++, 1, 1,
topRightInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel mph_Label = new JLabel("Enter the wind "
+ "speed in mph:");
mph_Label.setForeground(textColor);
addComponent(panel, mph_Label, 0, gridy, 1, 1,
insets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
mph_textField = new JTextField(15);
mph_textField.setHorizontalAlignment(JTextField.RIGHT);
addComponent(panel, mph_textField, 1, gridy++, 1, 1,
rightInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel dewPoint_Label = new JLabel("Enter the "
+ "dew point in degrees Fahrenheit:");
dewPoint_Label.setForeground(textColor);
addComponent(panel, dewPoint_Label, 0, gridy, 1, 1,
insets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
dewPoint_textField = new JTextField(15);
dewPoint_textField.setHorizontalAlignment(
JTextField.RIGHT);
addComponent(panel, dewPoint_textField, 1, gridy++, 1, 1,
rightInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
contentPane.add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.BLACK);
buttonPanel.setForeground(Color.BLACK);
buttonPanel.setLayout(new GridBagLayout());
JButton fileEntry_btn = new JButton("File Entry");
fileEntry_btn.setBackground(Color.BLACK);
fileEntry_btn.setForeground(textColor);
addComponent(buttonPanel, fileEntry_btn, 0, 0, 1, 1,
topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton compute_btn = new JButton("Compute");
compute_btn.setForeground(textColor);
compute_btn.setBackground(Color.BLACK);
compute_btn.setBounds(301, 283, 89, 23);
addComponent(buttonPanel, compute_btn, 1, 0, 1, 1,
topRightInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
compute_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fahrenheit = fahrenheit_textField.getText();
String speed = mph_textField.getText();
String dewpoint = dewPoint_textField.getText();
try {
double f = Double.valueOf(fahrenheit);
double s = Double.valueOf(speed);
double d = Double.valueOf(dewpoint);
WindChill windChill = new WindChill(f, s, d);
double answer = windChill.calculateWindChill();
//TODO Create output display
System.out.println(answer);
} catch (NumberFormatException e1) {
e1.printStackTrace();
}
}
});
contentPane.add(buttonPanel, BorderLayout.AFTER_LAST_LINE);
frame.add(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComponent(Container container,
Component component, int gridx, int gridy,
int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(
gridx, gridy, gridwidth, gridheight,
1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public class WindChill {
private final double temperature;
private final double windSpeed;
private final double dewPoint;
public WindChill(double temperature, double windSpeed,
double dewPoint) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.dewPoint = dewPoint;
}
public double calculateWindChill() {
double factor = Math.pow(windSpeed, 0.16);
return 35.74d + (0.6215d * temperature) -
(35.75d * (factor)) +
(0.4275 * (dewPoint * (factor)));
}
}
}