每当尝试按下特定按钮时,我都试图使用Car类中的加速和制动方法在CarView类中使用它们。当涉及到ActionListeners时,我的当前代码不断出现错误,我不确定从这里开始。这是我的代码。
import javax.swing.*; //Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface
public class CarView extends JFrame
{
private JPanel panel; //To reference a panel
private JLabel modelYearLable; //To reference a model year Label
private JLabel makeLable; //To reference a make Label
private JLabel speedLable; //To reference a speed Label
private JTextField modelTextField; // To reference a model yeartext field
private JTextField makeTextField; // To reference a make text field
private JTextField speedTextField; // To reference a speed text field
private JButton accelerateButton; // To reference an accelerate button
private JButton brakeButton; // To reference a brake button
private final int WINDOW_WIDTH = 310; // Window width
private final int WINDOW_HEIGHT = 100; // Window heigh
private final int carYear = Integer.parseInt(modelTextField.getText());
private final String type = makeTextField.getText();
Car vehicle = new Car(this.carYear, this.type); //Create an instance variable of the Car class!
//Constructor
public CarView()
{
//Set the window titile.
setTitle("Car referencer!");
//Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Build thhe panel and add it to the frame.
buildPanel();
//Add the panel to the frame's content pane.
add(panel);
//Display the window
setVisible(true);
}
//buildPanel()
//Responisibilities: Adds labels, text fields, and buttons to the panel
private void buildPanel()
{
//Create labels to display instructions
modelYearLable = new JLabel("Enter the year of your model");
makeLable = new JLabel("Enter the make of your car");
speedLable = new JLabel("Enter the current speed of your car");
//Create text fields as well
modelTextField = new JTextField(5);
makeTextField = new JTextField(5);
speedTextField = new JTextField(5);
//Create the buttons
accelerateButton = new JButton("Accelerate");
brakeButton = new JButton("Brake");
//Add an action listener to the buttons
accelerateButton.addActionListener(new AccelerateButtonListener());
brakeButton.addActionListener(new BrakeButtonListener());
//Create a JPanel object and let the panel field reference it.
panel = new JPanel();
//Add the labels,text fields, and buttons to the panel
panel.add(modelYearLable);
panel.add(makeLable);
panel.add(speedLable);
panel.add(modelTextField);
panel.add(makeTextField);
panel.add(speedTextField);
panel.add(accelerateButton);
panel.add(brakeButton);
}
//AccelerateButtonListener is an action listener private inner class for the Accelerate Button.
private class AccelerateButtonListener implements ActionListener
{
//The acitonPerformed method executes when the user clicks on the Accelerate Button
public void actionPerformed(ActionEvent e)
{
vehicle.accelerate();//Using the instance variable we made of Car earlier we can call the accelerate method from Car class.
}
}
//BrakeButtonListener is an action listener private inner class for the Brake Button.
private class BrakeButtonListener implements ActionListener
{
//The actton Performed method executes when the user clicks on the Brake Button
public void actionPerformed(ActionEvent e)
{
vehicle.brake();//Using the instance variable we made of Car earlier we can call the brake method from Car class.
}
}
//The main method creates an instance of the CarView, which causes it to display its window
public static void main(String[] args)
{
CarView cv = new CarView();
}
这是汽车课
public class Car
{
private int yearModel; //The yearModel is an int that holds the car's modcel year
private String make; //The make references a String object that holds the make of the car.
private double speed; //The speed field is a double that hold's thhe car's current speed.
//Construtor that accepts the car's year model and make as arguments. These values should be assigned to the obkect's modelYear and make fields.
//Also assign 0 to the speed
public Car(int model, String type)
{
this.yearModel = model;
this.make = type;
speed = 0.0; //Set the speed to 0.
}
//Get and Set methods for modelYear, make and speed fields.
//getModel
//Responsibilities: gets the model of the car
public int getModel()
{
return yearModel;
}
//getMake
//Responsibilities: gets the make of the car
public String getMake()
{
return make;
}
//getSpeed
//Responsibilities: gets the speed of the car
public double getSpeedl()
{
return speed;
}
//accelerate()
//Responsibilities: Shouyld add 8 to the speed each time it is called
public void accelerate()
{
speed = speed + 8; //Everytime this method is called, add 8 to the speed each time
}
//brake()
//Responsibilities: Should subtract 6 from the speed each time it is called.
public void brake()
{
speed = speed - 6; //Everytime this method is called subtract 6 from speeed.
}
}
我在运行当前代码时遇到NullPointerException异常,但我不知道该如何应对。我只想使用我的Car类方法来使动作侦听器加速和刹车,但我不知道如何做。
任何帮助表示感谢,谢谢!
因此,当我运行代码时,我得到一个NullPointerException
。仔细看一下代码,我会看到两个导致此问题的问题...
public class CarView extends JFrame
{
//...
private JTextField modelTextField; // To reference a model yeartext field
private JTextField makeTextField; // To reference a make text field
//...
private final int carYear = Integer.parseInt(modelTextField.getText());
private final String type = makeTextField.getText();
您无法从类的初始化阶段modelTextField
或makeTextField
在其初始化阶段获取值,因为变量是null
,即使它们是变量,它们也将为空,因为该组件甚至还没有显示在屏幕上。
相反,您需要在其他时间点获取值-记住GUI是事件驱动的,而不是过程驱动的或线性的。
还有很多其他事情,包括到处都是布局。没有验证;您不会在速度变化时向用户报告速度。
我可以花很多时间,但我会给你一点推动力
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CarView extends JFrame {
private JPanel panel; //To reference a panel
private JLabel modelYearLable; //To reference a model year Label
private JLabel makeLable; //To reference a make Label
private JLabel speedLable; //To reference a speed Label
private JTextField modelTextField; // To reference a model yeartext field
private JTextField makeTextField; // To reference a make text field
private JTextField speedTextField; // To reference a speed text field
private JButton accelerateButton; // To reference an accelerate button
private JButton brakeButton; // To reference a brake button
private JButton makeCarButton;
//private final int carYear;
//private final String type;
private Car vehicle;
//Constructor
public CarView() {
//Set the window titile.
setTitle("Car referencer!");
//Set the size of the window.
//setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Build thhe panel and add it to the frame.
buildPanel();
//Add the panel to the frame's content pane.
add(panel);
//Display the window
pack();
setVisible(true);
}
//buildPanel()
//Responisibilities: Adds labels, text fields, and buttons to the panel
private void buildPanel() {
//Create labels to display instructions
modelYearLable = new JLabel("Enter the year of your model");
makeLable = new JLabel("Enter the make of your car");
speedLable = new JLabel("Enter the current speed of your car");
//Create text fields as well
modelTextField = new JTextField(5);
makeTextField = new JTextField(5);
speedTextField = new JTextField(5);
//Create the buttons
accelerateButton = new JButton("Accelerate");
brakeButton = new JButton("Brake");
//Add an action listener to the buttons
accelerateButton.addActionListener(new AccelerateButtonListener());
brakeButton.addActionListener(new BrakeButtonListener());
// Don't want to use these until AFTER you've created a instance of Car
accelerateButton.setEnabled(false);
brakeButton.setEnabled(false);
speedTextField.setEnabled(false);
makeCarButton = new JButton("Make car");
makeCarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String make = makeTextField.getText();
String model = modelTextField.getText();
// Some funky validation
if (make == null || make.isBlank() || model == null || model.isBlank()) {
// Add an error message
return;
}
int year = Integer.parseInt(model);
vehicle = new Car(year, make);
makeCarButton.setEnabled(false);
accelerateButton.setEnabled(true);
brakeButton.setEnabled(true);
speedTextField.setEnabled(true);
}
});
//Create a JPanel object and let the panel field reference it.
panel = new JPanel(new GridLayout(-1, 2));
//Add the labels,text fields, and buttons to the panel
panel.add(modelYearLable);
panel.add(modelTextField);
panel.add(makeLable);
panel.add(makeTextField);
panel.add(new JPanel());
panel.add(makeCarButton);
panel.add(speedLable);
panel.add(speedTextField);
panel.add(accelerateButton);
panel.add(brakeButton);
}
//AccelerateButtonListener is an action listener private inner class for the Accelerate Button.
private class AccelerateButtonListener implements ActionListener {
//The acitonPerformed method executes when the user clicks on the Accelerate Button
public void actionPerformed(ActionEvent e) {
vehicle.accelerate();//Using the instance variable we made of Car earlier we can call the accelerate method from Car class.
speedTextField.setText(NumberFormat.getInstance().format(vehicle.speed));
}
}
//BrakeButtonListener is an action listener private inner class for the Brake Button.
private class BrakeButtonListener implements ActionListener {
//The actton Performed method executes when the user clicks on the Brake Button
public void actionPerformed(ActionEvent e) {
vehicle.brake();//Using the instance variable we made of Car earlier we can call the brake method from Car class.
speedTextField.setText(NumberFormat.getInstance().format(vehicle.speed));
}
}
//The main method creates an instance of the CarView, which causes it to display its window
public static void main(String[] args) {
CarView cv = new CarView();
}
public class Car {
private int yearModel; //The yearModel is an int that holds the car's modcel year
private String make; //The make references a String object that holds the make of the car.
private double speed; //The speed field is a double that hold's thhe car's current speed.
//Construtor that accepts the car's year model and make as arguments. These values should be assigned to the obkect's modelYear and make fields.
//Also assign 0 to the speed
public Car(int model, String type) {
this.yearModel = model;
this.make = type;
speed = 0.0; //Set the speed to 0.
}
//Get and Set methods for modelYear, make and speed fields.
//getModel
//Responsibilities: gets the model of the car
public int getModel() {
return yearModel;
}
//getMake
//Responsibilities: gets the make of the car
public String getMake() {
return make;
}
//getSpeed
//Responsibilities: gets the speed of the car
public double getSpeedl() {
return speed;
}
//accelerate()
//Responsibilities: Shouyld add 8 to the speed each time it is called
public void accelerate() {
speed = speed + 8; //Everytime this method is called, add 8 to the speed each time
}
//brake()
//Responsibilities: Should subtract 6 from the speed each time it is called.
public void brake() {
speed = speed - 6; //Everytime this method is called subtract 6 from speeed.
}
}
}
我建议看看:
花点时间更好地了解事件驱动环境的工作原理非常重要。创建一些不做任何事情的按钮,将一些按钮附加ActionListener
到它们上,然后使用它们System.out.println
打印出正在发生的事情,这将帮助您摆脱困境
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句