2012-10-06 63 views
1

我在不同的面板上使用JTabbedPane,並且我使用gridLayout作爲我將添加的按鈕數組。我試着將按鈕循環到更簡單的代碼。但是我得到一個空指針異常,我不知道如何得到那種錯誤。 這裏是我的代碼JTabbedPane給出空指針異常

import javax.swing.*; 
import javax.swing.border.CompoundBorder; 
import java.awt.*; 
public class HotelRoomsGUI extends JPanel{ 
    private JTabbedPane mainJTP; 
    private JPanel classicTab,deluxeTab,presidentialTab,classicSubPanel,deluxeSubPanel,presidentialSubPanel; 
    private String classicRoomNo[] = {"101","102","103","104","105","106","107","108","109","101","111","112"}; 
    private String deluxeRoomNo[] = {"201","202","203","204","205","206","207","208","209","201","211","212"}; 
    private String presidentialRoomNo[] = {"301","302","303","304","305","306","307","308","309","301","311","312"}; 
    private JButton[] classicRoom, deluxeRoom, presidentialRoom; 
    public HotelRoomsGUI(){ 

     setLayout(null); 
     setBackground(new Color(90,90,90)); 
     add(tabbedPane()); 

    } 
    public JPanel classic() 
    { 
     classicTab = new JPanel(); 
     classicTab.setBackground(new Color(70,70,70)); 
     classicTab.setLayout(null); 
     classicSubPanel(); 
     return classicTab; 
    } 
    public JPanel classicSubPanel() 
    { 
     classicSubPanel = new JPanel(); 
     classicSubPanel.setBounds(10,10,605,455); 
     classicSubPanel.setLayout(new GridLayout(4,3,10,10)); 
     classicSubPanel.setBackground(new Color(70,70,70)); 
     classicTab.add(classicSubPanel); 
     rooms(); 
     return classicTab; 
    } 
    public JPanel deluxe() 
    { 
     deluxeTab = new JPanel(); 
     deluxeTab.setBackground(new Color(70,70,70)); 
     deluxeTab.setLayout(null); 
     deluxeSubPanel(); 
     return deluxeTab; 
    } 
    public JPanel deluxeSubPanel() 
    { 
     deluxeSubPanel = new JPanel(); 
     deluxeSubPanel.setBounds(10,10,605,455); 
     deluxeSubPanel.setLayout(new GridLayout(4,3,10,10)); 
     deluxeSubPanel.setBackground(new Color(70,70,70)); 
     deluxeTab.add(deluxeSubPanel); 
     rooms(); 
     return deluxeSubPanel; 
    } 
    public JPanel presidential() 
    { 
     presidentialTab = new JPanel(); 
     presidentialTab.setBackground(new Color(70,70,70)); 
     presidentialTab.setLayout(null); 
     presidentialSubPanel(); 
     return presidentialTab; 
    } 
    public JPanel presidentialSubPanel() 
    { 
     presidentialSubPanel = new JPanel(); 
     presidentialSubPanel.setBounds(10,10,605,455); 
     presidentialSubPanel.setLayout(new GridLayout(4,3,10,10)); 
     presidentialSubPanel.setBackground(new Color(70,70,70)); 
     presidentialTab.add(presidentialSubPanel); 
     rooms(); 
     return presidentialSubPanel; 
    } 
    public JTabbedPane tabbedPane() 
    { 
     UIManager.put("TabbedPane.selected", Color.ORANGE); 
     mainJTP = new JTabbedPane(); 
     mainJTP.setBackground(Color.WHITE); 
     mainJTP.setBounds(3,1,630,500); 
     mainJTP.addTab("Classic",classic()); 
     mainJTP.addTab("Deluxe",deluxe()); 
     mainJTP.addTab("Presidential",presidential()); 
     return mainJTP; 
    } 
    public void rooms() 
    { 
     JButton presidentialRoom[] = new JButton[presidentialRoomNo.length];   
     JButton deluxeRoom[] = new JButton[deluxeRoomNo.length]; 
     JButton classicRoom[] = new JButton[classicRoomNo.length]; 
     for(int x = 0;x<classicRoomNo.length;x++){ 
      //classic rooms 
      ImageIcon imageC = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\classicRooms.JPG"); // image 
      classicRoom[x] = new JButton(classicRoomNo[x],imageC); 
      classicRoom[x].setBackground(Color.WHITE); 
      classicRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY), 
        BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY))); 
      classicSubPanel.add(classicRoom[x]); 
      //deluxe rooms 
      ImageIcon imageD = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\deluxeRooms.JPG"); // image 
      deluxeRoom[x] = new JButton(deluxeRoomNo[x],imageD); 
      deluxeRoom[x].setBackground(Color.WHITE); 
      deluxeRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY), 
        BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY))); 
      deluxeSubPanel.add(deluxeRoom[x]); 
      //presidential rooms 
      ImageIcon imageP = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\presidentialRooms.JPG"); // image 
      presidentialRoom[x] = new JButton(deluxeRoomNo[x],imageP); 
      presidentialRoom[x].setBackground(Color.WHITE); 
      presidentialRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY), 
        BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY))); 
      presidentialSubPanel.add(presidentialRoom[x]); 

     } 
    } 
} 

,並請給我如何我可以編寫更清晰的一些技巧和更好的方法

+0

請勿使用空白布局;添加堆棧跟蹤&顯示行號;編輯您的問題以包含[sscce](http://sscce.org/)。 – trashgod

+0

@ user1708134請檢查我的解決方案! – Juvanis

回答

2

在我的建議下看看,這是你的tabbedPane()方法:

public JTabbedPane tabbedPane() { 
    UIManager.put("TabbedPane.selected", Color.ORANGE); 
    mainJTP = new JTabbedPane(); 
    mainJTP.setBackground(Color.WHITE); 
    mainJTP.setBounds(3, 1, 630, 500); 
    mainJTP.addTab("Classic", classic()); 
    mainJTP.addTab("Deluxe", deluxe()); 
    mainJTP.addTab("Presidential", presidential()); 
    rooms(); // I've inserted this, no panic! 
    return mainJTP; 
} 

在上述方法中,調用classic(), deluxe()presidential()已經觸發上rooms()方法調用,如果仔細追查。相反,請按照上面所述完成一個rooms()調用。從代碼中的其他位置刪除對rooms()的呼叫。

+0

你能解釋更多關於我的錯誤嗎?我對你的方法感興趣。 – user1708134

2

你得到一個NullPointerExceptiondeluxeSubPanel打電話時rooms()打電話時沒有實例:

deluxeSubPanel.add(deluxeRoom[x]); 

作爲一般指導原則,最好在使用從屬組件創建組件時使用自頂向下的方法。這將使創建訂單— JTabbedPane - JPanel - 子組件。

+0

你能舉一個自頂向下的方法嗎?做一個代碼只是爲了演示。謝謝 – user1708134