2015-05-11 37 views
0

我跟着我的教科書試圖學習如何創建一個窗口化的應用程序和使用Java中的佈局管理器。我已經使用textpad輸入了代碼,就像書中顯示的那樣,但是並不是所有的東西都顯示在我的應用程序中。該應用程序是預訂派對室的程序。它顯示8個面板預訂吸菸和非吸菸房間。有多個客人選擇列表,兩個吸菸和不吸菸複選框,兩個用於姓名和電話號碼的文本字段以及一個用於預訂房間的按鈕。我已經輸入了代碼,它是如何顯示在書中唯一顯示的是2文本字段,選擇列表和書房按鈕。任何人都可以告訴我我做錯了什麼。沒有編譯錯誤。 這裏是我的房間命名外部類:面板和複選框將不會顯示

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

你的教科書多大了?這段代碼使用了'Frame','Panel'等,它們是AWT的一部分,並且很久以前被'JFrame','JPanel'等替代,它們是Swing的一部分。 –

+0

這就是爲什麼它不工作?在我的書中,它沒有輸入java.awt.Color,我不得不輸入,只是爲了獲得顏色。我正在使用文本輸入板7和最新的Java sdk和JRE。我正在使用Java編程:綜合概念和技術第3版。它沒有提到有關JFrame或JPanel的任何內容。什麼是瞭解這個最好的書? –

+0

不,代碼在使用AWT時是一致的,所以這不是問題;混合Swing和AWT組件可能會導致問題。 –

回答

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個框添加到面板。我不確定這是否是「業務」要求,所以我不能進一步評論。

+0

它沒有顯示在書中。我會試試謝謝你。 –

+0

現在它顯示只有3個房間面板吸菸室6至8.至少我現在正在某處... –

+0

請參閱我的編輯。 –