I am trying to return the value of calculate in the first block of code to my JLabel in my GUI (which will be in the second block of code that I paste.
First block of code-
import java.util.Scanner;
public class SpeedingTicketClass {
Scanner scan=new Scanner (System.in); //Creating a scanner to read in users input of the speed limit and their speed.
//Variables used to store users input of the speed limit and the speed they are going.
int speedLimit=0;
int usersSpeed=0;
String usersName;
//Method being used to obtain the speed limit of the area with a do/while loop to make sure a correct speed limit is entered.
public int getSpeedLimit() {
do {
System.out.println("Please enter the speed limit: ");
speedLimit=scan.nextInt();
}while (!(speedLimit==70 || speedLimit==20| speedLimit==30 || speedLimit==40 || speedLimit==50 || speedLimit==60));
return speedLimit;
}
//Method being used to obtain the speed in which a user is going at.
public int getUsersSpeed() {
System.out.println("Please enter your speed: ");
usersSpeed=scan.nextInt();
return usersSpeed;
}
public String getUsersInformation() {
usersName=scan.nextLine();
return usersName;
}
//Method being used to calculate the speed of the users inputs and what fine they will receive.
public String calculateSpeed() {
String calculate="";
if (speedLimit==20 && usersSpeed>20 && usersSpeed<=24) {
calculate="Just a warning. Please do not speed again!";
}
else if (speedLimit==20 && usersSpeed>24 && usersSpeed<30) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==20 && usersSpeed>=30 && usersSpeed<35) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==20 && usersSpeed>=35 && usersSpeed<40) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==20 && usersSpeed>=40) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==20 && usersSpeed<=20) {
calculate=("You are within the speed limit!");
} //20mph IF statement
if (speedLimit==30 && usersSpeed>30 && usersSpeed<=34) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==30 && usersSpeed>34 && usersSpeed<40) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==30 && usersSpeed>=40 && usersSpeed<45) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==30 && usersSpeed>=45 && usersSpeed<50) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==30 && usersSpeed>=50) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==30 && usersSpeed<=30) {
calculate=("You are within the speed limit!");
} //30mph IF statement
if (speedLimit==40 && usersSpeed>40 && usersSpeed<=44) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==40 && usersSpeed>44 && usersSpeed<50) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==40 && usersSpeed>=50 && usersSpeed<55) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==40 && usersSpeed>=55 && usersSpeed<60) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==40 && usersSpeed>=60) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==40 && usersSpeed<=40) {
calculate=("You are within the speed limit!");
} //40mph IF statement
if (speedLimit==50 && usersSpeed>50 && usersSpeed<=54) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==50 && usersSpeed>54 && usersSpeed<60) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==50 && usersSpeed>=60 && usersSpeed<65) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==50 && usersSpeed>=65 && usersSpeed<70) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==50 && usersSpeed>=70) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==50 && usersSpeed<=50) {
calculate=("You are within the speed limit!");
} //50mph IF statement
if (speedLimit==60 && usersSpeed>60 && usersSpeed<=64) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==60 && usersSpeed>64 && usersSpeed<70) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==60 && usersSpeed>=70 && usersSpeed<75) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==60 && usersSpeed>=75 && usersSpeed<80) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==60 && usersSpeed>=80) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==60 && usersSpeed<=60) {
calculate=("You are within the speed limit!");
} //60mph IF statement
if (speedLimit==70 && usersSpeed>70 && usersSpeed<=74) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==70 && usersSpeed>74 && usersSpeed<80) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==70 && usersSpeed>=80 && usersSpeed<85) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==70 && usersSpeed>=85 && usersSpeed<90) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==70 && usersSpeed>=90) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==70 && usersSpeed<=70) {
calculate=("You are within the speed limit!");
} //70mph IF statement
if (speedLimit>70) {
calculate=("Please enter a speed limit which is 70mph or under!");
}
System.out.println(calculate);
return calculate;
}
}
Now below is my second code of the GUI-
//Imports
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SpeedingTicketGUI {
private JLabel lblInfo;
private JLabel lblWelcomeText;
private JLabel lblEnterName;
private JLabel lblEnterSpeedLimit;
private JLabel lblEnterYourSpeed;
private JLabel lblResultOfCalculate;
private JLabel lblFinalResult;
private JButton btnCalculate;
private JButton btnClose;
private JTextField txtEnterName;
private JTextField txtEnterSpeedLimit;
private JTextField txtEnterUsersSpeed;
private JPanel panel;
private JFrame frame;
private SpeedingTicketClass speedTicket=new SpeedingTicketClass();
Font f=new Font("Helvetica", Font.BOLD, 80);
Font r=new Font ("Helvetica", Font.BOLD, 100);
public SpeedingTicketGUI(){
createForm();
createButtons();
createLabels();
createInputFields();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
frame=new JFrame();
frame.setSize(800,600);
frame.setTitle("Speeding Ticket");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
panel=new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(null);
}
public void createButtons() {
btnCalculate= new JButton("Calculate");
btnCalculate.setBounds(158,307,114,27);
btnCalculate.addActionListener(new CalculateHandler());
panel.add(btnCalculate);
btnClose= new JButton ("Close");
btnClose.setBounds(319, 307, 114, 27);
btnClose.addActionListener(new ExitHandler());
panel.add(btnClose);
}
public void createInputFields() {
txtEnterName=new JTextField();
txtEnterName.setBounds(158, 72, 145, 27);
txtEnterName.addActionListener(new UsersInfoHandler());
panel.add(txtEnterName);
txtEnterSpeedLimit=new JTextField();
txtEnterSpeedLimit.setBounds(158, 109, 145, 27);
txtEnterSpeedLimit.addActionListener(new SpeedLimitHandler());
panel.add(txtEnterSpeedLimit);
txtEnterUsersSpeed=new JTextField();
txtEnterUsersSpeed.setBounds(158, 139, 145, 27);
txtEnterSpeedLimit.addActionListener(new UsersSpeedHandler());
panel.add(txtEnterUsersSpeed);
}
public void createLabels() {
lblWelcomeText=new JLabel ("Welcome!");
lblWelcomeText.setBounds(211, 16, 169, 21);
panel.add(lblWelcomeText);
lblInfo=new JLabel ("Check what fine you are required to settle here.");
lblInfo.setBounds(158,37,275,38);
panel.add(lblInfo);
lblEnterName=new JLabel ("Enter your name:");
lblEnterName.setBounds(40, 75, 102, 21);
panel.add(lblEnterName);
lblEnterSpeedLimit=new JLabel ("Enter the speed limit:");
lblEnterSpeedLimit.setBounds(28, 112, 126, 21);
panel.add(lblEnterSpeedLimit);
lblEnterYourSpeed=new JLabel ("Enter your speed:");
lblEnterYourSpeed.setBounds(38, 142, 106, 21);
panel.add(lblEnterYourSpeed);
lblResultOfCalculate=new JLabel ("Result:");
lblResultOfCalculate.setBounds(60, 201, 63, 21);
panel.add(lblResultOfCalculate);
lblFinalResult=new JLabel("-Penalty will appear here-");
lblFinalResult.setBounds(158, 201, 145, 21);
panel.add(lblFinalResult);
}
//Action Listener to calculate the users speed
class CalculateHandler implements ActionListener{
@Override
public void actionPerformed (ActionEvent arg0) {
speedTicket.calculateSpeed();
lblFinalResult.setText(String.valueOf(speedTicket.calculateSpeed()));
}
}
class SpeedLimitHandler implements ActionListener{
@Override
public void actionPerformed (ActionEvent arg0) {
speedTicket.getSpeedLimit();
}
}
/* UserInfoHandler used to obtain the users name so the information can be written to a file
for viewing at a later date. */
class UsersInfoHandler implements ActionListener{
@Override
public void actionPerformed (ActionEvent arg0) {
speedTicket.getUsersInformation();
}
}
//UserSpeedHandler used to obtain the speed that the user enters into the TextArea.
class UsersSpeedHandler implements ActionListener{
@Override
public void actionPerformed (ActionEvent arg0) {
speedTicket.getUsersSpeed();
}
}
//ExitHandler used to control what happens when a user presses the 'Close' button.
class ExitHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int n=JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit?", null, JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
public static void main (String[] args) {
new SpeedingTicketGUI();
}
}
Please could someone help me as I have tried time and time again to make it work but it still isn't.
对于所有选择,应该使用诸如if (usersSpeed >= (!speedLimit + 20) && usersSpeed <= (!speedLimit + 40)
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.