Java:无法使JFrame工作

时间:2015-07-31 08:55:55

标签: java loops jframe

我对我已经工作了几天的程序有点麻烦。基本上我要做的就是创建一个GUI来显示HTML文件的用户输入。我想要做的是让我的程序循环在frame1的按钮上,根据他们拥有多少机场腿,以获取所有用户输入:机场名称,Lat和Long。任何帮助都是适用的

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

}

1 个答案:

答案 0 :(得分:0)

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

  • 在构造函数的开头一起声明所有变量
  • 使用明确的变量名,例如:避免使用frame,frame1,tf,tf1,tf2
    • 其他人难以理解您的代码
  • 听起来你似乎想为每条腿使用递归,所以我会为每条腿创建一个新的JFrame实例
  • 有了这个想法我认为现场创建实例是个好主意,当你需要它们而不是提前创建所有实例时
  • 我会将两个JFrame分成他们自己的类
  • 我很好奇为什么宽度和高度是静态的和最终的。我之前从未见过,如果这是无意的,我会把它们留作最后的
  • 另外,私有的Listener类,我建议总是以大写字母开始上课,据我所知这个分支也是私有类

提交的代码编辑

  • 正如我建议的使用实例,当用户点击“确定”时。创建第二个JFrame窗口的实例,而不是使其可见
  • 由于您的代码在不同的JFrame中继续存在,因此核心代码有一个主类,leg / speed JFrame有另一个类,机场/ lat / long JFrame有另一个类
  • 我对私人课程不太熟悉,所以我尽量避免让它变得混乱,请原谅任何不良代码
  • 使用JFrame后,JFrame被处理掉了,我建议在玩窗口可见性时创建新实例

            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());
            }
        }
    
  • 我没有碰到机场班,我希望这有帮助!许多代码风格和手段的方式是我的偏好,并且不需要复制任何东西