java的强制转换问题,高手们请帮帮我丫!!谢谢啦
我做课程设计,要求根据输入的温度改变背景图片,如春夏秋冬的图片,但是我获取了文本框里的内容,强制转换不了,请大家帮帮忙,出问题的地方我打了*************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.URL;
import javax.swing.JPanel;
import java.applet.AudioClip;
//JButton,JTextField,JLabel的用法
public class CelsiusConverter implements ActionListener {
JFrame con;
JPanel Pl,P2;
JTextField txt,txt2;
JLabel Label1,Label2,Label3,Label4;
JButton niu,niu2;
String music = "白日夢鋼琴曲.wav";
//构造函数
public CelsiusConverter() {
//创建容器
con = new JFrame("温度转换器");
con.setSize(1000,800);
Pl = new JPanel();
P2 = new JPanel();
Pl.setLayout(new FlowLayout());
addWidgets_1();
P2.setLayout(new FlowLayout());
addWidgets();
this.setBack();
//添加背景音乐
AudioClip clip = Applet.newAudioClip(getClass().getResource(music));
clip.play();
//增加widgets
//向frame中添加panel
con.getContentPane().add(Pl,BorderLayout.NORTH); //北边
//关闭窗口时退出
con.getContentPane().add(P2,BorderLayout.SOUTH); //南边
con.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示转换器
//con.pack(); //限制改变窗体大小
con.setVisible(true); //可见
}
//为转换器创建和增加widgets
private void addWidgets_1(){
//创建widgets.
txt = new JTextField(20);
Label1 = new JLabel("摄氏温度",SwingConstants.LEFT);
niu = new JButton("转换……");
Label2 = new JLabel("华氏温度",SwingConstants.LEFT);
//诊听转换器按钮发出的事件
niu.addActionListener(this);
//向容器中添加widgets
Pl.add(txt);
Pl.add(Label1);
Pl.add(niu);
Pl.add(Label2);
Label1.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
Label2.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
}
private void addWidgets(){
txt2 = new JTextField(20);
Label3 = new JLabel("华氏温度",SwingConstants.RIGHT);
niu2 = new JButton("转换……");
Label4 = new JLabel("摄氏温度",SwingConstants.RIGHT);
//niu2.addActionListener(actionPerformed_1);
niu2.addActionListener(this);
P2.add(txt2);
P2.add(Label3);
P2.add(niu2);
P2.add(Label4);
Label3.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
Label4.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
}
//实现ActionListener接口
public void actionPerformed(ActionEvent e) { //成功运行
//将摄氏温度转换为双精度小数,并且转换为华氏温度
if(e.getSource()==niu)//有多个组件的话用这个方法获取事件源
{
int temp =
(int)((Double.parseDouble(txt.getText())) * 1.8 + 32);
Label2.setText(temp + " 华氏度");
}
else if(e.getSource()==niu2)
{
int temp1 =
(int)(((Double.parseDouble(txt2.getText()))-32)/1.8);
Label4.setText(temp1 + " 摄氏度");
}
}
//显示图片函数
public void setBack() {
((JPanel)con.getContentPane()).setOpaque(false);
//niu.getText();***************************************************
int n=Integer.parseInt(niu.getText());
//int d=niu.getText();
if(n>=30)
{
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下
URL url = StudentManager.class.getResource("夏.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}
else if(n>=25)
{
URL url = StudentManager.class.getResource("春.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}
else if(n>=10)
{
URL url = StudentManager.class.getResource("秋.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}
else
{
URL url = StudentManager.class.getResource("冬.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}
}
public static void main(String args[]) {
CelsiusConverter a=new CelsiusConverter();
}
}