我对 Java 相当陌生,我得到一个“java.lang.NullPointerException”
两个文本字段都返回为空,我不确定为什么我使用 .setText() 并且它显示在文本字段中
本质上我想要做的是传递velocityTextView中的值并将其传递给returnTextView。
我试着把returnTextView.setText(velocityTextView.getText());
这段代码放在actionPerformed()
方法中。
我被困住了,我希望得到一些指导。
import java.awt.Color;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class kinematica implements ActionListener {
public JPanel totalGui;
public JLabel progTitle;
public JButton enterBtn;
public JTextField velocityTextField, returnTextField;
public static void main(String[] args) {
showGui();
}
public JPanel createContentPane() {
// GUI panel to contain the elements that make GUI
JPanel totalGui = new JPanel();
totalGui.setLayout(null);
// Title Label
JLabel progTitle = new JLabel("Linear Motion 1D");
progTitle.setLocation(10, 10);
progTitle.setSize(500, 30);
progTitle.setHorizontalAlignment(JLabel.LEFT);
totalGui.add(progTitle);
// Buttons
JButton enterBtn = new JButton("Enter");
enterBtn.setLocation(100, 200);
enterBtn.setSize(100, 50);
enterBtn.addActionListener(this);
totalGui.add(enterBtn);
// text fields
JTextField velocityTextField = new JTextField(20);
velocityTextField.setLocation(100, 50);
velocityTextField.setSize(80, 30);
velocityTextField.setText("Velocity =");
totalGui.add(velocityTextField);
JTextField returnTextField = new JTextField();
returnTextField.setLocation(100, 100);
returnTextField.setSize(80, 30);
returnTextField.setText("test1");
totalGui.add(returnTextField);
totalGui.setOpaque(true);
return totalGui;
}
public void actionPerformed(ActionEvent e) {
returnTextField.getText();
System.out.println(velocityTextField);
}
public static void showGui() {
JFrame window = new JFrame("Kinematica");
kinematica demoWindow = new kinematica();
window.setContentPane(demoWindow.createContentPane());
window.setSize(1500, 1000);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}