面板和复选框不会显示

时间:2015-05-11 07:21:14

标签: java swing

我正在跟随我的课本,试图学习如何在Java中创建窗口化应用程序和使用布局管理器。我已经使用textpad输入了代码,就像书中显示的那样,但是我的应用程序中并没有显示所有内容。该应用程序是预订派对室的程序。它显示8个面板预订吸烟和非吸烟房间。有一个客人数量的选择列表,两个吸烟和非吸烟复选框,2个文本字段的名称和电话号码和一个预订房间的按钮。我已经输入了代码如何在书中显示,唯一显示的是2文本字段,选项列表和书房按钮。谁能告诉我我做错了什么。没有编译错误。 这是我的名为Rooms的外部类:

 public class Rooms{

//declaring variables
int numSmoking;
int numNonSmoking;
boolean occupied[];

//method for rooms
public Rooms(int non, int sm){
  occupied = new boolean[sm+non];

    //for loop for array occupied
    for(int i=0; i < (sm+non); i++)
        occupied[i] = false; //set each occupied room to false or empty
        //initialize the number of smoking and non smoking rooms
        numSmoking = sm;
        numNonSmoking = non;

}//end method rooms
//method to book room
public int bookRoom(boolean smoking){
    int begin, end;
    int roomNumber=0;

    //if statement for non smoking and smoking room booking
    if(!smoking){
        begin = 0;
        end = numNonSmoking;
    }//end if
    else{
        begin = numNonSmoking;
        end = numSmoking+numNonSmoking;
    }//end else
    for (int i=begin; i < end; i++){
        if(!occupied[i]){//if room not occupied
        occupied[i] = true;
        roomNumber = i+1;
        i = end;//to exit loop
    }//end if
}//end for
return roomNumber;

 }//end method bookroom

}//ends class

这是我的主要课程,我命名为Module5example2:

 import javax.swing.JOptionPane;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Color;

 public class Module5example2 extends Frame implements  
  ActionListener{//
//creating color
Color lightRed = new Color(255, 90, 90);
Color lightGreen = new Color(140, 215, 40);

//variables
Rooms room = new Rooms(5, 3);

Panel roomPanel = new Panel();
    TextArea roomDisplay[] = new TextArea[9];

Panel buttonPanel = new Panel();
    Button bookButton = new Button("Book Room");

Panel inputPanel = new Panel();
    Label custNameLabel = new Label("Name");
    TextField nameField = new TextField(15);
    Label custPhoneLabel = new Label("Phone Number");
    TextField phoneField = new TextField(15);
    Label numLabel = new Label("Number in Party");
    Choice numberOfGuests = new Choice();
    CheckboxGroup options = new CheckboxGroup();
        Checkbox nonSmoking = new Checkbox("Nonsmoking",false,options);
        Checkbox smoking = new Checkbox("Smoking",false,options);
        Checkbox hidden = new Checkbox("",true,options);

//reservation method
public  Module5example2(){// (B Reserv)  constructor method
//set layouts for frame and three panels
    this.setLayout(new BorderLayout());
        roomPanel.setLayout(new GridLayout(2,4,10,10));
        buttonPanel.setLayout(new FlowLayout());
        inputPanel.setLayout(new FlowLayout());

    //add components to room panel
    for (int i=1; i<9; i++){ //(C for)
        roomDisplay[i] = new TextArea(null,3,5,3);
        if(i<6){
            roomDisplay[i].setText("Room " + i + " Nonsmoking");
        }
        else{
            roomDisplay[i].setText("Room " + i + " Smoking");
        roomDisplay[i].setEditable(false);
        roomDisplay[i].setBackground(lightGreen);
        roomPanel.add(roomDisplay[i]);
        }//end else
    }//(C for closed)end for loop

//add components to button panel
buttonPanel.add(bookButton);

//add components to input label
inputPanel.add(custNameLabel);
inputPanel.add(nameField);
inputPanel.add(custPhoneLabel);
inputPanel.add(phoneField);
inputPanel.add(numLabel);
inputPanel.add(numberOfGuests);

for (int i = 8; i <= 20; i++)
    numberOfGuests.add(String.valueOf(i));
inputPanel.add(nonSmoking);
inputPanel.add(smoking);

//add panels to frame
add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);

bookButton.addActionListener(this);

//overriding the windowClosing() method will alow the user to click
//the close button

addWindowListener(
    new WindowAdapter(){//
        public void windowClosing(WindowEvent e){//
            System.exit(0);
        }//close window closing method
    }// close window adapter
 );
}//(end constructor method 

//main
public static void main(String[] args){
Module5example2 f = new Module5example2();
f.setBounds(200,200,600,300);
f.setTitle("Reserve a Party Room");
f.setVisible(true);
}

public void actionPerformed(ActionEvent e){
 if(hidden.getState()){
    JOptionPane.showMessageDialog(null," You must select Nonsmoking or   
    Smoking.",
    "Error", JOptionPane.ERROR_MESSAGE);
}
else{
        int available = room.bookRoom(smoking.getState());
        if (available > 0){//(end 2nd if)
            roomDisplay[available].setBackground(lightRed);
            roomDisplay[available].setText(

            roomDisplay[available].getText()+
                                            "\n" +
                                            nameField.getText() +
                                            " " +
                                            phoneField.getText() +
                                            "\nparty of" +
                                         numberOfGuests.getSelectedItem()

                                             );
        clearFields();
        }

        else{ //room is not available
                 if (smoking.getState())
        JOptionPane.showMessageDialog(null, "Smoking is full.","Error",
                    JOptionPane.INFORMATION_MESSAGE);


                 else
            JOptionPane.showMessageDialog(null, "Nonsmoking is full.",  
                    "Error",
                    JOptionPane.INFORMATION_MESSAGE);
                    hidden.setState(true);


    }
  }
}

 //reset the text field choice components
void clearFields(){
nameField.setText("");
phoneField.setText("");
numberOfGuests.select(0);
nameField.requestFocus();
hidden.setState(true);
}//end void clearfield


}//(A Class closed)end class rexervations

1 个答案:

答案 0 :(得分:0)

您忘了将roomPanel添加到JFrame。您需要将面板添加到JFrame才能显示它。 只是创建一个JPanel实例并设置属性不会有帮助。

所以你的代码需要修改为:

//add panels to frame
add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);
add(roomPanel,BorderLayout.CENTER); //<----- You missed this.

修改 我只是建议如何在JFrames内容窗格上显示面板。当您从书籍/互联网上复制或阅读并输入内容时,请确保您了解每行提供的内容/原因。 再次,您只看到3个绿色框,因为您的for循环中有一个错误,要向面板添加房间框。因为

if(i<6){
  roomDisplay[i].setText("Room " + i + " Nonsmoking");
}

您正在跳过添加前6个框到面板。我不确定这是否是一个&#34;业务&#34;要求所以我不能进一步评论。