监听器确实监听到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());
}
}
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());
}
}
解决方案 »
- java连接数据库时加载驱动程序Class.forName(driveName)详解
- 急~~~谁有学生管理系统 程序给一个呗!
- 为什么我用FileWriter输出大文件时出现NullPointException?
- 初学者的一个小问题,请各位高手朋友不要笑话,本人刚开始学,不过10天.
- (异想天开)怎样为自己的程序定制一个jre。(高手看过来)
- java怎样连接ACCESS数据库
- 高分求教一个图片显示的问题
- 6000块我该配怎么样的机器?
- 哪位大虾帮我改错啊!(急!……)(内有原代码)
- 【GUI】怎样通过输入数字来控制时钟,面板和时钟程序已经写好。
- 急!请高手帮忙!!!
- Kettle 怎么进行日期处理啊?
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的更改
第二行把默认值(1月1日)的星座显示出来,
后面改变日期的事件已经收到,你的代码可以打印出month和date,类属性确实更改了,然后呢?
得把改变后的month,date转换成星座放到JLabel