我的天气数据java程序在eclipse中出现问题

我的天气数据java程序在eclipse中出现问题,java,Java,我正在尝试获取计算windchill的代码,但它不起作用。我希望计算完成后显示在单独的窗口中 此外,我需要程序有一个单独的错误对话框出现时,无效的数据输入或风寒是无效的,在GUI中的文本应该反映问题。选择“文件输入”按钮时,应显示文件选择窗口。文件打开错误将通过异常和对话框进行处理。如果文件有效并成功打开,程序将读取数据,计算结果,并在通过单独的类和Java文件创建的单独窗口中以列形式显示数据和结果,并绘制温度和风寒值 代码: } }我认为这个答案不能满足OP的要求。我希望其他人能从中学习 下面

我正在尝试获取计算windchill的代码,但它不起作用。我希望计算完成后显示在单独的窗口中

此外,我需要程序有一个单独的错误对话框出现时,无效的数据输入或风寒是无效的,在GUI中的文本应该反映问题。选择“文件输入”按钮时,应显示文件选择窗口。文件打开错误将通过异常和对话框进行处理。如果文件有效并成功打开,程序将读取数据,计算结果,并在通过单独的类和Java文件创建的单独窗口中以列形式显示数据和结果,并绘制温度和风寒值

代码:

}
}我认为这个答案不能满足OP的要求。我希望其他人能从中学习

下面是我对代码所做的更改

  • 我使用了
    JFrame
    。我没有扩展
    JFrame
    。当您想要覆盖其中一个类方法时,您只能扩展Swing组件或任何Java类

  • 我创建了GUI,让用户有机会在计算风速之前键入值

  • 我消除了所有的绝对定位,并使用了。我创建了一个主
    JPanel
    和两个从属
    JPanel
    s,一个用来保存标签和文本字段,另一个用来保存按钮。主面板使用
    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)));
            }
    
        }
    }
    
    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)));
            }
    
        }
    }