问题描述
我正在研究几天的程序遇到了麻烦。 基本上,我想做的是创建一个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;}
}
1楼
一些建议,这些是基于偏好的
- 在构造函数的开头一起声明所有变量
-
使用明确的变量名,例如:避免frame,frame1,tf,tf1,tf2
- 别人很难理解您的代码
- 听起来您正在寻找对每条腿使用递归的方法,所以我将为每条腿创建一个新的JFrame实例
- 考虑到这一点,我认为在需要时就地创建实例而不是事先创建所有实例是一个好主意
- 我将两个JFrame分为各自的类
- 我很好奇为什么宽度和高度都是静态的和最终的。 我以前从未见过,如果那是无意的,我会把它们留在最后
- 另外,就私人侦听器类而言,我建议始终以大写字母开头的类,因为据我所知,这也会分支到私人类
提交代码编辑
- 正如我建议的有关使用实例的建议一样,当用户单击“确定”时。 创建第二个JFrame窗口的实例,而不是使其可见
- 由于您的代码跨不同的JFrame继续,因此核心代码有一个主类,腿部/速度JFrame是一个主类,airport / 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()); } }
我没有接触过机场课程,希望对您有所帮助! 我偏爱很多代码风格和方法,不需要完全复制任何内容