2010-04-19 104 views
0

好了,所以我簡單地定義,像這樣的組合框...鞦韆組合框

JComboBox yearSelect = new JComboBox(); 

現在,我還沒有將此添加到面板或任何東西,我剛纔定義它。當我運行該應用程序時,沒有任何顯示..當我註釋掉這條線......該應用程序顯示其他面板,就像我想要的那樣..是否有什麼我做錯了?也許我忘記了一些與組合框有關的事情?我想這可能是我錯過的一些愚蠢的東西。

這是我的整個內容構造函數。

private Container pane;   // content pane 
private JPanel calendarPanel; // where our calendar will go 
private JPanel datePanel;  // where "todays date" will go 
private JPanel pnlToolbar;  // tool bar for moving in the year 
private JPanel bottomPanel; 
private JPanel yearPanel; 
private JLabel lbCurrentMonth; 
private JLabel dateLabel; 
private JButton monthBack; 
private JButton monthForward; 
private JComboBox yearSelect; 

private Date today = new Date(); 
private int currentMonth = today.getMonth(); 
private int currentYear = today.getYear(); 
final private String [] days = {"Sunday", "Monday", "Tuesday", 
           "Wednesday", "Thursday", "Friday", 
           "Saturday"}; 

final private String [] months = {"January", "Febuary", "March", "April", 
            "May", "June", "July", "August", 
            "September", "October", "November", 
            "December"}; 


public Calendar() { 

    // call the calendar frame class constructor (Window class) 
    // this function will setup our basic window 
    super(); 

    // define the current date of the calendar as today 



    // below are attributes to our window. They define what content 
    // is on them. 


    // define our window 
    pane = window.getContentPane(); 
    pane.setLayout(new BorderLayout()); 

    calendarPanel = new JPanel(new FlowLayout()); 
    calendarPanel.setBorder(BorderFactory.createTitledBorder("Calendar")); 

    pnlToolbar = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 5)); 
    pnlToolbar.setSize(300, 45); 


    ////////////////////////////////////////////////////////////////////// 
    /* setup our lower date panel, that displays todays date 
    and the year drop down menu */ 

    // for "todays date" 
    dateLabel = new JLabel(returnDateString()); 
    dateLabel.setFont(new Font(null, Font.BOLD, 12)); 

    datePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
    datePanel.add(new JLabel("Todays Date: "), BorderLayout.WEST); 
    datePanel.add(dateLabel, BorderLayout.EAST); 

    // for "year select" 
    // yearSelect = new JComboBox(); 

    yearPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5)); 
    //yearPanel.add(yearSelect); 

    bottomPanel = new JPanel(new BorderLayout()); 
    bottomPanel.add(datePanel, BorderLayout.WEST); 
    //bottomPanel.add(yearPanel, BorderLayout.EAST); 

    // setup the tool bar panel 
    lbCurrentMonth = new JLabel(months[currentMonth]); 
    monthBack = new JButton("<<"); 
    monthForward = new JButton(">>"); 
    monthForward.setEnabled(true); 
    monthBack.setEnabled(true); 

    pnlToolbar.add(monthBack); 
    pnlToolbar.add(lbCurrentMonth); 
    pnlToolbar.add(monthForward); 



    // add everything to the content panel 
    pane.add(calendarPanel, BorderLayout.CENTER); 
    pane.add(bottomPanel, BorderLayout.SOUTH); 
    pane.add(pnlToolbar, BorderLayout.NORTH); 
+1

沒有看到你的代碼的其餘部分,很難說。 – 2010-04-19 01:39:33

+0

你是不是故意說'JComboBox yearSelect = new JComboBox();'導致錯誤的面板渲染?這個問題不是很清楚。 – 2010-04-19 16:28:49

+0

發佈您的實際Java文件(無任何關鍵業務信息) – 2010-04-19 16:38:29

回答

2

您需要添加模型才能顯示數據。見JComboBox.setModel

+0

爲什麼在每個* JComboBox上都需要setModel來顯示數據? – 2010-04-19 16:37:43

+0

模型是數據的表示形式。在Swing控件中,視圖和模型之間存在明顯的分離,因此您可以以最佳方式表示數據。還有2種類型的模型,ComboBoxModel和MutableComboBoxModel,它告訴組合框你的數據是否可更新。是的,如果您只是顯示簡單的數據,那麼這很痛苦,但是如果您的日曆也查找了holdiays,鬧鐘,約會等並註釋,那麼它非常靈活。如果你進一步設置了一個單元格渲染器,那麼你可以獲得你的數據的一個實例,並以你的方式呈現它而不是一個字符串。 – 2010-04-20 01:26:08

0

沒什麼,我附上了我上面的代碼的其餘部分。即使我沒有將該對象添加到面板,它也會這樣做。我只是實例化對象,僅此而已。有任何想法嗎?此外,我創建了一個單獨的應用程序來確保我正確地執行組合框,這裏是我注意到的。它不中sperate應用同樣的事情..下面..(很髒)

public class Main { 


public static void main(String[] args) { 
    String [] selectFill = {"Cat", "Dog"}; 
    JFrame window = new JFrame("Window"); 
    window.setBounds(0, 0 , 800, 600); 
    window.setVisible(true); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setLayout(null); 

    //JComboBox selectBox = new JComboBox(selectFill); 
    JLabel check = new JLabel("Select: "); 
    JPanel contentPanel = new JPanel(new FlowLayout()); 
    Container pane = window.getContentPane(); 
    pane.add(contentPanel); 
    pane.setLayout(new FlowLayout()); 
    contentPanel.add(check); 
    //contentPanel.add(selectBox); 
} 

} 

由於一些奇怪的原因,當我調整窗口的大小,或minmize它加載的內容。或者..如果我註釋掉組合框對象的行,它也顯示內容。