2010-04-19 78 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它加载的内容。或者..如果我注释掉组合框对象的行,它也显示内容。