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(字段);它一直在方法中给我零点错误,当我在程序中先前调用该方法时。
答案 0 :(得分:0)
嗯,你正试图通过
获取字符串String.valueOf(JTextField)
多数人做的事与你想要的有所不同。您可以通过以下方式获取字段文本:
e.g. String d = field1.getText();
编辑:
我不认为,Unit2Exam有一个目的,但在你的按钮上,你还在使用它。首先,将距离声明为静态方法:
public static void distance()
然后,在ActionListener中替换
aa.distance()
带
Calculator.distance()
编辑2:
所以,另一个答案。
首先,你的代码非常混乱,我认为你是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;
}
答案 1 :(得分: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(); 是一个问题。