没有返回预期的内容

时间:2015-10-27 02:11:50

标签: java constructor calling-convention

我在打印(fname1lname1)中扫描的名字和姓氏时遇到问题。我必须创建6个对象,这些是我似乎无法开始的两个对象。此外,如果我输入除“是”或“y”之外的任何内容,它将不会循环回到我在片段上方插入的单选按钮。我该如何解决这个问题?

这是在输出窗口中输出的内容:

[,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

1 个答案:

答案 0 :(得分:0)

您显示一个Swing组件返回的toString()方法 - 可能是JPanel,因为它使用FlowLayout,解决方案是:

  • 不要将Swing组件传递到System.out.println(...)来电,
  • 而是打印出关键的非GUI对象的状态,称为“模型”对象,因为它们模拟了程序的行为和状态。
  • 确保这些类具有合适的public String toString()方法覆盖,因此打印出来的内容是有道理的。

你问:

  

什么是模型对象?

如果您正在创建一个飞机GUI,您可能需要创建几个类,一些GUI组件类,其他非类。模型类可以包括:

  • 乘客
  • 飞行
  • AirplaneSeat
  • ....

这些模型类将包含有关自己的信息,例如乘客将有一个名字,也许是一个年龄,一个乘客号码...飞机将拥有一流的AirplaneSeats和同样的教练。 AirplaneSeat将有一个布尔占用的字段,也许是一个Passenger字段来告诉谁占用了什么座位。但是这些类都不包含GUI组件代码,没有一个会扩展JPanel或JFrame等。

然后你可能会有扩展GUI组件或包含它们的视图类,它们将显示上面模型类的状态。您试图打印出视图类的toString(),它不会覆盖toString()方法。

侧面建议:您似乎正在尝试将Swing GUI程序与控制台程序混合使用,而您不会想要这样做。相反,您应该在GUI中进行所有用户交互和输出,而不是在控制台中,除非是为了简单的调试目的,但仅此而已。

相关问题