public static class CityItem{
String cname;//城市名称
String ccode;//城市代码
// CityItem内部类构造器
public CityItem(String cname,String ccode)
{this.cname=cname; this.ccode=ccode;}
//重写toString方法
@Override
public String toString(){return cname;}
}
// YahooWeatherFramePre类构造器
public YahooWeatherFramePre() {
//对标题和logo图片进行初始化
this.setTitle("Yahoo天气预报客户端");
image=new ImageIcon("img/ico.png").getImage();
this.setIconImage(image);
//对界面中的控件进行摆放
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,6));
jl.setPreferredSize(new Dimension(200,20));
this.add(jl);
jcb.setPreferredSize(new Dimension(150,20));
this.add(jcb);
jb.setPreferredSize(new Dimension(60,20));
this.add(jb);
//对城市列表初始化
this.initCityList();
//为“确定”按钮添加监听器
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//获取当前选中的城市
CityItem currCI =
(CityItem)jcb.getSelectedItem();
String selectedCityCode = currCI.ccode;
//未来在此处添加创建天气预报信息显示
//窗口的代码…… YahooWeatherFramePre.this.dispose();
} } );
//设置窗体首次出现的大小和位置
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int centerX=screenSize.width/2;//屏幕中央x坐标
int centerY=screenSize.height/2; //屏幕中央y坐标
int w=250;//本窗体宽度
int h=100;//本窗体高度
//设置窗体出现在屏幕中央
this.setBounds(centerX-w/2,centerY-h/2-100,w,h);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);//设置窗体不可修改大小
}
|