简体   繁体   English

不退还什么

[英]Not returning what's intended

I am having trouble printing out the first and last name scanned in ( fname1 , lname1 ). 我无法打印出在( fname1lname1 )中扫描的名字和姓氏。 I have to create 6 objects and these are two that I can't seem to even start with. 我必须创建6个对象,而这似乎是我什至无法开始的两个对象。 Also, if I enter anything except "yes" or "y", it will not loop back to the radiobuttons I inserted above the snippet. 另外,如果我输入“是”或“ y”以外的任何内容,它将不会循环回到我在代码片段上方插入的单选按钮。 How do I fix this? 我该如何解决?

This is what prints out in the output window: 这是在输出窗口中输出的内容:

[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=350,height=200]]
public class Cabin_Selector
{
    public static void main(String[] args)
    {
        JFrame cabin_selection = new JFrame("Select Your Cabin"); //Prompts the user
        cabin_selection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close the Frame when exiting

        Project2_JoshuaLucas selection = new Project2_JoshuaLucas("", "", "", "", "", "", "", 0.00); //Call the constructor for the Project2_JoshuaLucas class
        cabin_selection.getContentPane().add(selection); //put the object in the current pane of the jframe

        cabin_selection.pack();  //size the frame
        cabin_selection.setVisible(true);  //make the frame visible

    }  //end main method
}  //end class

if (source == cabin1)
{
    cabin1.setBackground(Color.darkGray);
    cabin2.setBackground(Color.gray);
    cabin3.setBackground(Color.gray);
    cabin4.setBackground(Color.gray);
    cabin5.setBackground(Color.gray);
    cabin6.setBackground(Color.gray);
    cabin7.setBackground(Color.gray);
    cabin8.setBackground(Color.gray);
    cabin9.setBackground(Color.gray);
    cabin10.setBackground(Color.gray);
    suite1.setBackground(Color.red);
    suite2.setBackground(Color.red);

    System.out.println("Your choice is Cabin 11-1, would you like to designate this as your room?");
    info1 = scan_in.nextLine();
    info1 = info1.toLowerCase();

    if ( info1.equals ("yes") || info1.equals ("y"))
    {
        continues=true;   
        System.out.println("Please enter the number of people in your cabin (*Maximum number of people is 2*)");
        cabin_people = scan_in.nextInt();
        scan_in.nextLine();

        while(continues)
        {
            switch (cabin_people)
            {
            case 1:
                System.out.println("There is one passenger within the cabin. (You will pay an EXTRA 45% because of the empty passenger slot)");
                continues=false;
                onepassenger=true;
                break;
            case 2:
                System.out.println("There are two passenger within this cabin.");
                continues=false;
                twopassenger=true;
                break;
            default:
                System.out.println("Please try again. Remember, the maximum amount of passengers allowed is 2.");
                System.out.println("How many passengers are staying within this cabin?");
                cabin_people=scan_in.nextInt();
                scan_in.nextLine();
                continues=true;
            }//Closes the Switch
        }//Closes the while(continues) loop

        while(onepassenger)
        {
            System.out.println("Please state your FIRST name: ");
            fname1=scan_in.nextLine();
            System.out.println();
            System.out.println("Please state your LAST name: ");
            lname1=scan_in.nextLine();

            onepassenger=false;
            Project2_JoshuaLucas passenger1 = new Project2_JoshuaLucas (fname1, lname1, "", "", "", "", "", 0.00);
            System.out.println(passenger1);
        } //Closes while(1passenger)
        while(twopassenger)
        {
            System.out.println("Please state your FIRST name: ");
            fname1=scan_in.nextLine();
            System.out.println("Please state your LAST name: ");
            lname1=scan_in.nextLine();
            System.out.println("Please enter the second passenger's FIRST name: ");
            fname2=scan_in.nextLine();
            System.out.println("Please enter the second passenger's LAST name: ");
            lname2=scan_in.nextLine();
            System.out.println("Please enter the city you live in: ");
            cob=scan_in.nextLine();

            twopassenger=false;
        } //Closes while(2passenger)
    } //Closes yes | y
    else 
        System.out.println("Please select another cabin");
    continues=false;
} //Closes source==cabin1

Your displaying the toString() method returned by one of your Swing components -- likely a JPanel since it uses FlowLayout, and the solution is: 您显示的是Swing组件之一返回的toString()方法-可能是一个JPanel,因为它使用FlowLayout,解决方案是:

  • Don't pass a Swing component into a System.out.println(...) call, 不要将Swing组件传递给System.out.println(...)调用,
  • Instead print out the state of your key non-GUI objects, called "model" objects because they model the behavior and state of your program. 而是打印出关键的非GUI对象的状态,这些对象称为“模型”对象,因为它们为程序的行为和状态建模。
  • Make sure that these classes have a decent public String toString() method override, so what they print out makes sense. 确保这些类具有适当的public String toString()方法重写,因此将它们打印出来是有意义的。

You ask: 你问:

What is a model object? 什么是模型对象?

If you are creating an airplane GUI for instance, you'll likely need to create several classes, some of the GUI component classes, others non. 例如,如果要创建飞机GUI,则可能需要创建几个类,其中一些GUI组件类,而另一些则不是。 The model classes could include: 模型类可以包括:

  • Passenger 乘客
  • Airplane 飞机
  • AirplaneSeat 飞机座位
  • .... ....

These model classes will contain information about themselves, such as a Passenger will have a name, perhaps an age, a passengerNumber id number... The Airplane will have a collection of first class AirplaneSeats and likewise for coach. 这些模型类别将包含有关其自身的信息,例如,旅客将有一个姓名,也许是年龄,一个旅客编号ID号...。飞机将有头等舱飞机座的集合,教练也是如此。 AirplaneSeat will have a boolean occupied field, and perhaps a Passenger field to tell who occupies what seat. AirplaneSeat将拥有一个布尔型的占用区域,也许还有一个旅客区域,以告诉谁占据了哪个座位。 But none of these classes will contain GUI component code, none will extend JPanel or JFrame or such. 但是这些类都不包含GUI组件代码,也不会扩展JPanel或JFrame等。

Then you'll likely have view classes that do either extend GUI components or contain them, and they will display the state of the model classes above. 然后,您将可能具有可以扩展GUI组件或包含它们的视图类,它们将显示上面模型类的状态。 You tried to print out the toString() of a view class, one that does not override the toString() method. 您试图打印出视图类的toString(),该类不覆盖toString()方法。

A side recommendation: you appear to be trying to mix a Swing GUI program with a console program, and you're not going to want to do this. 附带建议:您似乎正在尝试将Swing GUI程序与控制台程序混合使用,而您并不想这样做。 Instead you should have all user interaction and output in the GUI, not in console, except perhaps for simple debugging purposes, but nothing more. 取而代之的是,您应该在GUI中而不是在控制台中具有所有用户交互和输出,除非可能是出于简单调试目的,但仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM