Witam, mam za zadanie stworzyć program obliczający azymuty, z wykorzystaniem jakiegoś interfejsu.
Program się uruchamia, ale gdy naciskam na przycisk, mający zmienić tekst na przycisku obok na otrzymany wynik z obliczeń, wyskakują w konsoli błędy. Okienko w załączniku.
Oczywiście zaznaczam, że jestem lekko zielona (a nawet bardzo) w tym temacie ^^.
Z góry dziękuję za wszelką pomoc!
Oto kod:
public class obliczenia {
double A=0;
przyciski prz= new przyciski();
double x1=Double.parseDouble(prz.x1.getText());
double y1=Double.parseDouble(prz.y1.getText());
double x2=Double.parseDouble(prz.x2.getText());
double y2=Double.parseDouble(prz.y2.getText());
public obliczenia(){
double cz=Math.atan((y2-y1)/(x2-x1));
if ((y2-y1)>0){
if ((x2-x1)>0) A=cz; if (A>400) A=A-400;
if ((x2-x1)<0) A=200-cz; if (A>400) A=A-400;
}
if ((y2-y1)<0){
if ((x2-x1)<0) A=200+cz; if (A>400) A=A-400;
if ((x2-x1)>0) A=400-cz; if (A>400) A=A-400;
} } }
------------------------------------------------------------------------------------
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class przyciski extends JPanel implements ActionListener{
public static final int HEIGHT = 180;
public static final int WIDTH = 200;
private JButton Button1;
private JButton Button2;
public JTextField x1;
public JTextField y1;
public JTextField x2;
public JTextField y2;
public przyciski(){
Button1 = new JButton("A");
Button2= new JButton("LICZ");
x1 = new JTextField("x1");
y1 = new JTextField("y1");
x2 = new JTextField("x2");
y2 = new JTextField("y2");
Button2.addActionListener(this);
setLayout(new GridLayout(4,2));
setPreferredSize(new Dimension(WIDTH, HEIGHT));
add(x1);
add(y1);
add(x2);
add(y2);
add(Button1);
add(Button2);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if (source == Button2) {
obliczenia obl= new obliczenia();
try{
Button1.setText(Double.toString(obl.A));
}
catch (NumberFormatException e2){}
} }}
--------------------------------------------------------------------------------
import javax.swing.JFrame;
public class okno extends JFrame{
public okno(){
przyciski prz= new przyciski();
add(prz);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); //zmiejsz do rozmiaru panelu
setVisible(true);
}
}
---------------------------------------------------------------------------------
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
new okno();
}}
-----------------------------------------------------------
Oraz imponujący zestaw błędów:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "x1"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at obliczenia.<init>(obliczenia.java:10)
at przyciski.actionPerformed(przyciski.java:51)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)