java的强制转换问题,高手们请帮帮我丫!!谢谢啦

LXW295716355 2011-04-27 05:26:22
我做课程设计,要求根据输入的温度改变背景图片,如春夏秋冬的图片,但是我获取了文本框里的内容,强制转换不了,请大家帮帮忙,出问题的地方我打了*************************************************
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();
}
}





...全文
186 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
a19880905 2011-05-02
  • 打赏
  • 举报
回复
我的4张图片都能换,如果只能换两张图片,拿我也不知有什么问题了!
a19880905 2011-04-29
  • 打赏
  • 举报
回复

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;
JLabel background ;
JTextField txt,txt2;
JLabel Label1,Label2,Label3,Label4;
JButton niu,niu2;
String music = "白日夢鋼琴曲.wav";
//加了一个变量
int i=0;
//构造函数
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();
//添加背景音乐
//AudioClip clip = Applet.newAudioClip(getClass().getResource(music));
//clip.play();
//向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);
setBack(temp,i);
System.out.println(temp);
Label2.setText(temp + " 华氏度");
}
else if(e.getSource()==niu2)
{
int temp1 =
(int)(((Double.parseDouble(txt2.getText()))-32)/1.8);
System.out.println(temp1);

setBack(temp1,i);
Label4.setText(temp1 + " 摄氏度");
}
}
//改变了这个方法
//显示图片函数
public int setBack(int temp,int p) {
((JPanel)con.getContentPane()).setOpaque(false);
int n=temp;
if(n>=30)
{
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下
System.out.println("图片4出来了");
if(p==1)
con.getLayeredPane().remove(background);
ImageIcon img = new ImageIcon(getClass().getResource("春.jpg"));
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&&n<30)
{
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片1出来了");
ImageIcon img = new ImageIcon(getClass().getResource("秋.jpg"));
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&&n<25)
{
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片2出来了");
ImageIcon img = new ImageIcon(getClass().getResource("冬.jpg"));
background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}
else
{
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片3出来了");
ImageIcon img = new ImageIcon(getClass().getResource("夏.jpg"));
background = new JLabel(img);
con.getLayeredPane().add(background,
new Integer(Integer.MIN_VALUE));
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}

p=1;
i=p;
return i;
}

public static void main(String args[]) {
CelsiusConverter a=new CelsiusConverter();
}
}
现在能运行了
LXW295716355 2011-04-29
  • 打赏
  • 举报
回复
谢谢这位大哥,但是还有一点问题哦,就是不是按照正确的温度换的图片,换来换去只能换两张图啊!!!
a19880905 2011-04-28
  • 打赏
  • 举报
回复
int n=Integer.parseInt(niu.getText());
改成
int n = Integer.parseInt(txt.getText());


int n = Integer.parseInt(txt2.getText());

LXW295716355 2011-04-28
  • 打赏
  • 举报
回复
我是想根据输入的温度换图片来的,谁能帮我解决这个小问题吗,谢谢啊。
LXW295716355 2011-04-28
  • 打赏
  • 举报
回复
我改了还是有错啊,请高手指点我一二。。。
yingying19900908 2011-04-28
  • 打赏
  • 举报
回复
同上,你用niu.getText())是获得按钮转换的值啦

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧