Java: 无法让 JFrame 工作

Java: Can't get JFrame to work

我已经开发了几天的程序遇到了一些问题。基本上我想做的是创建一个 GUI 来显示用户对 HTML 文件的输入。我想要做的是让我的程序根据 frame1 的按钮循环,基于他们有多少机场航段,以便获取所有用户输入:机场名称、纬度和经度。欢迎任何帮助

package project4;

import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;



public class MainClass {

public static final int FRAME_WIDTH = 500;
public static final int FRAME_HEIGHT = 500;

    public static void main(String[] args) { new MainClass();}

    // Fields
        private int legs;
        private double speed;
        private double totTime = 0;
        private double totDist = 0;
        private double lat;
        private double lng;
        private String name;
        Airport user;

    public MainClass()  {

        // Variable Declarations

        // Frame window 1 : Contains Legs and Speed
        JFrame frame = new JFrame("Legs and Speed");
        JPanel panel = new JPanel();
        JLabel labelLegs = new JLabel("Legs:");
        JLabel labelSpeed = new JLabel("Speed:");
        JTextField tf = new JTextField(5); // Leg field
        JTextField tf1 = new JTextField(5); // Speed field
        JButton button = new JButton("Ok");


        JFrame frame1 = new JFrame();
        JLabel label1 = new JLabel("Airport Name:");
        JLabel label2 = new JLabel("Latitude:");
        JLabel label3 = new JLabel("Longitude");
        JPanel panel1 = new JPanel();
        JTextField tf2 = new JTextField(5);
        JTextField tf3 = new JTextField(5);
        JTextField tf4 = new JTextField(5);
        JButton button1 = new JButton("Ok");

            // Add elements to panel and then to frame
            panel.add(labelLegs);
            panel.add(tf); // Leg field
            panel.add(labelSpeed);
            panel.add(tf1); // Speed field
            panel.add(button);
            frame.add(panel);
        frame.setVisible(true);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        class legsAndSpeedListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e) {
                legs = Integer.parseInt(tf.getText());
                speed = Double.parseDouble(tf1.getText());
                    frame.setVisible(false);
                    frame1.setVisible(true);
            }
        }
        ActionListener listener = new legsAndSpeedListener();
        button.addActionListener(listener);

        // Arrays for Latitude and Longitude and Airport Name
        String[] airportName = new String[legs];
        double[] latitudeArr = new double[legs];
        double[] longitudeArr = new double[legs];




            panel1.add(label1);
            panel1.add(tf2);
            panel1.add(label2);
            panel1.add(tf3);
            panel1.add(label3);
            panel1.add(tf4);
            panel1.add(button1);
            frame1.add(panel1);
        frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame1.setSize(FRAME_WIDTH, FRAME_HEIGHT);

            class portItinerary implements ActionListener
            {
                public void actionPerformed(ActionEvent e) {
                    name = tf2.getText();
                    lat = Double.parseDouble(tf3.getText());
                    lng = Double.parseDouble(tf4.getText());
                }
            }
            ActionListener listener1 = new portItinerary();
            button1.addActionListener(listener1);

            frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        for (int i = 0; i < legs; i++)
        {
            latitudeArr[i] = lat;
            longitudeArr[i] = lng;
            airportName[i] = name;
        }
    }
}   





    package project4;
import java.awt.event.*;
import javax.swing.*;

public class Airport extends MainClass {

    public static final double EARTH_RADIUS = 10800 / Math.PI;
    public static final double RAD = Math.PI / 180;

    // Fields
    public static double Lat;
    public static double Long;
    public static String ID;
    public static double Distance;
    public static int legs;

    public Airport()
    {

    }

    public void setWindow()
    {


    }




    // Mutators
    public static void setDistance(double lat1, double long1, double lat2, double long2)
    {
        // Sin converter
        double distance1;
        double sinLat1 = lat1 * RAD;
        double sinLat2 = lat2 * RAD;

        // Cos converter
        double cosLat1 = lat1 * RAD;
        double cosLat2 = lat2 * RAD;
        double cosLong1 = long1 * RAD;
        double cosLong2 = long2 * RAD;


        // Calculation of the great circle distance based on two coordinates in nautical miles
        distance1 = (Math.sin(sinLat1) * Math.sin(sinLat2) + Math.cos(cosLat1) * Math.cos(cosLat2) * Math.cos((cosLong1 - cosLong2)));
        Distance = EARTH_RADIUS * Math.acos(distance1);
    }

    // Accessors
    public static double getLat() {return Lat;}
    public static double getDistance(){return Distance;}
    public static double getLong(){return Long;}
    public static String getID() {return ID;}

}

一些建议,这些是基于偏好

  • 在构造函数的开头一起声明所有变量
  • 使用明确的变量名,例如:避免使用 frame、frame1、tf、tf1、tf2
    • 别人很难理解你的代码
  • 听起来你想对每条腿使用递归,所以我会为每条腿创建一个新的 JFrame 实例
  • 有了这个想法,我相信在需要时当场创建实例是个好主意,而不是让他们提前创建所有实例
  • 我会将两个 JFrames 分开成它们自己的 classes
  • 我很好奇为什么宽度和高度是静态的和最终的。我以前从未见过,如果那是无意的,我会把它们保留为最终版本
  • 此外,私有侦听器 classes,我建议始终以大写字母开头 classes,据我所知,这也会分支到私有 classes

已提交代码编辑

  • 正如我关于使用实例的建议,当用户单击 'ok' 时。创建第二个 JFrame window 的实例,而不是使其可见
  • 由于您的代码在不同的 JFrame 中继续,因此有一个主要的 class 用于核心代码,另一个 class 用于 legs/speed JFrame,另一个 class 用于airport/lat/long JFrame
  • 我对私有classes不是很熟悉,所以我尽量避免弄得一团糟,请原谅任何错误的代码
  • 使用 JFrame 后,JFrame 被处理掉,我建议创建新实例而不是使用 window 可见性

            package airportproj;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        import javax.swing.*;
    
        public class AirportProj {
    
            public static void main(String[] args) { new AirportProj();}
    
            // Fields
            private MainClass mainWindow;
            private LegClass legClass;
            private int legs, currentLeg;
            private double speed;
            private double totTime = 0;
            private double totDist = 0;
            private String[] airportNames;
            private double[] latitudes;
            private double[] longtitudes;
            Airport user;
    
            public AirportProj()  {
                class legsAndSpeedListener implements ActionListener
                {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        legs = mainWindow.getLegs();
                        speed = mainWindow.getSpeed();
                        mainWindow.dispose();
                        airportNames= new String[legs];
                        latitudes = new double[legs];
                        longtitudes = new double[legs];
                        currentLeg = 0;
                        class PortItinerary implements ActionListener
                        {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                airportNames[currentLeg] = legClass.getAirportName();
                                latitudes[currentLeg] = legClass.getLatitude();
                                longtitudes[currentLeg] = legClass.getLongtitude();
                                legClass.dispose();
                                if (++currentLeg < legs) {
                                    legClass = new LegClass(new PortItinerary());
                                }else {
                                    doWhateverYouWantToDoNext();
                                }
                            }
                        }
    
                        legClass = new LegClass(new PortItinerary());
                    }
                }
    
                mainWindow = new MainClass(new legsAndSpeedListener());
            }
    
            public void doWhateverYouWantToDoNext() {
                for (int i = 0; i < legs; i ++) {
                    System.out.println("Airport Name: " + airportNames[i]);
                    System.out.println("Latitude: " + latitudes[i]);
                    System.out.println("Longtitude: " + longtitudes[i]);
                }
            }
        }   
    
        class MainClass extends JFrame {
            private final int FRAME_WIDTH = 500;
            private final int FRAME_HEIGHT = 500;
            private JTextField legInput, speedInput;
    
            public MainClass(ActionListener listener) {
                // Frame window 1 : Contains Legs and Speed
                JPanel container = new JPanel();
                JLabel labelLegs = new JLabel("Legs:");
                JLabel labelSpeed = new JLabel("Speed:");
                legInput = new JTextField(5); // Leg field
                speedInput = new JTextField(5); // Speed field
                JButton button = new JButton("Ok");
                button.addActionListener(listener);
    
                setTitle("Legs and Speed");
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
                // Add elements to panel and then to frame
                container.add(labelLegs);
                container.add(legInput); // Leg field
                container.add(labelSpeed);
                container.add(speedInput); // Speed field
                container.add(button);
                add(container);
                setVisible(true);
            }
    
            public int getLegs() {
                return Integer.parseInt(legInput.getText());
            }
    
            public double getSpeed() {
                return Double.parseDouble(speedInput.getText());
            }
        }
    
        class LegClass extends JFrame {
            private String name;
            private final int FRAME_WIDTH = 500;
            private final int FRAME_HEIGHT = 500;
            private JTextField airportInput, latitudeInput,longtitudeInput;
    
            public LegClass(ActionListener listener) {
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
    
                JLabel airportName = new JLabel("Airport Name:");
                JLabel latitude = new JLabel("Latitude:");
                JLabel longtitude = new JLabel("Longitude");
                JPanel container = new JPanel();
                airportInput = new JTextField(5);
                latitudeInput = new JTextField(5);
                longtitudeInput = new JTextField(5);
                JButton okay = new JButton("Ok");
                okay.addActionListener(listener);
    
                container.add(airportName);// airport
                container.add(airportInput);
                container.add(latitude);  // latitude
                container.add(latitudeInput);
                container.add(longtitude);   // longtitude
                container.add(longtitudeInput);
                container.add(okay);
                add(container);
    
                setVisible(true);
            }
    
            public String getAirportName() {
                return airportInput.getText();
            }
    
            public double getLatitude() {
                return Double.parseDouble(latitudeInput.getText());
            }
    
            public double getLongtitude() {
                return Double.parseDouble(longtitudeInput.getText());
            }
        }
    
  • 我没去机场class,希望对你有帮助!很多代码风格和方法都是我的偏好,我觉得你不需要完全复制任何东西