嘿伙计们,我正在强调一些可能非常简单的事情。基本上我正处于一个项目的中间,需要我使用JRadioButtons创建一个框架,并根据用户点击的内容执行某些任务。下面我在第一个文件中有我的主要方法:
import javax.swing.JFrame;
public class Project2_ScottHoganChanged
{
public static void main(String[] args)
{
JFrame main_frame = new JFrame("Welcome to Murratime Limited Cruise Lines"); //Sets a title for the JFrame
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the JFrame when exiting
Cabin_ChoiceChanged choice = new Cabin_ChoiceChanged("", ""); //Calls upon the constructor of my Cabin_Choice class
main_frame.getContentPane().add(choice); //Places choice into the current JFrame pane
main_frame.pack(); //Sizes the frame
main_frame.setVisible(true); //Allows us to see the frame
} //Ends the main method
} //Ends the class
这是我班级的现有代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class Cabin_ChoiceChanged extends JPanel
{
final private JLabel description;
final private JRadioButton cabin1;
String answer1, passFname1, passFname2, passLname1, passLname2;
int passnum;
boolean run=true;
boolean passengerx1;
boolean passengerx2;
Scanner scan_in = new Scanner(System.in);
public Cabin_ChoiceChanged(String passfname, String passlname) //This is the constructor for the Cabin_Choice class
{
description = new JLabel("Choose your Cabin or Suite"); //Creates the label
description.setFont(new Font("Helonia", Font.BOLD, 28)); //Changes text font and formats for the description
//Creates Radio Buttons for the frame.
cabin1 = new JRadioButton("Cabin 11-1");
//Changes foreground AKA the text.
cabin1.setForeground(Color.white);
//Changes the background of the radio button
cabin1.setBackground(new Color(31, 21, 202)); //Creates my own custom background color
//Adds the buttons to a group so only one can be selected at a time
ButtonGroup group = new ButtonGroup();
group.add(cabin1);
//Creates a new listener so we can recognize that the user clicks on certain radio buttons
Cabin_Listener cabinlisten = new Cabin_Listener();
cabin1.addActionListener(cabinlisten);
add(description);
add(cabin1);
//Sets the background for the entire frame
setBackground(Color.white);
setPreferredSize(new Dimension(1000, 1000)); //Sets the default size of the frame
}
//The following class implements the ActionListener and will
//will change the color of the radio buttons depending on what
//the user clicks on. It also makes use of if else statements.
private class Cabin_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == cabin1) //If the user clicks the Cabin 1 radio button, this statement will run
{//Beginning of the above if statement
cabin1.setBackground(new Color(24, 204, 224)); //Sets the background for the selected cabin
System.out.println("You have selected Cabin 11-1. Would you like to book this cabin? Y/N");
answer1=scan_in.nextLine();
answer1=answer1.toLowerCase();
//This creates a nested if statement if they choose yes
if(answer1.equals("yes") || answer1.equals("y"))
{//Begins the command of the nested if statement
System.out.println("Please provide us with the following information so we can finish your reservation for Cabin 1:");
System.out.println("How many passengers will be in this cabin? *Note: The maximum number of passengers in a cabin is 2*");
passnum=scan_in.nextInt();
scan_in.nextLine(); //I had to put this here because the scan_in.nextInt() messes with the next scan_in.nextLine(), so this line truly ends the scan_in.nextInt()
while(run==true)
{//Opens the while(run==true) loop
switch (passnum)
{//Switch (passnum) opening bracket
case 1:
System.out.println("You have 1 passenger in this cabin. You will be charged an extra 45% onto your bill because the cabin isn't fulfilled.");
run=false; //Won't repeat the loop
passengerx1=true; //For use later to determine information about the passenger
break;
case 2:
System.out.println("You have 2 passengers in this cabin");
run=false; //Won't repeat the loop
passengerx2=true; //For use later to determine information about the passengers
break;
default:
System.out.println("You entered a number of passengers that isn't allowed.");
System.out.println("How many passengers will be in this cabin?");
passnum=scan_in.nextInt(); //Will ask for user input on passenger number again to prevent an infinite loop
run=true; //This will cause the loop to repeat
}//Switch (passnum) closing bracket
}//Closes the while(run==true) loop
if(passengerx1=true) //The statement inside here will recieve the passenger's first and last name as well as information about their city, state, dining time, and excursions
{//Opens the if(passengerx1=true) statement
System.out.println();
System.out.println("Please enter the passenger's first name: ");
passFname1=scan_in.nextLine();
System.out.println("Please enter the passenger's last name: ");
passLname1=scan_in.nextLine();
System.out.println();
System.out.println("Thank you "+passFname1+" "+passLname1+".");
}//Closes the if(passengerx1=true) statement
}//Closes the first nested if statement
}//Ends the main if statement
}
}
}
程序本身有效,但我老师要求我们做的一件大事就是在我的班级中创建6种方法并在我的主程序中使用它们。我还必须在main中创建对象,并使用我班级的方法编辑它们。当我运行程序时它一切运行良好,但我不明白如何在我的类中创建方法并在我的主程序中使用它们。到目前为止,我所知道的是,当我调用一个方法时,它将执行该方法中的内容,但是例如说我创建的一个乘客对象需要更改某些内容,如何告诉我的主程序等到该乘客对象我创建的是完成了我班上的东西扫描?
我想最简单的方法是在我的主程序中创建一个 if 或 while 语句,只有当从类中扫描完成后才能运行内部的语句?但是我的主程序如何知道扫描何时完成?
- 编辑 -
我在课堂上有一些扫描仪,我想从扫描仪获取信息,然后在用户输入完所有数据后在主程序中使用和操作该信息