在Java中同时使用类和主程序
嘿,伙计们,我在为一些可能很简单的事情而紧张。基本上,我处于一个项目中,它需要我用JRadioButtons做一个框架,并根据用户点击的内容来完成某些任务。以下是我在第一个文件中的主要方法:在Java中同时使用类和主程序,java,class,main,Java,Class,Main,嘿,伙计们,我在为一些可能很简单的事情而紧张。基本上,我处于一个项目中,它需要我用JRadioButtons做一个框架,并根据用户点击的内容来完成某些任务。以下是我在第一个文件中的主要方法: import javax.swing.JFrame; public class Project2_ScottHoganChanged { public static void main(String[] args) { JFrame main_frame = new JFrame("Wel
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语句,只有当类的扫描完成时才运行里面的东西?但我的主程序如何知道扫描何时完成
-编辑-
我在类中有一些扫描器,我想从扫描器中获取信息,然后在用户输入完所有数据后在我的主程序中使用和操作这些信息我建议您在main和类的一些方法中设置一些断点,以便您可以看到类的流发生了什么如果你的代码是基于swing的,那么它(或者应该)都在一个线程上运行,所以你的事件处理程序代码一次调用一个。如果是这种情况,您不必等待其他代码“完成”,它已经完成了。我建议您在main和类的一些方法中设置一些断点,以便您可以看到代码流发生了什么。如果您的代码是基于swing的,那么它(或应该)都在单个线程上运行,因此,事件处理程序代码一次调用一个。如果是这种情况,您不必等待其他代码“完成”,它已经完成了。