求大神们指点,急
易做图确实监听到month的值为选定的值,但是星座不会变啊,是不是month的值并未改变?怎么解决。。求指教求指教import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Constellation extends JFrame{
private int month,date;
JComboBox jcbMonth=new JComboBox();
JComboBox jcbDate=new JComboBox();
JLabel jlConstellation=new JLabel();
public void setMonth(int month2) {
this.month = month2;
}
public int getMonth(){
return month;
}
public void setDate(int _date) {
this.date = _date;
}
public int getDate(){
return month;
}
public int getDay(){
int day=31;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:{
day=31;
break;
}
case 4:
case 6:
case 9:
case 11:{
day=30;
break;
}
case 2:{
day=29;
break;
}
}
return day;
}
public Constellation(){
for(int i=1;i<=12;i++){
jcbMonth.addItem(new Integer(i));
}
for(int i=1;i<=getDay();i++){
jcbDate.addItem(new Integer(i));
}
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(2,1));
panel1.add(new JLabel("生日"));
panel1.add(new JLabel("星座"));
JPanel panel2=new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(jcbMonth);
panel2.add(new JLabel("月"));
panel2.add(jcbDate);
panel2.add(new JLabel("日"));
JPanel panel3=new JPanel();
panel3.setLayout(new GridLayout(2,1));
panel3.add(panel2);
panel3.add(jlConstellation);
setLayout(new BorderLayout());
add(panel1,BorderLayout.WEST);
add(panel3,BorderLayout.CENTER);
setSize(200,100);
setLocation(400,200);
setVisible(true);
jcbMonth.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
month=((Integer)jcbMonth.getSelectedItem()).intValue();
System.out.println(month);
}
});
jcbDate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
date=((Integer)jcbDate.getSelectedItem()).intValue();
System.out.println(date);
}
});
}
public String getConstellation(){
String constellation="摩羯座";
switch(month){
case 1:{
if(date<=19) constellation="摩羯座";
else constellation="水瓶座";
break;
}
case 2:{
if(date<=18) constellation="水瓶座";
else constellation="双鱼座";
break;
}
case 3:{
if(date<=20) constellation="双鱼座";
else constellation="白羊座";
break;
}
case 4:{
if(date<=19) constellation="白羊座";
else constellation="金牛座";
break;
}
case 5:{
if(date<=20) constellation="金牛座";
else constellation="双子座";
break;
}
case 6:{
if(date<=21) constellation="双子座";
else constellation="巨蟹座";
break;
}
case 7:{
if(date<=22) constellation="巨蟹座";
else constellation="狮子座";
break;
}
case 8:{
if(date<=22) constellation="狮子座";
else constellation="易做图座";
break;
}
case 9:{
if(date<=22) constellation="易做图座";
else constellation="天秤座";
break;
}
case 10:{
if(date<=23) constellation="天秤座";
else constellation="天蝎座";
break;
}
case 11:{
if(date<=22) constellation="天蝎座";
else constellation="射手座";
break;
}
case 12:{
if(date<=21) constellation="射手座";
else constellation="摩羯座";
}
}
return constellation;
}
public static void main(String args[]){
Constellation c=new Constellation();
c.jlConstellation.setText(c.getConstellation());
}
} --------------------编程问答--------------------
jcbMonth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
month = ((Integer) jcbMonth.getSelectedItem()).intValue();
jlConstellation.setText(getConstellation());
System.out.println(month);
}
});
jcbDate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
date = ((Integer) jcbDate.getSelectedItem()).intValue();
jlConstellation.setText(getConstellation());
System.out.println(date);
}
});
在事件响应中加入JLabel的更改 --------------------编程问答-------------------- main方法第一行实例化了对象,
第二行把默认值(1月1日)的星座显示出来,
后面改变日期的事件已经收到,你的代码可以打印出month和date,类属性确实更改了,然后呢?
得把改变后的month,date转换成星座放到JLabel
补充:Java , Java SE