我有一个应用程序可以处理用 Java 编写的极坐标。我必须为它创建一个 GUI,我决定尝试使用 swing 组件而不是这个名为 BreezyGUI 的 JAR。我遇到的问题是我无法在没有错误的情况下设置框架选项。创建框架、标签和文本字段后,如何开始设计界面?任何建议都会真正帮助我,谢谢!
我在 Eclipse 中收到的错误并不具体。它只是将它突出显示为红色,就像它是一个直接的错过类型。
这是我的应用程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
public class PolarCoords extends JFrame
{
JFrame frameWin = new JFrame();
frameWin.setBounds(100, 200, 120, 120);
frameWin.setTitle("Polar Coordinates");
frameWin.setVisible(true);
frameWin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
private JLabel rLabel;
private JLabel thetaLabel;
private JLabel r2Label;
private JLabel theta2Label;
private JLabel answerLabel;
private static String rString = "R Coordinate";
private static String thetaString = "Theta";
private static String r2String = "R2 Coordinate";
private static String theta2String = "Theta2";
private static String answerString = "Answer";
private JFormattedTextField rField;
private JFormattedTextField thetaField;
private JFormattedTextField r2Field;
private JFormattedTextField theta2Field;
private JFormattedTextField answerField;
public PolarCoords(double rCoord, double theta)
{
return;
}
/**
* Returns theta in (r, theta)
* @param tTheta
* @return
*/
public double tTheta(double theta)
{
return theta;
}
/**
* Returns r coordinate in (r, theta)
* @param rCoord
* @return
*/
public double rRCoord(double rCoord)
{
return rCoord;
}
public Object magnitude()
{
double r1 = Double.valueOf(rField.getText());
double theta1 = Double.valueOf(thetaField.getText());
double r2 = 0;
double theta2 = 0;
double distance = Math.sqrt(Math.pow(r1, 2)) + Math.sqrt(Math.pow(r2, 2) - 2*(r1*r2) * Math.cos(theta1 - theta2));
return distance;
}
public Double angleFromOrigin()
{
double theta1 = Double.valueOf(thetaField.getText());
double theta2 = Double.valueOf(theta2Field.getText());
double answer = theta2 - theta;
return answer;
}
public Object distanceFromPoint()
{
// Distance formula Ã(r1)^2 + (r2)^2 - 2(r1)(r2) cos ( degree1 - degree2 )
double r1 = Double.valueOf(rField.getText());
double theta1 = Double.valueOf(thetaField.getText());
double r2 = Double.valueOf(r2Field.getText());
double theta2 = Double.valueOf(theta2Field.getText());
double distance = Math.sqrt(Math.pow(r1, 2)) + Math.sqrt(Math.pow(r2, 2) - 2*(r1*r2) * Math.cos(theta1 - theta2));
return distance;
}
public PolarCoords addPoints()
{
double r1 = Double.valueOf(rField.getText());
double theta1 = Double.valueOf(thetaField.getText());
double r2 = Double.valueOf(r2Field.getText());
double theta2 = Double.valueOf(theta2Field.getText());
double x = r1 * Math.cos(theta1);
double y = r1 * Math.sin(theta1);
double x2 = r2 * Math.cos(theta1);
double y2 = r2 * Math.sin(theta2);
double xAnswer = x2 - x;
double yAnswer = y2 - y;
double answer = Math.atan2(xAnswer, yAnswer);
PolarCoords polarAnswer = new PolarCoords(xAnswer, yAnswer);
return polarAnswer;
}
public static void main(String[] args)
{
Frame f = new Frame();
System.out.print("Visible = True");
}
}