1

我有一个应用程序可以处理用 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");
    }


}
4

3 回答 3

2

为什么被frameWin注释掉了,你还在后面的行中使用???你的意思是使用this?该部分应该在您的构造函数中。

此外,setvisible应该是在将所有组件添加到框架之后的最后一件事。

尝试:

public PolarCoords(double rCoord, double theta) //u'r not even using rCoord and theta
{
    this.setBounds(100, 200, 120, 120);
    this.setTitle("Polar Coordinates");
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setVisible(true);
}
于 2012-09-30T18:08:42.100 回答
2

您试图将 Java 语句赤裸裸地放在类中,在构造函数或方法之外,这是行不通的。考虑将这些语句放在您的构造函数中。更重要的是,在尝试进行 Swing GUI 编码之前,您应该了解 Java 教程的介绍。

此外,您忽略了代表您的this对象的 JFrame 并试图创建一个完全不相关的 JFrame 对象。不要那样做。摆脱 frameWin 变量,因为它只会误导你。

再次阅读 Java 的基础知识,因为这将极大地帮助您。

请查看Java 教程真正的大索引

于 2012-09-30T18:07:26.390 回答
2
 //JFrame frameWin = new JFrame();
    frameWin.setBounds(100, 200, 120, 120);
    frameWin.setTitle("Polar Coordinates");
    frameWin.setVisible(true);
    frameWin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;

应该在一个方法里面。当你扩展 JFrame 时,它​​必须是

setBounds(100, 200, 120, 120);
 setTitle("Polar Coordinates");
 setVisible(true);
 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;

你可以这样做:

public class PolarCoords extends JFrame
{
   public PolarCoords()
   {
     setBounds(100, 200, 120, 120);
     setTitle("Polar Coordinates");
     setVisible(true);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;
   }
}

或者

public class PolarCoords extends JFrame
{
   public PolarCoords()
   {
     Init();
   }
   public void Init()
   {
     setBounds(100, 200, 120, 120);
     setTitle("Polar Coordinates");
     setVisible(true);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ;
   }
}
于 2012-09-30T18:07:58.653 回答