Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有按钮和GUI的Java_Java_User Interface_Button - Fatal编程技术网

带有按钮和GUI的Java

带有按钮和GUI的Java,java,user-interface,button,Java,User Interface,Button,我已经完成了这个项目的代码。但是,我的按钮有问题。由于某种原因,当我按下按钮时,我的速度和刹车失灵。 *速度文本框应显示速度为0,当我按下加速按钮时,速度应增加7,当我按下制动按钮时,速度应减少5。我不确定我做错了什么。这是我的课 Car.Java public class Car { private String make; //holds car make private int speed; //holds car speed private int

我已经完成了这个项目的代码。但是,我的按钮有问题。由于某种原因,当我按下按钮时,我的速度和刹车失灵。 *速度文本框应显示速度为0,当我按下加速按钮时,速度应增加7,当我按下制动按钮时,速度应减少5。我不确定我做错了什么。这是我的课 Car.Java

public class Car
{
    private String make;    //holds car make
    private int speed;      //holds car speed
    private int yearModel;  //holds year model

    //Costructor that will accept the year model as
    //an argument
    public Car(String mk, int ym)
    {
        this.yearModel = ym;
        this.make = mk;
        this.speed = 0;
    }

    //getYearModel accesor
    public int getYearModel()
    {
        return yearModel;
    }

    //getSpeed accesor
    public int getSpeed()
    {
        return speed;
    }

    //getMake accessor
    public String getMake()
    {
        return make;
    }

    //method for accelerate, this method is also additng 7(mph/km)
    public void accelerate()
    {
        speed += 7; // accelerate the speed counter by 7
    }

    //Method for brake, this method should subtract 5
    //from the current speed
    public void brake()
    {
        speed = speed - 5;
    }
}
这是我的另一个类,我假设按钮不起作用

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

public class CarView extends JFrame 
{
    private JLabel yearModel,make,speed, color;    //labels for; yearModel, make, and speed
    private JTextField yearModelText, makeText, speedText;     //Text field for yearMake, make, 
    private JButton accelerate, brake; //botton for accelerate, brake 

    //Constructor
    public CarView(Car car)
    {
        //setting name/header for window
        super("Car Simulation View Window");
        //Setting frame size of window
        setSize(400, 250);
        //Specifying what will happen when botton clicked
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Setting the window to non-adjustable
        setResizable(false);
        //Adding components to panel
        addComponents(car);
        //Setting frame to visible
        setVisible(true);

    }

    //Adding components
    private void addComponents(Car car) 
    {
        //Create panel to hold UI compoenents
        JPanel panel = new JPanel();
        //Setting title of JPanel as CarUI
        panel.setBorder(BorderFactory.createTitledBorder("CarUI"));
        //Setting the panel layout
        panel.setLayout(new GridBagLayout());

        //Creating the year label
        yearModel = new JLabel("Year");
        //Adding the year label (0by0colum)
        addToPanel(panel, yearModel, 0, 0);
        //Creating the year text
        yearModelText = new JTextField(car.getYearModel() + "");
        //Making yearModelText to only readible
        yearModelText.setEditable(false);
        //Adding the yearText (0by2colum)
        addToPanel(panel, yearModelText, 0, 2);

        //Creating make label
        make = new JLabel("Make");
        //Adding the make label (1by0colum)
        addToPanel(panel, make, 1, 0);
        //Creating the make text
        makeText = new JTextField(car.getMake());
        //Making make text to onely readeble
        makeText.setEditable(false);
        //Adding makeText to panel (1by2colum)
        addToPanel(panel, makeText, 1, 2);

        //Creating the speed label
        speed = new JLabel("Speed");
        //Adding the speed label
        addToPanel(panel, speed, 2, 0);
        speedText = new JTextField(car.getSpeed());
        speedText.setEditable(false);
        addToPanel(panel, speedText, 2, 2);


        //Creating the accelerate button
        accelerate = new JButton("Accelerate");
        //Adding the accelerate button to a 4by0colum
        addToPanel(panel, accelerate, 4, 0);
        //Adding the action listener to the accelerate button
        accelerate.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                //accelerating the car
                car.accelerate();
            }
        });

        //Creating the brake button
        brake = new JButton("Brake");
        //Adding the brake button at 4 row and 2 column
        addToPanel(panel, brake, 4, 2);
        //Adding the action listener to the acceleration button
        brake.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                //Apply brakes to the car
                car.brake();
            }
        });

        // Add panel to the frame
        add(panel);
    }

    //Ading components to panel
    private void addToPanel(JPanel panel, Component comp, int row, int column) 
    {
        //Seting Constrain for layaout
        GridBagConstraints layoutConstraint = new GridBagConstraints();
        layoutConstraint.fill = GridBagConstraints.BOTH;
        layoutConstraint.anchor = GridBagConstraints.NORTH;
        //Setting constrain sizes in the folowing lines
        layoutConstraint.gridwidth = 2;
        layoutConstraint.weightx = 1;
        layoutConstraint.weighty = 0;
        layoutConstraint.ipady = 10;
        layoutConstraint.gridx = column;
        layoutConstraint.gridy = row;
        //Adding the constrains to panel
        panel.add(comp, layoutConstraint);
    }

    public static void main(String[] args)
    {
        //New Car object that will be displayed
        Car c = new Car("Toyota", 1995);
        new CarView(c);
    }
}

非常感谢您的任何帮助

您实际上需要从
Car
获取状态值,并在其更改时使用它更新
JTextField

accelerate.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            //accelerating the car
            car.accelerate();
            speedText.setText(Integer.toString(car.getSpeed()));
        }
    });

现在对中断执行相同的操作

,因为在更新回调中的backing字段时,不会更新
speedText

您可能希望执行以下操作:

 accelerate.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                //accelerating the car
                car.accelerate();
                speedText.setText(Integer.toString(car.getSpeed()));
            }
        });

        brake.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                //Apply brakes to the car
                car.brake();
                speedText.setText(Integer.toString(car.getSpeed()));
            }
        });

每次加速/减速时必须更新文本字段。