Swing 摇摆组合框
好的,我只是定义一个组合框,就像这样Swing 摇摆组合框,swing,Swing,好的,我只是定义一个组合框,就像这样 JComboBox yearSelect = new JComboBox(); 现在,我甚至没有将它添加到面板或任何东西中,我只是定义了它。当我运行应用程序时,不会显示任何内容。。当我把这行注释掉的时候。。该应用程序显示其他面板,就像我希望的那样。。我做错什么了吗?也许我忘了和组合框有关的东西?我想我错过的可能是一些愚蠢的事情 这是我的内容的整个构造器 private Container pane; // content pane pri
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);
专用容器窗格;//内容面板
专用JPanel日历面板;//我们的日历将去哪里
私有JPanel datePanel;//“今天的约会”将去哪里
私人JPanel pnlToolbar;//用于在年中移动的工具栏
私人JPanel小组;
私人JPanel年鉴小组;
私人JLabel lbCurrentMonth;
私人JLabel日期标签;
私人杰布顿·蒙特巴克;
蒙特沃德二等兵;
私人JComboBox yearSelect;
今天的私人日期=新日期();
private int currentmount=today.getMonth();
private int currentYear=today.getYear();
最终私有字符串[]天={“星期日”、“星期一”、“星期二”,
“星期三”、“星期四”、“星期五”,
“星期六”};
最终私有字符串[]月={“一月”、“二月”、“三月”、“四月”,
“五月”、“六月”、“七月”、“八月”,
“九月”、“十月”、“十一月”,
“12月”};
公历(){
//调用日历框架类构造函数(窗口类)
//此功能将设置我们的基本窗口
超级();
//将日历的当前日期定义为今天
//下面是我们窗口的属性。它们定义了什么内容
//这是他们的。
//定义我们的窗口
pane=window.getContentPane();
setLayout(新的BorderLayout());
calendarPanel=newJPanel(newFlowLayout());
calendarPanel.setBorder(BorderFactory.createTitledBorder(“日历”));
pnlToolbar=新的JPanel(新的FlowLayout(FlowLayout.CENTER,40,5));
pnlToolbar.设置尺寸(300,45);
//////////////////////////////////////////////////////////////////////
/*设置较低的日期面板,显示今天的日期
和年份下拉菜单*/
//“今天的日期”
dateLabel=newjlabel(returnDateString());
dateLabel.setFont(新字体(null,Font.BOLD,12));
datePanel=newJPanel(新的FlowLayout(FlowLayout.LEFT,5,5));
datePanel.add(新的JLabel(“今天的日期”),BorderLayout.WEST);
datePanel.add(dateLabel,BorderLayout.EAST);
//对于“年份选择”
//yearSelect=newjcombobox();
yearPanel=新的JPanel(新的FlowLayout(FlowLayout.RIGHT,5,5));
//yearPanel.add(yearSelect);
bottomPanel=newjpanel(newborderlayout());
添加(datePanel,BorderLayout.WEST);
//底部面板。添加(yearPanel,BorderLayout.EAST);
//设置工具栏面板
lbCurrentMonth=新的JLabel(月[currentMonth]);
monthBack=新的JButton(“”);
monthForward.setEnabled(true);
monthBack.setEnabled(true);
pnlToolbar.add(monthBack);
pnlToolbar.add(lbCurrentMonth);
pnlToolbar.add(前一个月);
//将所有内容添加到内容面板
添加(calendarPanel,BorderLayout.CENTER);
窗格。添加(底部面板,边框布局。南部);
添加窗格(pnlToolbar,BorderLayout.NORTH);
你需要添加一个模型来显示你的数据。请参阅。是的,我已经附加了上面的其余代码。即使我没有将对象添加到面板中,它也会这样做。我只是实例化对象,没有别的。有什么想法吗?另外,我已经创建了一个单独的应用程序,以确保我正确地执行组合框我注意到了一些东西。它在sperate应用程序中不完全相同..下面..(非常脏)
由于某种奇怪的原因,当我调整窗口大小或最小化窗口时,它会加载内容。或者..如果我对组合框对象的行进行注释,它也会显示内容。在没有看到其余代码的情况下,很难说。你的意思是说取消注释
JComboBox yearSelect=new JComboBox();
导致面板呈现错误?问题不太清楚。发布实际的Java文件(无任何关键业务信息)为什么每个JComboBox上都需要setModel来显示数据?模型是数据的表示形式。Swing控件中的视图和模型之间有明确的分隔,因此您可以以最佳方式表示数据。还有两种类型的模型,ComboxModel和MutableComboxModel,它们告诉combobox您的数据是否正确可更新。是的,如果只是显示简单的数据,这是一件痛苦的事情,但是如果您的日历,例如,还可以查找holdiays、报警、约会等并添加注释,那么它将非常灵活。如果您进一步设置单元格呈现器,则可以获取数据的实例,并以您的方式呈现,而不是以字符串的方式呈现。
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);
}
}