2016-04-28 39 views
0
package Unit2Exam; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Unit2Exam { 

    static int r; 
    static int g; 
    static int b; 
    static int rb; 
    static int rg; 
    static int on; 
    static int rr; 
    static int i; 
    public static JTextField field1; 
    public static JTextField field2; 
    public static JTextField field3; 

    public static void main(String[] args) { 
     //This is my constructor for the Math class, it allows me to import methods from that class 
     //final Math a = new Math(); 
     final Unit2Exam Calculator = new Unit2Exam(); 

     //Makes a font 
     Font font = new Font("Verdana", Font.PLAIN, 12); 

     GridBagConstraints grid = new GridBagConstraints(); 
     //This creates the JFRAMES 
     JFrame frame = new JFrame("Free Fall Application"); 
     //This sets the title 
     JPanel panel = new JPanel(new GridBagLayout()); 
     //makes and adds the panel 
     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     grid.fill = GridBagConstraints.BOTH; 

     JFrame colors = new JFrame("Colors"); 
     JPanel colorPanel = new JPanel(new GridBagLayout()); 
     colors.add(colorPanel); 
     colors.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     grid.fill = GridBagConstraints.BOTH; 

     //causes the jbuttons to fill the borders (to make it look better) 
     grid.weightx = 1; 
     grid.weighty = 1; 
     // frame.setSize(new Dimension(200, 200)); 
     frame.setResizable(false); 
     colors.setResizable(false); 
     colors.setVisible(true); 
     colorPanel.setVisible(true); 
     frame.setVisible(true); 
     panel.setVisible(true); 

     //This is where I implement all of the J stuff (i.e. Buttons, Textfields, and JLabels) 
     JLabel title = new JLabel("Jonathan Kipper"); 
     JLabel label1 = new JLabel("Initial Velocity (m/s):"); 
     JLabel label2 = new JLabel("Time (Seconds):"); 
     JLabel label3 = new JLabel("Distance (meters):"); 
     JTextField field1 = new JTextField(6); 
     JTextField field2 = new JTextField(6); 
     JTextField field3 = new JTextField(6); 
     JButton button1 = new JButton("Calculate"); 
     JButton button2 = new JButton("Clear"); 
     JButton colorButton = new JButton(); 

     grid.gridx = 0; 
     grid.gridy = 0; 
     panel.add(title, grid); 
     title.setVisible(true); 
     title.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 1; 
     panel.add(label1, grid); 
     label1.setVisible(true); 
     label1.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 2; 
     panel.add(label2, grid); 
     label2.setVisible(true); 
     label2.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 3; 
     panel.add(label3, grid); 
     label3.setVisible(true); 
     label3.setFont(font); 

     grid.gridx = 1; 
     grid.gridy = 1; 
     panel.add(field1, grid); 
     field1.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 2; 
     panel.add(field2, grid); 
     field2.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 3; 
     panel.add(field3, grid); 
     field3.setText(""); 
     field3.setVisible(true); 

     grid.gridx = 0; 
     grid.gridy = 4; 
     panel.add(button1, grid); 
     button1.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 4; 
     panel.add(button2, grid); 
     button2.setVisible(true); 
     button2.setFont(font); 

     frame.pack(); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     frame.setLocation(dim.width/2 - frame.getSize().width/2, dim.height/2 - frame.getSize().height/2); 
     frame.setSize(250, 150); 

     grid.gridx=0; 
     grid.gridy=0; 
     colorPanel.add(colorButton, grid); 
     colorButton.setText("Click to stop colors"); 
     colorButton.setVisible(true); 

     colors.pack(); 
     Dimension dimz = Toolkit.getDefaultToolkit().getScreenSize(); 
     colors.setLocation(dimz.width/3 - frame.getSize().width/3, dimz.height/2 - frame.getSize().height/2); 
     colors.setSize(200, 150); 

     i=1; 
     //Infinite for loop 

     button1.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       double zzz = Calculator.distance(); 
       String za = String.valueOf(zzz); 
       field3.setText(za); 
      } 
     }); 

     button2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       field1.setText(""); 
       field2.setText(""); 
       field3.setText(""); 

      } 
     }); 



     colorButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       if (i == 1) { 
        colorButton.setText("Click here to start"); 
        i = 0; 
       } else if (i == 0) { 
        colorButton.setText("Click here to stop"); 
        i = 1; 
       } 

      } 
     }); 

     on = 0; 
     r = 0; 
     g = 0; 
     b = 0; 

     rr = 255; 
     rg = 255; 
     rb = 255; 
     //This is the loop for the color changing 
     while (i != 0) { 
      //Colors 
      Color color = new Color(r, g, b); 
      Color rcolor = new Color(rr, rg, rb); 

      //Panels 
      panel.setBackground(color); 
      frame.setBackground(color); 

      //Buttons 
      button1.setBackground(rcolor); 
      button2.setBackground(rcolor); 
      colorButton.setBackground(rcolor); 

      //Labels 
      label1.setForeground(rcolor); 
      label2.setForeground(rcolor); 
      label3.setForeground(rcolor); 
      title.setForeground(rcolor); 

      //Text fields 
      field1.setForeground(rcolor); 
      field2.setForeground(rcolor); 
      field3.setForeground(rcolor); 
      field1.setBackground(color); 
      field2.setBackground(color); 
      field3.setBackground(color); 

      //Sets a slight delay to avoid seizures 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      //Goes through the different colors 
      if (on == 0) { 
       if (r == 255) { 
        on = 1; 
       } else { 
        r++; 
        rr--; 
       } 
      } else if (on == 1) { 
       if (g == 255) { 
        on = 2; 
       } else { 
        g++; 
        rg--; 
       } 
      } else if (on == 2) { 
       if (b == 255) { 
        on = 3; 
       } else { 
        b++; 
        rb--; 
       } 
      } else if (on == 3) { 
       if (r == 0) { 
        on = 4; 
       } else { 
        r--; 
        rr++; 
       } 
      } else if (on == 4) { 
       if (g == 0) { 
        on = 5; 
       } else { 
        g--; 
        rg++; 
       } 
      } else if (on == 5) { 
       if (b == 0) { 
        on = 0; 
       } else { 
        b--; 
        rb++; 
       } 
      } 
     } 
    } 

    public static double distance(){ 

     String d = field1.getText(); 
     double velocity = Double.parseDouble(d); 
     String dd = field2.getText(); 
     double time = Double.valueOf(dd); 

     double totalDistance = (velocity * time) + (4.9 * (time * time)); 

     return totalDistance; 
    } 
} 

更新:我不能離開方法作爲公共靜態void,因爲它嘗試調用它返回一個值。我已經將字符串更改爲field.getText();而不是String.valueOf(field);它不斷給我在方法中的零點錯誤,當我在程序的早些時候調用方法。線程「AWT-EventQueue-0」中的異常java.lang.NumberFormatException:對於輸入字符串:「null」

回答

0

嗯,你試圖通過

String.valueOf(JTextField) 

那做的事情,你要哪個waaay不同得到的字符串。

e.g. String d = field1.getText(); 

編輯:您可以通過獲取字段的文本

我不認爲,Unit2Exam都是有目的的,但你的按鈕,你還在用它。首先,聲明距離爲靜態方法:

public static void distance() 

然後,

Calculator.distance() 

編輯2替換您的ActionListener

aa.distance() 

因此,另一種答案。

首先,你的代碼是相當混亂的,我假設你是新來的java。讓我來告訴你,你的錯誤:

首先:

final Unit2Exam Calculator = new Unit2Exam(); 

既然你有沒有構造,你不需要這個。剪掉它。

二:

的原因,爲什麼您選擇現場返回null,是因爲:

JTextField field1 = new JTextField(6); 
JTextField field2 = new JTextField(6); 
JTextField field3 = new JTextField(6); 

創建本地文本字段名爲field1的,這無關你全球 JTextField的字段1 ,它的餘生一直空空如也。

每個聲明之前就去掉 「的JTextField」,它應該工作:

field1 = new JTextField(6); 
field2 = new JTextField(6); 
field3 = new JTextField(6); 

最終結果:

package Unit2Exam; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Unit2Exam { 

    static int r; 
    static int g; 
    static int b; 
    static int rb; 
    static int rg; 
    static int on; 
    static int rr; 
    static int i; 
    public static JTextField field1; 
    public static JTextField field2; 
    public static JTextField field3; 

    public static void main(String[] args) { 

     //Makes a font 
     Font font = new Font("Verdana", Font.PLAIN, 12); 

     GridBagConstraints grid = new GridBagConstraints(); 
     //This creates the JFRAMES 
     JFrame frame = new JFrame("Free Fall Application"); 
     //This sets the title 
     JPanel panel = new JPanel(new GridBagLayout()); 
     //makes and adds the panel 
     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     grid.fill = GridBagConstraints.BOTH; 

     JFrame colors = new JFrame("Colors"); 
     JPanel colorPanel = new JPanel(new GridBagLayout()); 
     colors.add(colorPanel); 
     colors.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     grid.fill = GridBagConstraints.BOTH; 

     //causes the jbuttons to fill the borders (to make it look better) 
     grid.weightx = 1; 
     grid.weighty = 1; 
     // frame.setSize(new Dimension(200, 200)); 
     frame.setResizable(false); 
     colors.setResizable(false); 
     colors.setVisible(true); 
     colorPanel.setVisible(true); 
     frame.setVisible(true); 
     panel.setVisible(true); 

     //This is where I implement all of the J stuff (i.e. Buttons, Textfields, and JLabels) 
     JLabel title = new JLabel("Jonathan Kipper"); 
     JLabel label1 = new JLabel("Initial Velocity (m/s):"); 
     JLabel label2 = new JLabel("Time (Seconds):"); 
     JLabel label3 = new JLabel("Distance (meters):"); 
     field1 = new JTextField(6); 
     field2 = new JTextField(6); 
     field3 = new JTextField(6); 
     JButton button1 = new JButton("Calculate"); 
     JButton button2 = new JButton("Clear"); 
     JButton colorButton = new JButton(); 

     grid.gridx = 0; 
     grid.gridy = 0; 
     panel.add(title, grid); 
     title.setVisible(true); 
     title.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 1; 
     panel.add(label1, grid); 
     label1.setVisible(true); 
     label1.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 2; 
     panel.add(label2, grid); 
     label2.setVisible(true); 
     label2.setFont(font); 

     grid.gridx = 0; 
     grid.gridy = 3; 
     panel.add(label3, grid); 
     label3.setVisible(true); 
     label3.setFont(font); 

     grid.gridx = 1; 
     grid.gridy = 1; 
     panel.add(field1, grid); 
     field1.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 2; 
     panel.add(field2, grid); 
     field2.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 3; 
     panel.add(field3, grid); 
     field3.setText(""); 
     field3.setVisible(true); 

     grid.gridx = 0; 
     grid.gridy = 4; 
     panel.add(button1, grid); 
     button1.setVisible(true); 

     grid.gridx = 1; 
     grid.gridy = 4; 
     panel.add(button2, grid); 
     button2.setVisible(true); 
     button2.setFont(font); 

     frame.pack(); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     frame.setLocation(dim.width/2 - frame.getSize().width/2, dim.height/2 - frame.getSize().height/2); 
     frame.setSize(250, 150); 

     grid.gridx=0; 
     grid.gridy=0; 
     colorPanel.add(colorButton, grid); 
     colorButton.setText("Click to stop colors"); 
     colorButton.setVisible(true); 

     colors.pack(); 
     Dimension dimz = Toolkit.getDefaultToolkit().getScreenSize(); 
     colors.setLocation(dimz.width/3 - frame.getSize().width/3, dimz.height/2 - frame.getSize().height/2); 
     colors.setSize(200, 150); 

     i=1; 
     //Infinite for loop 

     button1.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       double zzz = Unit2Exam.distance(); 
       String za = String.valueOf(zzz); 
       field3.setText(za); 
      } 
     }); 

     button2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       field1.setText(""); 
       field2.setText(""); 
       field3.setText(""); 

      } 
     }); 



     colorButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       if (i == 1) { 
        colorButton.setText("Click here to start"); 
        i = 0; 
       } else if (i == 0) { 
        colorButton.setText("Click here to stop"); 
        i = 1; 
       } 

      } 
     }); 

     on = 0; 
     r = 0; 
     g = 0; 
     b = 0; 

     rr = 255; 
     rg = 255; 
     rb = 255; 
     //This is the loop for the color changing 
     while (i != 0) { 
      //Colors 
      Color color = new Color(r, g, b); 
      Color rcolor = new Color(rr, rg, rb); 

      //Panels 
      panel.setBackground(color); 
      frame.setBackground(color); 

      //Buttons 
      button1.setBackground(rcolor); 
      button2.setBackground(rcolor); 
      colorButton.setBackground(rcolor); 

      //Labels 
      label1.setForeground(rcolor); 
      label2.setForeground(rcolor); 
      label3.setForeground(rcolor); 
      title.setForeground(rcolor); 

      //Text fields 
      field1.setForeground(rcolor); 
      field2.setForeground(rcolor); 
      field3.setForeground(rcolor); 
      field1.setBackground(color); 
      field2.setBackground(color); 
      field3.setBackground(color); 

      //Sets a slight delay to avoid seizures 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      //Goes through the different colors 
      if (on == 0) { 
       if (r == 255) { 
        on = 1; 
       } else { 
        r++; 
        rr--; 
       } 
      } else if (on == 1) { 
       if (g == 255) { 
        on = 2; 
       } else { 
        g++; 
        rg--; 
       } 
      } else if (on == 2) { 
       if (b == 255) { 
        on = 3; 
       } else { 
        b++; 
        rb--; 
       } 
      } else if (on == 3) { 
       if (r == 0) { 
        on = 4; 
       } else { 
        r--; 
        rr++; 
       } 
      } else if (on == 4) { 
       if (g == 0) { 
        on = 5; 
       } else { 
        g--; 
        rg++; 
       } 
      } else if (on == 5) { 
       if (b == 0) { 
        on = 0; 
       } else { 
        b--; 
        rb++; 
       } 
      } 
     } 
    } 

    public static double distance(){ 

     String d = field1.getText(); 
     double velocity = Double.parseDouble(d); 
     String dd = field2.getText(); 
     double time = Double.valueOf(dd); 

     double totalDistance = (velocity * time) + (4.9 * (time * time)); 

     return totalDistance; 
    } 
+0

NullPointerException發生在哪裏? –

+0

第273行,其中String d = field1.getText();在 – Sethexgs2

+0

這是什麼?最終Unit2Exam aa = new Unit2Exam(); –

0

我將其更改爲

public double distance() { 

    String d = field1.getText(); 
    double velocity = Double.parseDouble(d); 
    String dd = field2.getText(); 
    double time = Double.valueOf(dd); 

    double totalDistance = (velocity * time) + (4.9 * (time * time)); 

    return totalDistance; 
} 

不過我仍然得到空指針異常。但它稍微改變了錯誤。它是在第273行,這是: String d = field1.getText(); 是個問題。

相關問題