Hello there, I'm trying on Gui exercises. One of them requires a simple GUI class for Vehicle.
The GUI should have buttons for creating new SportsCar and new Van objects. and then should display the values of the instance variables of these variables.
The GUI should also calculate and display the fuel consumption and acceleration of the vehicle objects that have been added by the user.
For Vehicle class I, this is the code
public class Vehicle
{
// instance variables - replace the example below with your own
private double horsepower, aerodynamics, weight;
public Vehicle(double initialHorsepower, double initialAerodynamics, double initialWeight)
{
// initialise instance variables
horsepower = initialHorsepower;
aerodynamics = initialAerodynamics;
weight = initialWeight;
}
public double getHorsepower()
{
return horsepower;
}
public double getAerodynamics()
{
return aerodynamics;
}
public double getWeight()
{
return weight;
}
public double getConsumption()
{
return (1000+(this.getWeight())/5)*(this.getAerodynamics())*(this.getHorsepower())/10000;
}
public double acceleration()
{
return ((100/this.horsepower)*this.aerodynamics*this.weight/100);
}
}
Basically in this code there are methods to calculate Acceleration and Consumption
For the GUI,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FVehicle extends JFrame
{
public static void main()
{
FVehicle driver = new FVehicle();
}
public FVehicle()
{
setTitle("Vehicle");
setVisible(true);
setSize(500,500);
setLayout(new GridLayout(2, 2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fire();
}
public void fire()
{
JPanel panel = new JPanel();
add(panel);
JButton button = new JButton("Add Sports Car");
JButton button1 = new JButton("Add Van");
panel.add(button);
panel.add(button1);
button.addActionListener(new buttonlistener());
button1.addActionListener(new button1listener());
}
class buttonlistener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFrame frame2 = new JFrame("SportsCar");
frame2.setVisible(true);
frame2.setSize(300,300);
JPanel panel = new JPanel();
frame2.add(panel);
JButton setSC = new JButton("Set Sports Car");
panel.add(setSC);
setSC.addActionListener(new setSportsCarListener());
}
}
class button1listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFrame frame3 = new JFrame("Van");
frame3.setVisible(true);
frame3.setSize(300,300);
JLabel label1 = new JLabel("New Van");
JPanel panel1 = new JPanel();
frame3.add(panel1);
panel1.add(label1);
}
}
class setSportsCarListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFrame setSCFrame = new JFrame("SportsCar");
setSCFrame.setVisible(true);
setSCFrame.setSize(300,300);
JPanel panelSC = new JPanel();
setSCFrame.add(panelSC);
TextField horsepowerField = new TextField();
TextField weightField = new TextField();
TextField topspeedField = new TextField();
add(new Label("Weight"));
add(weightField);
weightField.addActionListener(new setSportsCarListener());
add(new Label("Horsepower"));
add(horsepowerField);
horsepowerField.addActionListener(new setSportsCarListener());
add(new Label("Topspeed"));
add(topspeedField);
topspeedField.addActionListener(new setSportsCarListener());
}
}
}
This is what I have written so far. But I'm not sure whether this is correct. It's rather incomplete but it compiled.
My problems are:
I want to create a main frame which displays 2 buttons: Add New SportsCar and Add New Van
which I have already created.
And then when the user clicks Add New Sports Car, a new frame will appear with another button : Set New Sports Car and when the user clicks that, another frame will appear which will display several text fields.
These are for the user to input the horsepower, toppeed and weight of their desired new sports car ; which I have written but somehow doesn't appear like I thought it would. It comes out with several frames when I click add Set Sports Car
How do I link the new sports cars/vans created with the Vehicle class and then implement the formulas(consumption and acceleration) I have written in the class?